请问我要写一个公用类,根据泛型自动对属性赋值,返回的该泛型实体,应该怎么做?

[已解决] 请问我要写一个公用类,根据泛型自动对属性赋值,返回的该泛型实体,应该怎么做?

200
[软件及编程 > C#]
比如我有一个类(也可能是别的):
public class MyClass
{
   public int Id { get; set; }
   public string   Name { get; set; }
   public int? Age { get; set; }
}

想写一个方法,如:
T SetValue<T>([参数])

使用反射,根据T里面所有的属性,分别从[参数]中获取,应该怎么做呢?
注意:T里面所有的类型都是未知的。

另外要注意到int?类型,对于Nullable的类型怎么处理呢?
提问时间:2010/4/12 20:17:33 | 提问者:GaLiJiKuai | 悬赏:200 | 浏览:707
最佳答案
可以将下列代码复制到Console控制台中测试:
================== Program.cs 文件清单 =====================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication1
{
   public class MyClass
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public int? Age { get; set; }
   }

   class Program
   {
       static void Main(string[] args)
       {
           //模拟数据,如从POST提交中的Form中获取。
           Dictionary<string, string> values = new Dictionary<string, string>();
           values.Add("Id", "1");
           values.Add("Name", "TNT2");
           values.Add("Age", null);//可随意修改以测试,如"26"

           MyClass myClass = SetVaue<MyClass>(values);
           Console.WriteLine("从MyClass中获取到的ID:{0}", myClass.Id);
           Console.WriteLine("从MyClass中获取到的Name:{0}", myClass.Name);
           Console.WriteLine("从MyClass中获取到的Age:{0}", myClass.Age == null ? "Null" : myClass.Age.ToString());
           Console.ReadKey();
       }

       public static T SetVaue<T>(Dictionary<string, string> values) where T : new()
       {
           T result = new T();
           Type type = typeof(T);
           PropertyInfo[] pi = type.GetProperties();
           foreach (var item in pi)
           {
               Type proType = item.PropertyType;
               if (!values.ContainsKey(item.Name))
               {
                   continue;
               }
               object value = values[item.Name];
               //这里的IF也可以改用SWITCH来判断。
               if (proType == typeof(String))
               {
                   item.SetValue(result, value, null);
               }
               else if (proType == typeof(Int32))
               {
                   item.SetValue(result, item == null ? 0 : Convert.ToInt32(value), null);
               }
               else if (proType == typeof(Nullable<int>))//int?
               {
                   //所有Nulable<T>的类型以此类推,如double?类型
                   item.SetValue(result, values[item.Name] == null ? null : (int?)Convert.ToInt32(value), null);
               }
               else
               {
                   //继续用if或者switch/case添加更多的可能。以应对更复杂的自定义类型
               }
           }

           return result;
       }
   }
}

============= 返回结果 ============
从MyClass中获取到的ID:1
从MyClass中获取到的Name:TNT2
从MyClass中获取到的Age:Null
===================================

关键方法都在public static T SetVaue<T>(Dictionary<string, string> values) where T : new()中,其他只是帮助测试可以忽略。

关于Nullable<T>类型,如int?类型,注意理解这一句:
item.SetValue(result, values[item.Name] == null ? null : (int?)Convert.ToInt32(value), null);
其他的Nullable<T>类型的处理也都是类似的。
回答时间:2010/4/12 20:24:41 | 回答者:zsu
其他参考答案(0)
提交失败!请检查错误!错误信息:

注:以上所有信息由网友提供,仅供交流、参考,均不代表盛派网络言论,如果有任何问题或不妥,请立即联系我们

以下信息或许对您有用: