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

后端 未结 5 1266
一整个雨季
一整个雨季 2020-12-30 01:30

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(         


        
相关标签:
5条回答
  • 2020-12-30 02:11

    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.

    0 讨论(0)
  • 2020-12-30 02:20

    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?

    0 讨论(0)
  • 2020-12-30 02:30

    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?

    0 讨论(0)
  • 2020-12-30 02:32

    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.

    0 讨论(0)
  • 2020-12-30 02:32

    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.

    0 讨论(0)
提交回复
热议问题