Type.InvokeMember(..) in CoreCLR
Asked Answered
C

2

6

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");
        }
    }

}
Cordon answered 18/11, 2015 at 15:20 Comment(0)
U
4

With this code, it works :

        MethodInfo method = typeof(Program).GetTypeInfo().GetDeclaredMethod("DoStuff");
        method.Invoke(new Program(), null);
Undersell answered 18/11, 2015 at 15:52 Comment(2)
The original code should work too, if he throws in a GetTypeInfo().Nonparticipating
@BenVoigt: GetTypeInfo().InvokeMember does not work. 'TypeInfo' does not contain a definition for 'InvokeMember' and no extension method 'InvokeMember' accepting a first argument of type 'TypeInfo' could be found (are you missing a using directive or an assembly reference?) InvokeMember.DNX Core 5.0Cordon
O
0

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);
Ogden answered 7/12, 2016 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.