Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top