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

后端 未结 5 1267
一整个雨季
一整个雨季 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: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?

提交回复
热议问题