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