Created
September 20, 2010 19:26
-
-
Save einaros/588495 to your computer and use it in GitHub Desktop.
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
internal static class AssemblyExtension | |
{ | |
#region Methods: public | |
public static object CreateInstance(this Assembly self, string typeName, params object[] args) | |
{ | |
try | |
{ | |
return Activator.CreateInstance(self.GetType(typeName, true), args, null); | |
} | |
catch (Exception e) | |
{ | |
throw e.InnerException; | |
} | |
} | |
#endregion | |
} | |
internal static class ObjectExtension | |
{ | |
#region Methods: public | |
public static void Call(this object self, string methodName, params object[] args) | |
{ | |
try | |
{ | |
MethodInfo method = self.GetType().GetMethod(methodName, | |
BindingFlags.Instance | BindingFlags.Public | | |
BindingFlags.NonPublic, null, | |
args.Select(obj => obj.GetType()).ToArray(), null); | |
method.Invoke(self, args); | |
} | |
catch (Exception e) | |
{ | |
throw e.InnerException; | |
} | |
} | |
public static T Call<T>(this object self, string methodName, params object[] args) | |
{ | |
try | |
{ | |
MethodInfo method = self.GetType().GetMethod(methodName, | |
BindingFlags.Instance | BindingFlags.Public | | |
BindingFlags.NonPublic, null, | |
args.Select(obj => obj.GetType()).ToArray(), null); | |
return (T)method.Invoke(self, args); | |
} | |
catch (Exception e) | |
{ | |
throw e.InnerException; | |
} | |
} | |
public static T CallStatic<T>(Type type, string methodName, params object[] args) | |
{ | |
try | |
{ | |
MethodInfo method = type.GetMethod(methodName, | |
BindingFlags.Public | | |
BindingFlags.NonPublic | BindingFlags.Static, null, | |
args.Select(obj => obj.GetType()).ToArray(), null); | |
return (T)method.Invoke(null, args); | |
} | |
catch (Exception e) | |
{ | |
throw e.InnerException; | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment