[英]Type.InvokeMember(..) in CoreCLR
我正在尝试使用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");
}
}
}
使用此代码,它可以工作:
MethodInfo method = typeof(Program).GetTypeInfo().GetDeclaredMethod("DoStuff");
method.Invoke(new Program(), null);
对于可能一直使用Type.InvokeMember()和BindingFlags.SetProperty
来设置对象属性(而不是BindingFlags.InvokeMethod
)的任何人,您都可以使用以下语法,该语法与@aguetat给出的答案略有不同:
PropertyInfo property = typeof(Program).GetTypeInfo().GetDeclaredProperty("MyProperty");
property.SetValue(new Program(), newValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.