2011-12-22 68 views
2

我有一個字典,我想將其轉換爲循環中類的當前數據類型。擴展字符串,轉換爲數據類型

public static T ToClass<T>(this IDictionary<string, string> source) where T : class, new() 
{ 
    Type type = typeof(T); 
    T ret = new T(); 

    foreach (var keyValue in source) 
    { 
     type.GetProperty(keyValue.Key).SetValue(ret, keyValue.Value.ToNullable<T>(), null); 
    } 

    return ret; 
} 

public static Nullable<T> ToNullable<T>(this string s) where T : struct 
{ 
    Nullable<T> result = new Nullable<T>(); 
    try 
    { 
     if (!string.IsNullOrWhiteSpace(s)) 
     { 
      TypeConverter conv = TypeDescriptor.GetConverter(typeof(T)); 
      result = (T)conv.ConvertFrom(s); 
     } 
    } 
    catch { } 
    return result; 
} 

不知道HOWTO使keyValue.Value.ToNullable<T>()工作,我希望它它轉換爲數據類型在迴路中的電流特性。

這個例子是如何完成的?


我試過這段代碼,不能讓它工作。

public static T TestParse<T>(this string value) 
{ 
    return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(value); 
} 
+0

你ToNullable是使用T型這是不正確的,是包含所有屬性的目標類的類型。你應該使用每個屬性的類型(不幸的是你沒有在字典中)。 – Strillo 2011-12-22 10:00:07

+0

你有一個使它成爲不可空的轉換方法的例子嗎? – 2011-12-22 10:08:27

回答

2

嘗試以下方法:

/// <summary> 
/// ClassExtensions 
/// </summary> 
static class ClassExtensions 
{ 
    /// <summary> 
    /// Converts an object to nullable. 
    /// </summary> 
    /// <typeparam name="P">The object type</typeparam> 
    /// <param name="s">The object value.</param> 
    /// <returns></returns> 
    public static Nullable<P> ToNullable<P>(this string s) where P : struct 
    { 
     Nullable<P> result = new Nullable<P>(); 
     try 
     { 
      if (!string.IsNullOrWhiteSpace(s)) 
      { 
       TypeConverter conv = TypeDescriptor.GetConverter(typeof(P)); 
       result = (P)conv.ConvertFrom(s); 
      } 
     } 
     catch { } 
     return result; 
    } 

    /// <summary> 
    /// Converts a dictionary of property values into a class. 
    /// </summary> 
    /// <typeparam name="T">The class type.</typeparam> 
    /// <param name="source">The properties dictionary.</param> 
    /// <returns>An instance of T with property values set to the values defined in the dictionary.</returns> 
    public static T ToClass<T>(this IDictionary<string, string> source) where T : class, new() 
    { 
     Type classType = typeof(T); 
     T returnClass = new T(); 

     foreach (var keyValue in source) 
     { 
      PropertyInfo prop = classType.GetProperty(keyValue.Key); 

      MethodInfo method = typeof(ClassExtensions).GetMethod("ToNullable"); 
      MethodInfo generic = method.MakeGenericMethod(prop.PropertyType); 
      object convertedValue = generic.Invoke(keyValue.Value, new object[] { keyValue.Value }); 

      prop.SetValue(returnClass, convertedValue, null); 
     } 

     return returnClass; 
    } 
} 

/// <summary> 
/// TestClass 
/// </summary> 
class TestClass 
{ 
    public int Property1 { get; set; } 
    public long Property2 { get; set; } 
} 

/// <summary> 
/// Program. 
/// </summary> 
class Program 
{ 
    static void Main(string[] args) 
    { 
     IDictionary<string, string> properties = new Dictionary<string, string> { { "Property1", "1" }, { "Property2", "2" } }; 
     properties.ToClass<TestClass>(); 
    } 
} 
相關問題