我在字符串中保存方法体。我想动态创建方法。但我不知道,如何设置它的身体。我看到使用 CodeDom 的方式非常乏味。我看到使用 Emit 和 OpCodes。有什么方法可以使用字符串变量中的现成代码吗?
string method_body = "return \"Hello, world!\";"; //there is method body
DynamicMethod dm = new System.Reflection.Emit.DynamicMethod("My_method",
typeof(string), new Type[] { }); //any way to create method dynamically
//any way to set body
string result = (string)dm.Invoke(...); //I need write result in variable