dimanche 17 mars 2019

How to call a method in abstract class inheritance

    private abstract class A<T>
    {
        public abstract void DoSomething(string asd, T obj);
    }

    private class MyClass : A<int>
    {
        public override void DoSomething(string asd, int obj)
        {
            Console.WriteLine(obj);
        }
    }

    static void Main(string[] args)
    {
        Type unboundGenericType = typeof(A<>);
        Type boundGenericType = unboundGenericType.MakeGenericType(typeof(int));
        MethodInfo doSomethingMethod = boundGenericType.GetMethod("DoSomething");
        object instance = Activator.CreateInstance(boundGenericType);
        doSomethingMethod.Invoke(instance, new object[] {"Hello", 123});
    }

Error retrieving method or instance. I also tried to call the usual method, but also errors :(





Aucun commentaire:

Enregistrer un commentaire