如何查找类实例的通用属性名称以及如何在运行时为属性赋值
我有以下课程。在 BE 的实例中(假设是 objBE),我想在运行时选择属性名称并分配它的值。例如,我们有一个填充了所有属性的组合,并且在窗口窗体上有文本框和命令按钮。我想从组合中选择属性名称,然后在文本框中键入一些值,然后单击按钮,我想从 objBE 中查找属性名称,并将文本框值分配给所选属性。无法找到如何完成它的方法。可以帮忙一些。 提前致谢。 霍尼韦尔
public class MyPropertyBase
{
public int StartOffset { get; set; }
public int EndOffset { get; set; }
}
public class MyProperty<T> : MyPropertyBase
{
public MyProperty(T propertyValue)
{
PropertyValue = propertyValue;
}
public T PropertyValue { get; set; }
public static implicit operator MyProperty<T>(T t)
{
return new MyProperty<T>(t);
}
}
public class BE
{
private List<Admin_Fee> _Admin_Fee = new List<Admin_Fee>();
public MyProperty<int> RFID
{get;set;}
public MyProperty<string> CUSIP
{get;set;}
public MyProperty<string> FUND_CITY
{get;set;}
public MyProperty<int> SomeOtherProperty { get; set; }
//public List<MyPropertyBase> MyDataPoints { get; set; }
public List<Admin_Fee> Admin_Fee
{
get{return _Admin_Fee;}
set{}
}
}
I have following classes. In instance of BE (let's say objBE) i want to select property name on run time and assign it's value. e.g. we have a combo with all properties populated, and have text box and command button on window form. I want to select property name from the combo and type some value in text box and on button click i want to find the property name from the objBE and assign the text box value to the selected property. Couldn't get way how to get it done. Can some help.
Thanks in Advance.
H N
public class MyPropertyBase
{
public int StartOffset { get; set; }
public int EndOffset { get; set; }
}
public class MyProperty<T> : MyPropertyBase
{
public MyProperty(T propertyValue)
{
PropertyValue = propertyValue;
}
public T PropertyValue { get; set; }
public static implicit operator MyProperty<T>(T t)
{
return new MyProperty<T>(t);
}
}
public class BE
{
private List<Admin_Fee> _Admin_Fee = new List<Admin_Fee>();
public MyProperty<int> RFID
{get;set;}
public MyProperty<string> CUSIP
{get;set;}
public MyProperty<string> FUND_CITY
{get;set;}
public MyProperty<int> SomeOtherProperty { get; set; }
//public List<MyPropertyBase> MyDataPoints { get; set; }
public List<Admin_Fee> Admin_Fee
{
get{return _Admin_Fee;}
set{}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
Type
上使用GetProperty
,然后在PropertyInfo
实例上使用SetValue
。根据您的描述,我认为您想要这样的东西:You can use
GetProperty
on theType
, then useSetValue
on thePropertyInfo
instance. Based on your description, I think you want something like this: