C# 基于[Type]对象转换和强制转换对象
希望这是一个足够简单的问题,但我正在努力寻找答案 如何将“对象”强制转换为“类型”对象中定义的类型 例如,基本上,我正在尝试对反射执行以下操作,使其尽可能通用:C# 基于[Type]对象转换和强制转换对象,c#,reflection,casting,C#,Reflection,Casting,希望这是一个足够简单的问题,但我正在努力寻找答案 如何将“对象”强制转换为“类型”对象中定义的类型 例如,基本上,我正在尝试对反射执行以下操作,使其尽可能通用: public class MyClass { public string Prop1 { get; set; } public int Prop2 { get; set; } } public T FillMyClass<T>() where T : new() { //The definitio
public class MyClass
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
}
public T FillMyClass<T>() where T : new()
{
//The definitions here are suppled in code - each class we want to work with needs to be "mapped".
string name1 = "Prop1";
Type type1 = typeof(string);
string name2 = "Prop2";
Type type2 = typeof(int);
//The values always start out as a string because of the way I'm receiving it.
string val1 = "test";
string val2 = "1";
T t= new T();
//Works fine, because val1 is already a string
t.GetType().GetProperty(name1).SetValue(t, val1, null);
//Having trouble with the below.
object o = Convert.ChangeType(val2, type2);
//Fails because o is not an int
t.GetType().GetProperty(name2).SetValue(t, o, null);
}
公共类MyClass
{
公共字符串Prop1{get;set;}
公共int Prop2{get;set;}
}
公共T FillMyClass(),其中T:new()
{
//这里的定义是在代码中补充的——我们想要处理的每个类都需要“映射”。
字符串名称1=“Prop1”;
类型1=类型(字符串);
字符串名称2=“Prop2”;
类型2=类型(int);
//由于我接收值的方式,这些值总是以字符串开头。
字符串val1=“测试”;
字符串val2=“1”;
T=新的T();
//工作正常,因为val1已经是一个字符串
t、 GetType().GetProperty(name1).SetValue(t,val1,null);
//有以下问题。
对象o=Convert.ChangeType(val2,type2);
//失败,因为o不是int
t、 GetType().GetProperty(name2).SetValue(t,o,null);
}
因此,类型是由用户定义的(甚至可能只是通过查找属性的类型)。
但我就是不知道如何将对象强制转换为[Type]。我也曾多次遇到过这个问题。虽然我确信有一个更优雅的解决方案来解决这个问题,但我已经做了以下扩展方法来帮助您实现99%的目标
public static object TryConvertToType(this object source, Type destinationType, object defaultValue = null)
{
try
{
if (source == null)
return defaultValue;
if (destinationType == typeof(bool))
{
bool returnValue = false;
if (!bool.TryParse(source.ToString(), out returnValue))
{
return Convert.ChangeType(source.ToString() == "1", destinationType);
}
else
{
return Convert.ChangeType(returnValue, destinationType);
}
}
else if (destinationType.IsSubclassOf(typeof(Enum)))
{
try
{
return Enum.Parse(destinationType, source.ToString());
}
catch
{
return Enum.ToObject(destinationType, source);
}
}
else if (destinationType == typeof(Guid))
{
return Convert.ChangeType(new Guid(source.ToString().ToUpper()), destinationType);
}
else if (destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type genericType = destinationType.GetGenericArguments().First();
return Convert.ChangeType(source, genericType);
}
else if (source.GetType().IsSubclassOf(destinationType))
{
return Convert.ChangeType(source, destinationType);
}
else if (!source.GetType().IsValueType
&& source.GetType() != typeof(string)
&& destinationType == typeof(string))
{
return Convert.ChangeType(source.GetType().Name, destinationType);
}
else
{
return Convert.ChangeType(source, destinationType);
}
}
catch
{
return defaultValue;
}
}
对我来说很好。尽管您确实需要一个
返回值
,以使其能够编译。
t.GetType().GetProperty(name).GetValue(t, null).TryConvertToType(type2, 0);