Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Call generic implementation of method using Reflection
void Main()
GetPersisterFor(new Foo());
public interface IPersister { }
public interface IEntity { }
public class Foo : IEntity { }
public class Bar : IEntity { }
public IPersister GetPersisterFor(IEntity entity)
Console.WriteLine("Call GetPersisterFor(IEntity entity)");
MethodInfo getPersisterForGenericMethod =
// iterate over all methods to find proper generic implementation
.Single(methodInfo => methodInfo.Name == "GetPersisterFor" && methodInfo.IsGenericMethod)
// supply it with generic type parameter
// invoke it
return getPersisterForGenericMethod.Invoke(this, null) as IPersister;
public IPersister GetPersisterFor<TEntity>() where TEntity : IEntity
Console.WriteLine("Call GetPersisterFor<TEntity>");
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.