c#,利用反射设置属性值
2010-01-29 16:04
267 查看
代码
1 /// <summary>
2 /// 设置相应属性的值
3 /// </summary>
4 /// <param name="entity">实体</param>
5 /// <param name="fieldName">属性名</param>
6 /// <param name="fieldValue">属性值</param>
7 public static void SetValue(object entity, string fieldName, string fieldValue)
8 {
9 Type entityType = entity.GetType();
PropertyInfo propertyInfo = entityType.GetProperty(fieldName);
if (IsType(propertyInfo.PropertyType, "System.String"))
{
propertyInfo.SetValue(entity, fieldValue, null);
}
if (IsType(propertyInfo.PropertyType, "System.Boolean"))
{
propertyInfo.SetValue(entity, Boolean.Parse(fieldValue), null);
}
if (IsType(propertyInfo.PropertyType, "System.Int32"))
{
if (fieldValue != "")
propertyInfo.SetValue(entity, int.Parse(fieldValue), null);
else
propertyInfo.SetValue(entity, 0, null);
}
if (IsType(propertyInfo.PropertyType, "System.Decimal"))
{
if (fieldValue != "")
propertyInfo.SetValue(entity, Decimal.Parse(fieldValue), null);
else
propertyInfo.SetValue(entity, new Decimal(0), null);
}
if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.DateTime]"))
{
if (fieldValue != "")
{
try
{
propertyInfo.SetValue(
entity,
(DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null), null);
}
catch
{
propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null), null);
}
}
else
propertyInfo.SetValue(entity, null, null);
}
}
/// <summary>
/// 类型匹配
/// </summary>
/// <param name="type"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public static bool IsType(Type type, string typeName)
{
if (type.ToString() == typeName)
return true;
if (type.ToString() == "System.Object")
return false;
return IsType(type.BaseType, typeName);
}
1 /// <summary>
2 /// 设置相应属性的值
3 /// </summary>
4 /// <param name="entity">实体</param>
5 /// <param name="fieldName">属性名</param>
6 /// <param name="fieldValue">属性值</param>
7 public static void SetValue(object entity, string fieldName, string fieldValue)
8 {
9 Type entityType = entity.GetType();
PropertyInfo propertyInfo = entityType.GetProperty(fieldName);
if (IsType(propertyInfo.PropertyType, "System.String"))
{
propertyInfo.SetValue(entity, fieldValue, null);
}
if (IsType(propertyInfo.PropertyType, "System.Boolean"))
{
propertyInfo.SetValue(entity, Boolean.Parse(fieldValue), null);
}
if (IsType(propertyInfo.PropertyType, "System.Int32"))
{
if (fieldValue != "")
propertyInfo.SetValue(entity, int.Parse(fieldValue), null);
else
propertyInfo.SetValue(entity, 0, null);
}
if (IsType(propertyInfo.PropertyType, "System.Decimal"))
{
if (fieldValue != "")
propertyInfo.SetValue(entity, Decimal.Parse(fieldValue), null);
else
propertyInfo.SetValue(entity, new Decimal(0), null);
}
if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.DateTime]"))
{
if (fieldValue != "")
{
try
{
propertyInfo.SetValue(
entity,
(DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null), null);
}
catch
{
propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null), null);
}
}
else
propertyInfo.SetValue(entity, null, null);
}
}
/// <summary>
/// 类型匹配
/// </summary>
/// <param name="type"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public static bool IsType(Type type, string typeName)
{
if (type.ToString() == typeName)
return true;
if (type.ToString() == "System.Object")
return false;
return IsType(type.BaseType, typeName);
}
相关文章推荐
- 利用反射取得类中属性(引用及非引用数据类型)及设置该类实例化对象的属性值
- 利用java反射获取和设置属性值
- c#中如何利用反射设置属性值
- c# 利用反射设置属性值
- [转]c#中如何利用反射设置属性值
- Android 应用利用反射机制获取/设置系统属性值
- c# 利用反射设置属性值
- 利用反射 获取android布局文件中设置的属性值
- Java 用反射设置对象的属性值
- 利用反射技术动态获取任意Java类实例的属性值
- 使用反射设置自定义类的属性值
- C#反射技术之一动态读取和设置对象的属性值
- 利用反射通用的设置某个对象的某个字段为指定的值
- C#反射设置属性值和获取属性值
- c# 如何通过反射 获取\设置属性值
- 反射获取属性值并设置属性值
- [转] C#反射设置属性值和获取属性值
- 如何利用jqGrid表格方法重新设置caption属性值
- C#反射设置属性值和获取属性值
- 利用反射对私有属性/方法进行设置/调用