I want invoke Method2 which is an extension method of a MyClass using MyClass type. I am wondering if this is possible or not.
using System;
using System.Linq;
using System.Reflection;
namespace ConsoleApplication9
{
    public static class MyClassStatic
    {
        public static void Method2(this ConsoleApp2.MyClass obj)
        {
            Console.WriteLine("You have called ex Method 2");
        }
    }
    public interface IClass
    {
        void Method1();
    }
    public class MyClass : ConsoleApp2.IClass
    {
        public void Method1()
        {
            Console.WriteLine("You have called Method 1");
        }
    }
    class Program
    {
        public static void CallWhereMethod()
        {
            var whereMethods = typeof(MyClass)
                .GetMethods(BindingFlags.Static | BindingFlags.Public)
                .Where(mi => mi.Name == "Method2");
            Console.WriteLine(whereMethods.Count());
            // returns zero
        }
        static void Main(string[] args)
        {
            CallWhereMethod();
            Console.ReadKey();
        }
    }
}
 
Aucun commentaire:
Enregistrer un commentaire