使用对象类型的字符串名称在c#中进行类型转换
我有下面的代码,应该很容易通过使用对象类型的字符串名称在c#中进行类型转换,c#,reflection,types,casting,C#,Reflection,Types,Casting,我有下面的代码,应该很容易通过 public class Foo { public void FooHasAMethod() { Console.WriteLine("it is me, foo!!!"); } } public class Bar { public Foo FooProperty { get; set; } } public class FooBar { public static void Main() {
public class Foo
{
public void FooHasAMethod()
{
Console.WriteLine("it is me, foo!!!");
}
}
public class Bar
{
public Foo FooProperty { get; set; }
}
public class FooBar
{
public static void Main()
{
Bar bar = new Bar{ FooProperty = new Foo() };
CallPropertyByName(bar, "Foo");
}
public static void CallPropertyByName(Bar bar, string propertyName)
{
PropertyInfo pi = bar.GetType().GetProperty(propertyName + "Property");
object fooObj = pi.GetValue(bar, null);
((Foo)fooObj).FooHasAMethod(); // this works
/* but I want to use
* ((Type.GetType(propertyName))fooObj).FooHasAMethod(); This line needs fix
* which doesnt work
* Is there a way to type cast using a string name of a object?
* */
}
}
不可能强制转换为编译时未知的类型
请看一下.NET 4.0的类型。如果您使用的是.NET 4,它实际上非常简单=D
dynamic obj = bar;
obj.FooProperty.FooHasAMethod();
但是,如果只想将结果强制转换为其他类型,可以在运行时使用Convert.ChangeType方法:
object someBoxedType = new Foo();
Bar myDesiredType = Convert.ChangeType(typeof(Bar), someBoxedType) as Bar;
现在,这一个与实际类型Foo和Bar有很强的链接。但是,您可以将该方法泛化以获得所需的:
public T GetObjectAs<T>(object source, T destinationType)
where T: class
{
return Convert.ChangeType(typeof(T), source) as T;
}
使用activator,您可以在运行时创建所需的任何类型。泛型方法被推理欺骗,试图从boxedType转换为运行时类型
此外,如果您只想对某个动态属性值调用一个方法,那么最佳实践(imo)就是将其转换为所需的对象
ISomething propValue = obj.GetProperty("FooPropery").GetValue(obj, null) as ISomething;
if(propValue != null)
propValue.FooHasAMethod();
无法使用字符串进行强制转换。但是您可以使用,或者如果
T
是一个类,Convert.ChangeType
是无用的(除非您使用的是IConvertible
)。转换当然会失败,但有一个例外。我有一个特别大的GenericConverter扩展方法,因此我可以简单地说anyObject.ConvertTo()
。
ISomething propValue = obj.GetProperty("FooPropery").GetValue(obj, null) as ISomething;
if(propValue != null)
propValue.FooHasAMethod();
Type fooObjType = fooObj.GetType();
MethodInfo method = fooObjType.GetMethod("FooHasAMethod");
method.Invoke(fooObj, new object[0]);