PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Mono.Cecil/Test/Mono.Cecil.Tests/Extensions.cs

http://github.com/icsharpcode/ILSpy
C# | 92 lines | 73 code | 19 blank | 0 comment | 6 complexity | c2d9d8db80f00fc3a71a1395154efc51 MD5 | raw file
 1using System;
 2using System.Linq;
 3using SR = System.Reflection;
 4
 5using Mono.Cecil;
 6
 7namespace Mono.Cecil.Tests {
 8
 9	public static class Extensions {
10
11		public static MethodDefinition GetMethod (this TypeDefinition self, string name)
12		{
13			return self.Methods.Where (m => m.Name == name).First ();
14		}
15
16		public static FieldDefinition GetField (this TypeDefinition self, string name)
17		{
18			return self.Fields.Where (f => f.Name == name).First ();
19		}
20
21		public static TypeDefinition ToDefinition (this Type self)
22		{
23			var module = ModuleDefinition.ReadModule (self.Module.FullyQualifiedName);
24			return (TypeDefinition) module.LookupToken (self.MetadataToken);
25		}
26
27		public static MethodDefinition ToDefinition (this SR.MethodBase method)
28		{
29			var declaring_type = method.DeclaringType.ToDefinition ();
30			return (MethodDefinition) declaring_type.Module.LookupToken (method.MetadataToken);
31		}
32
33		public static FieldDefinition ToDefinition (this SR.FieldInfo field)
34		{
35			var declaring_type = field.DeclaringType.ToDefinition ();
36			return (FieldDefinition) declaring_type.Module.LookupToken (field.MetadataToken);
37		}
38
39		public static TypeReference MakeGenericType (this TypeReference self, params TypeReference [] arguments)
40		{
41			if (self.GenericParameters.Count != arguments.Length)
42				throw new ArgumentException ();
43
44			var instance = new GenericInstanceType (self);
45			foreach (var argument in arguments)
46				instance.GenericArguments.Add (argument);
47
48			return instance;
49		}
50
51		public static MethodReference MakeGenericMethod (this MethodReference self, params TypeReference [] arguments)
52		{
53			if (self.GenericParameters.Count != arguments.Length)
54				throw new ArgumentException ();
55
56			var instance = new GenericInstanceMethod (self);
57			foreach (var argument in arguments)
58				instance.GenericArguments.Add (argument);
59
60			return instance;
61		}
62
63		public static MethodReference MakeGeneric (this MethodReference self, params TypeReference [] arguments)
64		{
65			var reference = new MethodReference {
66				Name = self.Name,
67				DeclaringType = self.DeclaringType.MakeGenericType (arguments),
68				HasThis = self.HasThis,
69				ExplicitThis = self.ExplicitThis,
70				ReturnType = self.ReturnType,
71				CallingConvention = self.CallingConvention,
72			};
73
74			foreach (var parameter in self.Parameters)
75				reference.Parameters.Add (new ParameterDefinition (parameter.ParameterType));
76
77			foreach (var generic_parameter in self.GenericParameters)
78				reference.GenericParameters.Add (new GenericParameter (generic_parameter.Name, reference));
79
80			return reference;
81		}
82
83		public static FieldReference MakeGeneric (this FieldReference self, params TypeReference [] arguments)
84		{
85			return new FieldReference {
86				Name = self.Name,
87				DeclaringType = self.DeclaringType.MakeGenericType (arguments),
88				FieldType = self.FieldType,
89			};
90		}
91	}
92}