Created
August 31, 2011 04:50
-
-
Save iguigova/1182841 to your computer and use it in GitHub Desktop.
Invoking a Generic Method With Parameters using Reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
http://www.codeproject.com/Tips/247781/Invoking-a-Generic-Method-With-Parameters-using-Re