Skip to content

Instantly share code, notes, and snippets.

@einaros
Created September 20, 2010 19:26
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 einaros/588495 to your computer and use it in GitHub Desktop.
Save einaros/588495 to your computer and use it in GitHub Desktop.
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