Update a property on an object knowing only its name

后端 未结 3 890
梦如初夏
梦如初夏 2021-01-06 03:40

I have some public variables that are defined as follows:

public class FieldsToMonitor
{
    public int Id { get; set; }
    public string Title { get; set;          


        
3条回答
  •  时光取名叫无心
    2021-01-06 04:41

    You're going to have to use reflection to accomplish that. Here's a short example:

    class Foo
    {
        public int Num { get; set; }
    }
    
    static void Main(string[] args)
    {
        Foo foo = new Foo() { Num = 7 };
    
        Console.WriteLine(typeof(Foo).GetProperty("Num").GetValue(foo, null));
    }
    

提交回复
热议问题