如何查找类实例的通用属性名称以及如何在运行时为属性赋值

发布于 2024-12-16 17:36:09 字数 1138 浏览 1 评论 0原文

我有以下课程。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2024-12-23 17:36:09

您可以在 Type 上使用 GetProperty,然后在 PropertyInfo 实例上使用 SetValue。根据您的描述,我认为您想要这样的东西:

void Main()
{
    BE be  = new BE();
    SetMyPropertyValue("RFID", be, 2);
    SetMyPropertyValue("CUSIP", be, "hello, world");

    Console.WriteLine(be.RFID.PropertyValue);
    Console.WriteLine(be.CUSIP.PropertyValue);
}

private void SetMyPropertyValue(string propertyName, object instance, object valueToSet) 
{
    Type be = instance.GetType();
    Type valueType = valueToSet.GetType();
    Type typeToSet = typeof(MyProperty<>).MakeGenericType(valueType);
    object value = Activator.CreateInstance(typeToSet,valueToSet);

    var prop = be.GetProperty(propertyName);
    prop.SetValue(instance, value, null);
}

You can use GetProperty on the Type, then use SetValue on the PropertyInfo instance. Based on your description, I think you want something like this:

void Main()
{
    BE be  = new BE();
    SetMyPropertyValue("RFID", be, 2);
    SetMyPropertyValue("CUSIP", be, "hello, world");

    Console.WriteLine(be.RFID.PropertyValue);
    Console.WriteLine(be.CUSIP.PropertyValue);
}

private void SetMyPropertyValue(string propertyName, object instance, object valueToSet) 
{
    Type be = instance.GetType();
    Type valueType = valueToSet.GetType();
    Type typeToSet = typeof(MyProperty<>).MakeGenericType(valueType);
    object value = Activator.CreateInstance(typeToSet,valueToSet);

    var prop = be.GetProperty(propertyName);
    prop.SetValue(instance, value, null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文