I have a extension method as below.
public static class ExtensionMethod
{
public static string GetTableName<T>(this ObjectContext context) where T : class
{
//Content
}
}
And I invoke this method as below.
static void Main(string[] args)
{
using (BreakAwayEntities context = new BreakAwayEntities())
{
Customer cus = context.Customers.First();
ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(cus);
Type t = entry.Entity.GetType();
MethodInfo method = typeof(ExtensionMethod).GetMethod("GetTableName");
MethodInfo genericMethod = method.MakeGenericMethod(t);
genericMethod.Invoke(null, null);
}
}
But at the last line of the code “genericMethod.Invoke(null,null)” throws an exception “Parameter count mismatch.”
Anyone can help?
The GetTableName method has one parameter but you are calling Invoke will a second argument of null indicating that no parameters should be passed. You need to pass your ObjectContext as the second parameter: