2017-02-24 6 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(要求)を渡すだけで、フラットファイルから記入する正しいサブタイプを特定する必要があります。フラットファイルを別のクラスのプロパティであるリストに変換する

以下のコードをObjectに変換するコードがあります。どのように唯一の文字列型を持つプロパティのために働いています。

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); 
を設定して私は、コードの下に使用して固定
関連する問題