Created
January 30, 2015 07:35
-
-
Save tomspilman/99ddac3b60f72814c6e5 to your computer and use it in GitHub Desktop.
Resolving Generic Types In Mono.Cecil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static TypeReference ResolveGenericParameter(this TypeReference type, TypeReference parent) | |
{ | |
var genericParent = parent as GenericInstanceType; | |
if (genericParent == null) | |
return type; | |
if (type.IsGenericParameter) | |
return genericParent.GenericArguments[(type as GenericParameter).Position]; | |
if (type.IsArray) | |
{ | |
var array = type as ArrayType; | |
array.ElementType.ResolveGenericParameter(parent); | |
return array; | |
} | |
if (!type.IsGenericInstance) | |
return type; | |
var inst = type as GenericInstanceType; | |
for (var i = 0; i < inst.GenericArguments.Count; i++) | |
{ | |
if (!inst.GenericArguments[i].IsGenericParameter) | |
continue; | |
var param = inst.GenericArguments[i] as GenericParameter; | |
inst.GenericArguments[i] = genericParent.GenericArguments[param.Position]; | |
} | |
return inst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment