Skip to content

Instantly share code, notes, and snippets.

@iguigova
Created August 31, 2011 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iguigova/1182841 to your computer and use it in GitHub Desktop.
Save iguigova/1182841 to your computer and use it in GitHub Desktop.
Invoking a Generic Method With Parameters using Reflection
private Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
public bool TryFindType(string typeName, out Type t)
{
lock (typeCache)
{
if (!typeCache.TryGetValue(typeName, out t))
{
t = Type.GetType(typeName);
if (t == null)
{
var types = from assembly in System.AppDomain.CurrentDomain.GetAssemblies()
from assemblyType in assembly.GetTypes()
where assemblyType.Name == typeName
select assemblyType;
t = types.FirstOrDefault();
}
typeCache[typeName] = t; // perhaps null
}
}
return t != null;
}
public string Get(string entityTypeName, string query)
{
Type entityType;
if (TryFindType(entityTypeName, out entityType))
{
var method = <owner_of_Lookup_method>.GetType()
.GetMethod("Lookup", new Type[] {typeof (string)})
.MakeGenericMethod(entityType);
return Convert.ToString(method.Invoke(<owner_of_Lookup_method>, new[] { query }));
}
return string.Empty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment