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

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

提交回复
热议问题