2012-02-13 112 views
1

創建匿名類型以便爲屬性名稱創建別名時可以嗎?匿名類型屬性的別名

我的問題是,我的屬性名稱是相當大的,我試圖JSON數據保持在最低限度,以使其更加輕便,而不是改變,這是非常描述實際的屬性名稱,我是想知道我是否可以爲每個屬性創建一個別名?

var result = myModel.Options.Select(l => new { l.Id, l.LargePropertyName, l.LargePropertyName2 }).ToDictionary(l => l.Id.ToString(), l => new { l.LargePropertyName1, l.LargePropertyName2 }); 
JavaScriptSerializer serializer = new JavaScriptSerializer(); 
Json = serializer.Serialize(result); 

非常感謝

回答

0

下面的代碼片段:

var resultWithLengthyPropertyNames = new { Identificator = 1, VeryLengthyPropertyName1 = "Fred", VeryLengthyPropertyName2 = "Brooks" }; 

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
var sourceJSON = serializer.Serialize(resultWithLengthyPropertyNames); 

System.Console.WriteLine("Source JSON => {0}", sourceJSON); 

string[] propertyNamesAliases = { "ID", "FN", "LN"};   
var intermediateResult = (IDictionary<string, object>)serializer.DeserializeObject(sourceJSON); 
var renamedIntermediateResult = new Dictionary<string, object>(); 
for (int i = 0; i < propertyNamesAliases.Length; i++) 
    renamedIntermediateResult.Add(propertyNamesAliases[i], 
     intermediateResult[intermediateResult.Keys.ToArray()[i]]); 

var convertedJSON = serializer.Serialize(renamedIntermediateResult); 

System.Console.WriteLine("Converted JSON => {0}", convertedJSON); 

結果測試輸出:

Source JSON => {"Identificator":1,"VeryLengthyPropertyName1":"Fred","VeryLengthyPropertyName2":"Brooks"} 
Converted JSON => {"ID":1,"FN":"Fred","LN":"Brooks"} 

解決方案沒有創建重命名屬性新的匿名對象名稱,但它確實解決了保持JSON字符串輕量級的任務,不是嗎?

P.S.這裏是第二種解決方案:

var resultWithLengthyPropertyNames = new { Identificator = 1, VeryLengthyPropertyName1 = "Fred", VeryLengthyPropertyName2 = "Brooks" }; 

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
var sourceJSON = serializer.Serialize(resultWithLengthyPropertyNames); 

System.Console.WriteLine("Source JSON => {0}", sourceJSON); 

Dictionary<string, string> propertyNamesAliases = new Dictionary<string, string>() 
      { 
       { "Identificator", "ID"}, 
       { "VeryLengthyPropertyName1", "FN" }, 
       { "VeryLengthyPropertyName2", "LN" } 
      }; 

var renamedTempResult = new Dictionary<string, object>(); 

foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(resultWithLengthyPropertyNames)) 
{ 
    renamedTempResult.Add(
      propertyNamesAliases[propertyDescriptor.Name], 
      propertyDescriptor.GetValue(resultWithLengthyPropertyNames)); 
} 

var convertedJSON = serializer.Serialize(renamedTempResult); 

System.Console.WriteLine("Converted JSON => {0}", convertedJSON); 

P.P.S.這是另一種解決方案 - 似乎是通過創建具有別名屬性的匿名對象直接解決任務:

var resultWithLengthyPropertyNames = new { Identificator = 1, VeryLengthyPropertyName1 = "Fred", VeryLengthyPropertyName2 = "Brooks" }; 

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 

var sourceJSON = serializer.Serialize(resultWithLengthyPropertyNames); 

System.Console.WriteLine("Source JSON => {0}", sourceJSON); 

var targetObjectTemplate = new { ID = -1, FN = "", LN = "" }; 

var convertedObject = 
      Activator.CreateInstance(targetObjectTemplate.GetType(), 
        resultWithLengthyPropertyNames.GetType() 
       .GetProperties() 
       .Select(p => p.GetValue(resultWithLengthyPropertyNames)) 
       .ToArray()); 

var convertedJSON = serializer.Serialize(convertedObject); 

System.Console.WriteLine("Converted JSON => {0}", convertedJSON);