henningst
henningst

Reputation: 1684

Type.InvokeMember(..) in CoreCLR

I'm trying to dynamically invoke a member on a specific type using CoreCLR, but the method Type.InvokeMember is not available when compiling against DNXCORE50. However, if I compile against DNX451 it works fine.

Below is a sample of how this can be achieved using DNX451, but how can I do the same in 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");
        }
    }

}

Upvotes: 6

Views: 1368

Answers (2)

Sam H
Sam H

Reputation: 879

For anyone that may have been using Type.InvokeMember() with BindingFlags.SetProperty to set a property on an object (instead of BindingFlags.InvokeMethod), you can use this syntax which is slightly modified from the answer given by @aguetat:

PropertyInfo property = typeof(Program).GetTypeInfo().GetDeclaredProperty("MyProperty");
property.SetValue(new Program(), newValue);

Upvotes: 0

aguetat
aguetat

Reputation: 514

With this code, it works :

        MethodInfo method = typeof(Program).GetTypeInfo().GetDeclaredMethod("DoStuff");
        method.Invoke(new Program(), null);

Upvotes: 4

Related Questions