转换为A类在C#.NET飞 [英] Convert to a Type on the fly in C#.NET
本文介绍了转换为A类在C#.NET飞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对一个对象转换为在运行时被分配一个类型。
例如,假设我有一个分配的字符串值的函数(从文本框或DROPDOWNLIST
)到 Object.Property
。
我怎么会值转换为正确的类型?例如,它可以是一个整数,字符串或枚举。
公共无效美孚(obj对象,字符串propertyName的,对象的值)
{
//获取类型物业OG的对象。
型T = obj.GetType()的getProperty(作为propName).PropertyType。
//现在设置该属性的值。
//但它引发错误,因为有时候类型为int和值为2
//或类型是枚举(EA:Gender.Male)和值是男
//假设总是投是否有效(2可以转换成int 2)
obj.GetType()的getProperty(作为propName).SetValue(OBJ,值null)。
}
解决方案
您需要使用转换.ChangeType(...)功能(输入可以很容易地将一个对象......我只是一个字符串版本预焙):
///<总结>
///在一个对象设置一个值,用于隐藏所有进入
///处理这种事情的逻辑,所以这是做工考究在一行。
///< /总结>
///< PARAM NAME =目标>< /参数>
///< PARAM NAME =PROPERTYNAME>< /参数>
///< PARAM NAME =为PropertyValue>< /参数>
公共静态无效SetValueFromString(此对象的目标,字符串propertyName的,字符串为PropertyValue)
{
的PropertyInfo oProp = target.GetType()的getProperty(propertyName的)。
型tProp = oProp.PropertyType;
//可空属性有区别对待,因为我们
//使用其基本属性的对象
如果(tProp.IsGenericType
设置值&功放;&安培; tProp.GetGenericTypeDefinition()等于(typeof运算(可空<>)))
{
//如果是空,只需设置从保留字空值,并返回
如果(为PropertyValue == NULL)
{
oProp.SetValue(目标,NULL,NULL);
的回报;
}
//获取基础类型属性,而不是可空通用
tProp =新NullableConverter(oProp.PropertyType).UnderlyingType;
}
//使用转换器来得到正确的值
oProp.SetValue(目标,Convert.ChangeType(为PropertyValue,tProp),NULL);
}
I want to to convert an Object to a type that will be assigned at runtime.
For example, assume that I have a function that assigns a string value(from a TextBox or Dropdownlist
) to an Object.Property
.
How would I convert the value to proper type? For instance, it could be an integer, string, or enum.
Public void Foo(object obj,string propertyName,object value)
{
//Getting type of the property og object.
Type t= obj.GetType().GetProperty(propName).PropertyType;
//Now Setting the property to the value .
//But it raise an error,because sometimes type is int and value is "2"
//or type is enum (e.a: Gender.Male) and value is "Male"
//Suppose that always the cast is valid("2" can be converted to int 2)
obj.GetType().GetProperty(propName).SetValue(obj, value, null);
}
解决方案
You need to use the Convert.ChangeType(...) function (the input could just as easily be an object ... I just had a string version pre-baked):
/// <summary>
/// Sets a value in an object, used to hide all the logic that goes into
/// handling this sort of thing, so that is works elegantly in a single line.
/// </summary>
/// <param name="target"></param>
/// <param name="propertyName"></param>
/// <param name="propertyValue"></param>
public static void SetValueFromString(this object target, string propertyName, string propertyValue)
{
PropertyInfo oProp = target.GetType().GetProperty(propertyName);
Type tProp = oProp.PropertyType;
//Nullable properties have to be treated differently, since we
// use their underlying property to set the value in the object
if (tProp.IsGenericType
&& tProp.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
//if it's null, just set the value from the reserved word null, and return
if (propertyValue == null)
{
oProp.SetValue(target, null, null);
return;
}
//Get the underlying type property instead of the nullable generic
tProp = new NullableConverter(oProp.PropertyType).UnderlyingType;
}
//use the converter to get the correct value
oProp.SetValue(target, Convert.ChangeType(propertyValue, tProp), null);
}
这篇关于转换为A类在C#.NET飞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文