我有一个获取参数的方法,例如:
public void Foo(ref Action<string> bar);
使用 Cecil 枚举参数会产生一个 ByReferenceType。调用 GetElementType() 以尝试取消对参数的引用会返回具有全名的 TypeReference:
System.Action`1
它以某种方式丢失了通用参数,不再是 GenericInstanceType。
我怎样才能正确取消对 byref 参数的引用,并获得实际的通用实例类型?
最佳答案
您可以使用它深入研究 TypeSpec(当然,当您知道自己在追求什么时,您可以缩短它):
ParameterDefinition parameter = ...;
ByReferenceType byref = (ByReferenceType) parameter.ParameterType;
GenericInstanceType action_string = (GenericInstanceType) byref.ElementType;
TypeReference action = action_string.ElementType;
TypeReference str = action_string.GenericArguments [0];
GetElementType 方法返回构造 TypeSpec 的原始元素类型。
关于c# - 使用 Mono.Cecil 从 ByReferenceType 获取通用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6016392/