在C#.NET中即时转换为Type

我想将Object转换为将在运行时分配的类型。

例如,假设我有一个函数将字符串值(从TextBox or Dropdownlist )分配给Object.Property

如何将值转换为正确的类型? 例如,它可以是整数,字符串或枚举。

 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 (ea: 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); } 

你需要使用Convert.ChangeType(…)函数(输入可以很容易成为一个对象……我只是预先编写了一个字符串版本):

 ///  /// 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. ///  ///  ///  ///  public static void SetPropertyValueFromString(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); } 

您寻求的是通用型转换器! 不容易的壮举..

试试这种方法:

通用型转换器

您可以在NuGet Install-Package UniversalTypeConverter

此外,在这里使用GenericsGenerics的? 如果您至少知道转换的目标类型,它将有助于解决方案。

这是你可以做到的另一种方式,但我不确定

不支持generics类型:

 public void Foo(object obj,string propertyName,object value) { //Getting type of the property og object. Type type = obj.GetType().GetProperty(propertyName).PropertyType; obj.GetType().GetProperty(propertyName).SetValue(obj, Activator.CreateInstance(type, value), null); } 

支持generics类型:

 public void Foo(object obj,string propertyName,object value) { //Getting type of the property og object. Type type = obj.GetType().GetProperty(propertyName).PropertyType; if (type.IsGenericType) type = type.GetGenericArguments()[0]; obj.GetType().GetProperty(propertyName).SetValue(obj, Activator.CreateInstance(type, value), null); } 

我不确定它是否有效,但试一试:

 public static T To(this IConvertible obj) { return (T)Convert.ChangeType(obj, typeof(T)); } Public void Foo(object obj,string propertyName,object value) { Type t= obj.GetType().GetProperty(propName).PropertyType; obj.GetType().GetProperty(propName).SetValue(obj, value.To(), null); } 

我在C#中使用了Convert函数。 我正在进行转换的方式是使用reflection:

 Type type = this.myObject.GetType(); if (type.Name == "MyObjectClass") { var Voucherrequest = (MyObjectClass)Convert.ChangeType(myObject, typeof(MyObjectClass)); 

这都是假设您知道要转换为何种类型。 您甚至可以进行切换并具有您想要反映的多种类型。