Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tomspilman
Created January 30, 2015 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomspilman/99ddac3b60f72814c6e5 to your computer and use it in GitHub Desktop.
Save tomspilman/99ddac3b60f72814c6e5 to your computer and use it in GitHub Desktop.
Resolving Generic Types In Mono.Cecil
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