문제

I have a method which gets a parameter such as:

public void Foo(ref Action<string> bar);

Using Cecil to enumerate the parameters yields a ByReferenceType. Calling GetElementType() in an attempt to dereference the parameter returns a TypeReference with fullname:

System.Action`1

Somehow it has lost the generic parameters, and is no longer a GenericInstanceType.

How can I properly dereference the byref parameter, and get to the actual generic instance type?

도움이 되었습니까?

해결책

You can dive into the TypeSpec using this (you can of course make it shorter when you know what you're after):

ParameterDefinition parameter = ...;
ByReferenceType byref = (ByReferenceType) parameter.ParameterType;
GenericInstanceType action_string = (GenericInstanceType) byref.ElementType;
TypeReference action = action_string.ElementType;
TypeReference str = action_string.GenericArguments [0];

The GetElementType method returns the original element type from which the TypeSpec is constructed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top