English 中文(简体)
如何找到集体财产名称以及如何分配财产价值的时间
原标题:How to find generic property name of class instance and how to assign value to the property run time

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{}
     }
}
最佳回答

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);
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签