通过反射设置属性,使用隐式转换 [英] Setting a property by reflection, with implicit conversion
问题描述
鉴于课程:
class Class
{
public static implicit operator int(Class c)
{
返回0;
}
}
班级持有人
{
public int Property
{
set {}
}
}
我可以写代码:
持有人h =新持有人();
h .Property = new Class();
如何使用反射做同样的事情,而不对所使用的类进行任何更改
?
我尝试过:
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。 GetProperty(" Property");
pi.SetValue(h,c,System.Reflection.BindingFlags.SetProperty,
null,new object [] {},null );
抛出异常:
类型''System.ArgumentException'的未处理异常''
发生在mscorlib.dll
中
附加信息:不能从目标类型扩展到
原始类型。
尝试
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。GetProperty(" Property");
pi.SetValue(h," VALUE",null);
" Aaron Queenan" < **********了Aa @ discussions.microsoft.com>在消息中写道
news:42 ********************************** @ microsof t.com ...给出了类:
类Class
{public static implicit operator int(Class c)
{
返回0;
}
}
班主持人
{
public int Property
{
set {}
Holder h = new Holder();
h.Property = new Class() ;
如何使用反射做同样的事情,而不对所用的类进行任何更改
?
我试过:
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。GetProperty(" Property");
pi.SetValue(h,c,System.Reflection.BindingFlags.SetProperty ,
null,new object [] {},null);
抛出异常:
类型为''System.ArgumentException''的未处理异常
发生在mscorlib.dll中
其他信息on:无法从目标类型扩展到原始类型。
我也尝试过,但结果相同。 :-)
给定一个System.Type,我需要一些方法在
运行时将任何对象转换为该类型,同时尊重隐式转换真实对象的类型
是有能力的。我知道我可以通过搜索typeof(object)和
typeof(target)来实现它,以获得合适的隐式转换,但我希望已经有了一个
方法实施。
" Shardool Karnik"写道:尝试
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。GetProperty(" Property");
pi.SetValue(h,VALUE,null);
" Aaron Queenan" < **********了Aa @ discussions.microsoft.com>在消息中写道
新闻:42 ********************************** @ microsof t.com。 ..给出了类:
类Class
{public static implicit operator int(Class c)
{
返回0;
}
}
班主持人
{
public int Property
{
set {}
} <我可以写代码:
Holder h = new Holder();
h.Property = new Class();
如何使用反射做同样的事情而不做任何改动
使用的类?
我有试过:
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。GetProperty(" Property");
pi.SetValue(h,c ,System.Reflection.BindingFlags.SetProperty,
null,new object [] {},null);
引发异常:
未处理的异常在msco中输入''System.ArgumentException''
rlib.dll
附加信息:不能从目标类型扩展到原始类型。
>
好吧现在我很困惑......我在课堂上一直使用反射......如果你有点清楚的话,我可以帮助你... b / b $
从我的理解......你有一个用户定义的类型..你想要
填充一个属性值...是正确的吗?
,但你想使用Type.GetType而不是typeof
" Aaron Queenan" < **********了Aa @ discussions.microsoft.com>在消息中写道
news:01 ********************************** @ microsof t.com ...我也尝试过,但结果相同。 :-)
给定一个System.Type,我需要一些方法在
运行时将任何对象转换为该类型,同时尊重隐式转换
对象的实际类型有能力的。我知道我可以通过搜索typeof(object)和
typeof(target)来实现它,以获得合适的隐式转换,但我希望已经实现了
方法。
Shardool Karnik写道:尝试
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。GetProperty(" Property");
pi.SetValue(h,VALUE,null);
" Aaron Queenan" < **********了Aa @ discussions.microsoft.com>写在
消息新闻:42 ********************************** @ microsof t.com。 ..给出了类:
类Class
{public static implicit operator int(Class c)
{
返回0;
}
}
班主持人
{
public int Property
{
set {}
} <我可以写代码:
Holder h = new Holder();
h.Property = new Class();
如何使用反射做同样的事情,而不对所用的类进行任何
更改?
我有试过:
System.Reflection.PropertyInfo pi =
Type.GetType(" Holder")。GetProperty(" Property");
pi.SetValue(h,c ,System.Reflection.BindingFlags.SetProperty,
null,new object [] {},null);
引发异常:
未处理的异常在mscorl中输入''System.ArgumentException''
ib.dll
附加信息:不能从目标类型扩展到原始类型。
Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}
I can write the code:
Holder h = new Holder();
h.Property = new Class();
How can I do the same thing using reflection, without making any changes to
the classes used?
I have tried:
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);
An exception gets thrown:
An unhandled exception of type ''System.ArgumentException''
occurred in mscorlib.dll
Additional information: Cannot widen from target type to
primitive type.
try
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, "VALUE", null);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}
I can write the code:
Holder h = new Holder();
h.Property = new Class();
How can I do the same thing using reflection, without making any changes to the classes used?
I have tried:
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);
An exception gets thrown:
An unhandled exception of type ''System.ArgumentException''
occurred in mscorlib.dll
Additional information: Cannot widen from target type to
primitive type.
I''ve tried that too, but got the same result. :-)
Given a System.Type, I need some way to convert any object to that type at
runtime, while honouring the implicit conversions the real type of the object
is capable of. I know I could implement it by searching typeof(object) and
typeof(target) for suitable implicit conversions, but I''m hoping there''s a
method already implemented.
"Shardool Karnik" wrote:try
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, "VALUE", null);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}
I can write the code:
Holder h = new Holder();
h.Property = new Class();
How can I do the same thing using reflection, without making any changes
tothe classes used?
I have tried:
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);
An exception gets thrown:
An unhandled exception of type ''System.ArgumentException''
occurred in mscorlib.dll
Additional information: Cannot widen from target type to
primitive type.
ok now i am confused ... i use reflection all the time on my classes .... I
can help if you are a little clear ....
from what I understand .. you have a user defined type .. and u want to
populate a property value ... is that correct ??
but you want to use Type.GetType instead of typeof ???
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...I''ve tried that too, but got the same result. :-)
Given a System.Type, I need some way to convert any object to that type at
runtime, while honouring the implicit conversions the real type of the object is capable of. I know I could implement it by searching typeof(object) and
typeof(target) for suitable implicit conversions, but I''m hoping there''s a
method already implemented.
"Shardool Karnik" wrote:try
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, "VALUE", null);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message news:42**********************************@microsof t.com...Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}
I can write the code:
Holder h = new Holder();
h.Property = new Class();
How can I do the same thing using reflection, without making anychanges to
the classes used?
I have tried:
System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);
An exception gets thrown:
An unhandled exception of type ''System.ArgumentException''
occurred in mscorlib.dll
Additional information: Cannot widen from target type to
primitive type.
这篇关于通过反射设置属性,使用隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!