The following program works correctly. However, I would like to change it to not use InvokeMember. I would like to be able to cast the return value from CreateInstance into GenericBase and call the Foo method directly. In this code sample I know that the type is Derived. However, in my actual code, all I know is that the type is derived from GenericBase. All my attempts to cast an object into a GenericBase fail to compile. C# insists that I supply the type parameter when I use GenericType in a cast.
Is this possible?
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericTest
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(Derived);
object o = Activator.CreateInstance(t);
t.InvokeMember("Foo", System.Reflection.BindingFlags.InvokeMethod, null, o, new object[] { "Bar" });
}
}
class GenericBase<T>
{
public void Foo(string s)
{
Console.WriteLine(s);
}
}
class Derived : GenericBase<int>
{
}
}
A priori, you know nothing about the common features that might exist between
GenericBase<Bar>andGenericBase<Baz>. It’s possible they have nothing in common. So without knowing the type of the parent generic class, you actually don’t know anything useful at all about the object.Now, on the other hand, it’s clear that what you’re saying in this particular example is that there is something in common between
GenericBase<Bar>andGenericBase<Baz>— they both implement the non-genericvoid Foo(string s). So to be able to do something useful with your object, you need to formalize this behavior; putFoointo a non-genericIFoointerface, and haveGenericBase<T>implement the interface. Then you can cast your object to anIFoo, and callFoo.