2017-02-24 52 views
-1
public class DummyResponse 
{ 
    public int UpdatedRecords { get; set; } 
    public string Id { get; set; } 
    public bool Isvalid { get; set; } 
} 

public class Request 
{ 
    public List<DummyResponse> Changes { get; set; } 
    public string ReuestedBy { get; set; } 

    public Request() 
    { 
     Changes = new List<DummyResponse>(); 
    } 
} 

我有一個平面文件,其中包含用於虛擬響應的製表符分隔數據。 我希望將其序列化爲Request對象。 實現需求應該是通用的,因爲在這種情況下我只需要用戶傳遞T(請求),並從平面文件中識別出正確的子類型。將平面文件轉換爲另一個類的屬性的列表

我有下面的代碼將其轉換成對象。它只能用於具有字符串類型的屬性。

interface ICollectionBuilder 
{ 
    object Build(IList dictionaries); 
} 

internal class CollectionBuilder<T> : ICollectionBuilder where T : new() 
{ 
    public object Build(IList dictionaries) 
    { 
     var dictConverter = new DictionaryConerter<T>(); 
     return dictionaries 
      .OfType<IDictionary<string, object>>() 
      .Select(dict => dictConverter.ConvertTyped(dict)) 
      .ToList(); 
    } 
} 

interface IDictionaryConverter 
{ 
    object Convert(IDictionary<string, object> dict); 
} 

internal class DictionaryConerter<T> : IDictionaryConverter where T : new() 
{ 
    public object Convert(IDictionary<string, object> dict) 
    { 
     return ConvertTyped(dict); 
    } 

    public T ConvertTyped(IDictionary<string, object> dict) 
    { 
     T t = new T(); 
     var properties = t.GetType().GetProperties(); 

     foreach (KeyValuePair<string, object> curr in dict) 
     { 
      if (String.IsNullOrEmpty(curr.Key)) continue; 
      if (curr.Value == null) continue; 

      Type valType = null; 
      Type newType = null; 
      PropertyInfo currProperty = null; 
      foreach (PropertyInfo p in properties) 
      { 
       if (String.IsNullOrEmpty(p.Name)) continue; 

       if (String.Compare(p.Name.ToLower(), curr.Key.ToLower()) == 0) 
       { 
        valType = t.GetType().GetProperty(p.Name).PropertyType; 
        newType = Nullable.GetUnderlyingType(valType) ?? valType; 
        currProperty = p; 
        break; 
       } 
      } 
      object newVal = curr.Value; 

      var curDict = curr.Value as IDictionary<string, object>; 
      var curList = curr.Value as IList; 
      if (curDict != null && newType.GetConstructor(Type.EmptyTypes) != null) 
      { 
       newVal = ((IDictionaryConverter)Activator.CreateInstance(typeof(DictionaryConerter<>).MakeGenericType(newType))).Convert(curDict); 
      } 
      else if (
       curList != null && 
       curList.OfType<IDictionary<string, object>>().Any() && 
       newType.IsGenericType && 
       newType.GetGenericTypeDefinition() == typeof(List<>) && 
       newType.GetGenericArguments()[0].GetConstructor(Type.EmptyTypes) != null) 
      { 
       newVal = ((ICollectionBuilder)Activator.CreateInstance(typeof(CollectionBuilder<>).MakeGenericType(newType.GetGenericArguments()[0]))).Build(curList); 
      } 

      t.GetType().GetProperty(currProperty.Name).SetValue(t, newVal); 
     } 

     return t; 
    } 
} 

用法示例:

void Main() 
    { 
     var dict = new Dictionary<string,object>(); 
     dict.Add("ReuestedBy",abc); 
     var innerDict = new Dictionary<string,object>(); 
     var list = new LIst<Dictionary<string,object>>(); 
     innerDict.Add("UpdatedRecords","45"); 
     innerDict.Add("Id","1"); 
     innerDict.Add("IsValid","False"); 
     dict.Add("Changes",list) 
    } 

這裏的問題是,它不工作的字符串以外的任何其他類型。

回答

0

我固定它用下面的代碼,同時設定值

   propertyVal = Convert.ChangeType(propertyVal, targetType); 
       propertyInfo.SetValue(inputObject, propertyVal, null); 
相關問題