请问我要写一个公用类,根据泛型自动对属性赋值,返回的该泛型实体,应该怎么做?
[已解决] 请问我要写一个公用类,根据泛型自动对属性赋值,返回的该泛型实体,应该怎么做?
当前页面:http://www.senparc.com/SZD-51
{ 收藏当前页面 }
最佳答案
可以将下列代码复制到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>类型的处理也都是类似的。
================== 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)
提交失败!请检查错误!错误信息:
以下信息或许对您有用:
- [已解决] 5 ArrayList或者HashTable支持序列化和反序列化吗? 2010/9/9 23:12:41
- [已解决] 5 JSON.Net的SerializeObject可以处理List数据吗? 2010/9/9 23:05:22
- [已解决] 10 在VS2010中装Silverlight tools 4出错 2010/8/13 8:40:43
- [已解决] 5 C#中如何取余数? 2010/8/3 18:39:49
- [已解决] 10 C#中委托能不能序列化?WCF中需要传递委托! 2010/7/31 11:09:43