blob: 73245840b8e6c6ccfbf6938194ea1bc4d596f3b8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections.Generic;
using System.Linq;
namespace Mono.Cecil {
public static class TypeDefinitionUtils {
public static TypeReference TypeDefinitionForGeneric (
Type genericType, AssemblyDefinition genericAssemblyDefinition,
Type paramterType, AssemblyDefinition parameterAssemblyDefinition)
{
var paramDefinition = TypeDefinitionUtils.TypeDefinitionFor (paramterType, parameterAssemblyDefinition);
var genericDefinition = TypeDefinitionUtils.TypeDefinitionFor (genericType, genericAssemblyDefinition);
var genericInstance = new GenericInstanceType (genericDefinition);
genericInstance.GenericArguments.Add (paramDefinition);
return genericInstance;
}
public static TypeDefinition TypeDefinitionFor (Type type, AssemblyDefinition assemblyDefinition)
{
var stack = new Stack<string> ();
var currentType = type;
while (currentType != null) {
stack.Push ( (currentType.DeclaringType == null ? currentType.Namespace + "." : "") + currentType.Name);
currentType = currentType.DeclaringType;
}
var typeDefinition = assemblyDefinition.MainModule.GetType (stack.Pop ());
if (typeDefinition == null)
return null;
while (stack.Count > 0) {
var name = stack.Pop ();
typeDefinition = typeDefinition.NestedTypes.Single (t => t.Name == name);
}
return typeDefinition;
}
}
}
|