C# 反射的PropertyInfo中的SetValue
我正在使用反射从反射属性中设置属性。我必须使用反射,因为我不知道子属性的类型,但是每次我得到System.Target.TargetException(在prop.SetValue上)时,prop都指向正确的属性 我可以找到很多SetValue的例子,我遇到的问题是,我希望selectSubProcess是一个PropertyInfo而不是一个实际的类C# 反射的PropertyInfo中的SetValue,c#,reflection,subclass,propertyinfo,C#,Reflection,Subclass,Propertyinfo,我正在使用反射从反射属性中设置属性。我必须使用反射,因为我不知道子属性的类型,但是每次我得到System.Target.TargetException(在prop.SetValue上)时,prop都指向正确的属性 我可以找到很多SetValue的例子,我遇到的问题是,我希望selectSubProcess是一个PropertyInfo而不是一个实际的类 PropertyInfo selectedSubProcess = process.GetProperty(e.ChangedItem.Pare
PropertyInfo selectedSubProcess = process.GetProperty(e.ChangedItem.Parent.Label);
Type subType = selectedSubProcess.PropertyType;
PropertyInfo prop = subType.GetProperty(e.ChangedItem.Label + "Specified");
if (prop != null)
{
prop.SetValue(process, true, null);
}
看起来进程是“类型”,而不是对象的实例。排队
prop.SetValue(process, true, null);
您需要设置对象的实例,而不是类型
使用“GetValue”获取您关心的对象的实例:
public void test()
{
A originalProcess = new A();
originalProcess.subProcess.someBoolean = false;
Type originalProcessType = originalProcess.GetType();
PropertyInfo selectedSubProcess = originalProcessType.GetProperty("subProcess");
object subProcess = selectedSubProcess.GetValue(originalProcess, null);
Type subType = selectedSubProcess.PropertyType;
PropertyInfo prop = subType.GetProperty("someBoolean");
if (prop != null)
{
prop.SetValue(subProcess, true, null);
}
MessageBox.Show(originalProcess.subProcess.someBoolean.ToString());
}
public class A
{
private B pSubProcess = new B();
public B subProcess
{
get
{
return pSubProcess;
}
set
{
pSubProcess = value;
}
}
}
public class B
{
private bool pSomeBoolean = false;
public bool someBoolean
{
get
{
return pSomeBoolean;
}
set
{
pSomeBoolean = true;
}
}
}
看起来进程是“类型”,而不是对象的实例。排队
prop.SetValue(process, true, null);
您需要设置对象的实例,而不是类型
使用“GetValue”获取您关心的对象的实例:
public void test()
{
A originalProcess = new A();
originalProcess.subProcess.someBoolean = false;
Type originalProcessType = originalProcess.GetType();
PropertyInfo selectedSubProcess = originalProcessType.GetProperty("subProcess");
object subProcess = selectedSubProcess.GetValue(originalProcess, null);
Type subType = selectedSubProcess.PropertyType;
PropertyInfo prop = subType.GetProperty("someBoolean");
if (prop != null)
{
prop.SetValue(subProcess, true, null);
}
MessageBox.Show(originalProcess.subProcess.someBoolean.ToString());
}
public class A
{
private B pSubProcess = new B();
public B subProcess
{
get
{
return pSubProcess;
}
set
{
pSubProcess = value;
}
}
}
public class B
{
private bool pSomeBoolean = false;
public bool someBoolean
{
get
{
return pSomeBoolean;
}
set
{
pSomeBoolean = true;
}
}
}
在转换代码时遇到一些问题,我的GetValue和SetValue需要一个额外的参数,当我将它们设置为Null时。。。我得到了与以前相同的错误。您正在运行哪个.Net版本?代码已更新为.Net 2.0,尽管唯一的更改是添加空参数,听起来您已经这样做了。你能让我的代码按原样工作吗(也就是说,不要试图将其转换为你的结构)?另外,在设置值时,确保传递的是布尔值的父对象的实例,而不是布尔值本身的实例。有关在父级中设置哪个属性的信息由PropertyInfo提供。如果在转换代码时遇到一些问题,我的GetValue和SetValue需要一个额外的参数,当我将它们设置为Null时。。。我得到了与以前相同的错误。您正在运行哪个.Net版本?代码已更新为.Net 2.0,尽管唯一的更改是添加空参数,听起来您已经这样做了。你能让我的代码按原样工作吗(也就是说,不要试图将其转换为你的结构)?另外,在设置值时,确保传递的是布尔值的父对象的实例,而不是布尔值本身的实例。PropertyInfo提供了有关在父级中设置哪个属性的信息