How can I evaluate C# code dynamically?

后端 未结 16 1963
[愿得一人]
[愿得一人] 2020-11-22 03:37

I can do an eval(\"something()\"); to execute the code dynamically in JavaScript. Is there a way for me to do the same thing in C#?

An example of what I

16条回答
  •  遥遥无期
    2020-11-22 04:12

    You can use reflection to get the property and invoke it. Something like this:

    object result = theObject.GetType().GetProperty("Property" + i).GetValue(theObject, null);
    

    That is, assuming the object that has the property is called "theObject" :)

提交回复
热议问题