Method may only be called on a Type for which Type.IsGenericParameter is true

entity framework method may only be called on a type for which type.is generic parameter is true
jil method may only be called on a type for which type isgenericparameter is true

I am getting this error on a routine which uses reflection to dump some object properties, something like the code below.

MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance) ;

foreach (MemberInfo m in members)
{
    PropertyInfo p = m as PropertyInfo;
    if (p != null)
    {
       object po = p.GetValue(obj, null);

       ...
    }
}

The actual error is "Exception has been thrown by the target of an invocation" with an inner exception of "Method may only be called on a Type for which Type.IsGenericParameter is true."

At this stage in the debugger obj appears as

  {Name = "SqlConnection" FullName = "System.Data.SqlClient.SqlConnection"} 

with the type System.RuntimeType

The method m is {System.Reflection.MethodBase DeclaringMethod}

Note that obj is of type System.RuntimeType and members contains 188 items whereas a simple typeof(System.Data.SqlClient.SqlConnection).GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) only returns 65.

I tried checking isGenericParameter on both obj and p.PropertyType, but this seems to be false for most properties including those where p.GetValue works.

So what exactly is a "Type for which Type.IsGenericParameter is true" and more importantly how do I avoid this error without a try/catch?

Firstly, you've made in incorrect assumption, that is, you've assumed that members has returned the members of an instance of System.Data.SqlClient.SqlConnection, which it has not. What has been returned are the members of an instance of System.Type.

From the MSDN documentation for DeclaringType:

Getting the DeclaringMethod property on a type whose IsGenericParameter property is false throws an InvalidOperationException.

So... it's understandable that an InvalidOperationException is being thrown, since naturally, you're not dealing with an open generic type here. See Marc Gravells answer for an explanation of open generic types.

Invalid operation exception while executing Type.GetType, Operation exception with an inner exception telling me that "Method may only be called on a Type for which Type.IsGenericParameter is true. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Method may only be called on a Type for which Type.IsGenericParameter is true after upgrading ServiceStack app to NetCore 3

So what exactly is a "Type for which Type.IsGenericParameter is true"

That means it is a generic type argument in an open generic type - i.e. where we haven't picked a T yet; for example:

// true
bool isGenParam = typeof(List<>).GetGenericArguments()[0].IsGenericParameter;

// false (T is System.Int32)
bool isGenParam = typeof(List<int>).GetGenericArguments()[0].IsGenericParameter;

So; have you got some open generics hanging around? Perhaps if you can give an example of where you got your obj from?

Error Message, After much probing I found this particular message - "Method may only be called on a Type for which Type.IsGenericParameter is true." - Is this  How did you get the message "Method may only be called on a Type for which Type.IsGenericParameter is true"? Was an exception thrown, and if there was, can you post the call stack? ‹ Previous Thread | Next Thread ›

All the clues are in there. The type of the obj is the Type class itself (or rather the strange RuntimeType derivative).

At the point of failure you loop has arrived the Type class property called DeclaringMethod. However the type that this instance of the Type class is describing is System.Data.SqlClient.SqlConnection which is not a Generic Type of a method.

Hence attempting to invoke the get on DeclaringMethod results in the exception.

The key is you are examining the type of the class Type. Its a bit circular but think of this:-

SqlConnection s = new SqlConnection();
Type t = s.GetType()
Type ouch = t.GetType()

What is class ouch describing?

SimpleJson Bug (Method may only be called on a Type for which , IsGenericParameter is true.) · Issue #2052 · elastic/elasticsearch-net · GitHub  Property accessor 'DeclaringMethod' on object 'System.RuntimeType' threw the following exception:'Method may only be called on a Type for which Type.IsGenericParameter is true.' StackTrace message like below

How do I avoid this error without a try/catch?

You almost certainly can't. When you call p.GetValue, you're calling the getter on that property, which could throw any kind of exception. For example, SqlConnection.ServerVersion will throw an exception if the connection is closed, and you have to handle that.

Where are these extra members coming from?

Your obj already contains the RuntimeType object representing SqlConnection, rather than an instance of SqlConnection. obj.GetMembers() would return the 65 members of the SqlConnection class, but by calling GetType() again, you get the 188 members of RuntimeType.

What is IsGenericParameter?

Instead of representing a class, you can have an instance of RuntimeType that represents a generic parameter to a class or method (The T and TOutput in List<T>.ConvertAll<TOutput>. In this case, DeclaringMethod on the object representing TOutput would let you get a MethodInfo object representing the ConvertAll<> method. However, when the RuntimeType represents a class, the idea of a declaring method makes no sense. That's why reading the property causes the exception that you saw.

Error: Method may only be called on a Type for which Type , IsGenericParameter is true." This is how I called it. string test = ObjectDumper.​Dump(directoryEntry);. Any advice? wcf method may only be called on a type for which type isgenericparameter is true (3) So what exactly is a "Type for which Type.IsGenericParameter is true" That means it is a generic type argument in an open generic type - i.e. where we haven't picked a T yet; for example:

My problem was solved by deleting repetitive fields and tables in my model and commented defining query and deleted store: in Model.edmx XML File.

T560779, Why might the "Method may only be called on a Type for which Type.​IsGenericParameter is true" error occur when the GridView extension is  Error: Method may only be called on a Type for which Type.IsGenericParameter is true. #1 syaifulnizamyahya opened this issue Nov 14, 2017 · 2 comments Labels

Method may only be called on a Type for which Type , Method may only be called on a Type for which Type.IsGenericParameter is true. Question. I am getting this error on a routine which uses reflection to dump  For a generic type or method definition, the IsGenericParameter property returns true for every element of the resulting array. For a closed constructed type or method, the IsGenericParameter property returns false for every element of the array returned by the GetGenericArguments method.

Getting : Object reference not set to an instance of an object , + base {"Method may only be called on a Type for which Type.​IsGenericParameter is true."} System.SystemException {System. Type gatewayPluginType = Type.GetType(gatewayPluginConfiguration.Type, true); This operation doesn't throw an exception but when i look into the gatewayPluginType i can see that the DeclaringMethod throws an Invalid Operation exception with an inner exception telling me that "Method may only be called on a Type for which Type.IsGenericParameter

NET Development >>Type.GetType problem., Exception Message: Method may only be called on a Type for which Type.​IsGenericParameter is true. Any ideas I have tried to search about this, but I couldn't  I am using Code First to an Existing Database. A subset of the model is setup as follows: A Group has one-to-many Locations. A Location must have only one Group public class

Comments
  • I thinking I am beginning to see the light. It isn't saying that p.GetValue can 'only called on a Type for which Type.IsGenericParameter is true' but rather that the underlying property represented by p, which in this case is DeclaringMethod, can only be called is Type.IsGenericParameter is true.
  • Exactly - that's what "Exception has been thrown by the target of an invocation" means, the "target of an invocation" in this case is the DeclaringMethod property getter, and you'd get the same IsGenericParameter exception by reading obj.DeclaringMethod directly.
  • I've marked this answer as my accepted answer as it was the most helpful, but in truth most of the other answers helped as well. So thanks to everyone.
  • The OP does state the type of in obj its System.RuntimeType
  • From what I can see, the problem isn't so much the existence of open generics, but the existence of normal types :)
  • I start with a exception (in this case a SqlException) which is passed to my dump routine. The dump routine is recursive, so the dump is called on all the properties and fields in the exception. There are a few generics in my code, but no open generics. What is confusing me is the error seems to say that p.GetValue only works if GenericParameter is true which is plainly not true.
  • @sgmoore: What do you thing GetValue is doing? Its invoking the get operation of property described by the PropertyInfo on the obj. The PropertyInfo describes the DeclaringMethod of a Type object, hence GetValue calls the get operation of DeclaringMethod which is then failing because obj is not a Generic Type of a Method.
  • @anthonywjones. I understand that (now). It seems obvious in hindsight, but it wasn't until I read Eric Smith's answer that it clicked. My only excuse is that the word Method in the error message threw me. (And yes, I understand the properties are effectively a get and set method, but I don't think of properties as methods and assumed the method it was talking about must be the GetValue method)