我试图使用CoreCLR动态调用特定类型的成员,但是在针对DNXCORE50进行编译时,Type.InvokeMember方法不可用。但是,如果我使用DNX451进行编译,它可以正常工作。
下面是如何使用DNX451实现这一目标的示例,但是如何在DNXCORE50中做到这一点呢?
using System;
using System.Reflection;
namespace InvokeMember
{
public class Program
{
public void Main(string[] args)
{
typeof (Program).InvokeMember("DoStuff", BindingFlags.InvokeMethod, null, new Program(), null);
}
public void DoStuff()
{
Console.WriteLine("Doing stuff");
}
}
}发布于 2015-11-18 15:52:32
使用此代码,它可以工作:
MethodInfo method = typeof(Program).GetTypeInfo().GetDeclaredMethod("DoStuff");
method.Invoke(new Program(), null);发布于 2016-12-07 22:24:28
对于任何使用Type.InvokeMember()和BindingFlags.SetProperty来设置对象属性(而不是BindingFlags.InvokeMethod)的人,您可以使用这个语法,这个语法与@aguetat给出的答案略有不同:
PropertyInfo property = typeof(Program).GetTypeInfo().GetDeclaredProperty("MyProperty");
property.SetValue(new Program(), newValue);https://stackoverflow.com/questions/33783828
复制相似问题