summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs
blob: cfd22f302f48f4aba629bbb123ea2d288c3c1542 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Xamarin.Forms.Xaml;
using Xamarin.Forms.Xaml.Internals;

namespace Xamarin.Forms.Build.Tasks
{
	static class NodeILExtensions
	{
		public static IEnumerable<Instruction> PushConvertedValue(this ValueNode node, ILContext context,
			TypeReference targetTypeRef, IEnumerable<ICustomAttributeProvider> attributeProviders,
			IEnumerable<Instruction> pushServiceProvider, bool boxValueTypes, bool unboxValueTypes)
		{
			TypeReference typeConverter = null;
			foreach (var attributeProvider in attributeProviders)
			{
				CustomAttribute typeConverterAttribute;
				if (
					(typeConverterAttribute =
						attributeProvider.CustomAttributes.FirstOrDefault(
							cad => TypeConverterAttribute.TypeConvertersType.Contains(cad.AttributeType.FullName))) != null)
				{
					typeConverter = typeConverterAttribute.ConstructorArguments[0].Value as TypeReference;
					break;
				}
			}
			return node.PushConvertedValue(context, targetTypeRef, typeConverter, pushServiceProvider, boxValueTypes,
				unboxValueTypes);
		}

		public static IEnumerable<Instruction> PushConvertedValue(this ValueNode node, ILContext context, FieldReference bpRef,
			IEnumerable<Instruction> pushServiceProvider, bool boxValueTypes, bool unboxValueTypes)
		{
			var module = context.Body.Method.Module;
			var targetTypeRef = bpRef.GetBindablePropertyType(node, module);
			var typeConverter = bpRef.GetBindablePropertyTypeConverter(module);

			return node.PushConvertedValue(context, targetTypeRef, typeConverter, pushServiceProvider, boxValueTypes,
				unboxValueTypes);
		}

		public static IEnumerable<Instruction> PushConvertedValue(this ValueNode node, ILContext context,
			TypeReference targetTypeRef, TypeReference typeConverter, IEnumerable<Instruction> pushServiceProvider,
			bool boxValueTypes, bool unboxValueTypes)
		{
			var module = context.Body.Method.Module;
			var str = (string)node.Value;

			//If the TypeConverter has a ProvideCompiledAttribute that can be resolved, shortcut this
			var compiledConverterName = typeConverter?.GetCustomAttribute (module.Import(typeof(ProvideCompiledAttribute)))?.ConstructorArguments?.First().Value as string;
			Type compiledConverterType;
			if (compiledConverterName != null && (compiledConverterType = Type.GetType (compiledConverterName)) != null) {
				var compiledConverter = Activator.CreateInstance (compiledConverterType);
				var converter = typeof(ICompiledTypeConverter).GetMethods ().FirstOrDefault (md => md.Name == "ConvertFromString");
				var instructions = (IEnumerable<Instruction>)converter.Invoke (compiledConverter, new object[] {
					node.Value as string, context.Body.Method.Module, node as BaseNode});
				foreach (var i in instructions)
					yield return i;
				if (targetTypeRef.IsValueType && boxValueTypes)
					yield return Instruction.Create (OpCodes.Box, module.Import (targetTypeRef));
				yield break;
			}

			//If there's a [TypeConverter], use it
			if (typeConverter != null)
			{
				var isExtendedConverter = typeConverter.ImplementsInterface(module.Import(typeof (IExtendedTypeConverter)));
				var typeConverterCtor = typeConverter.Resolve().Methods.Single(md => md.IsConstructor && md.Parameters.Count == 0);
				var typeConverterCtorRef = module.Import(typeConverterCtor);
				var convertFromInvariantStringDefinition = isExtendedConverter
					? module.Import(typeof (IExtendedTypeConverter))
						.Resolve()
						.Methods.FirstOrDefault(md => md.Name == "ConvertFromInvariantString" && md.Parameters.Count == 2)
					: typeConverter.Resolve()
						.AllMethods()
						.FirstOrDefault(md => md.Name == "ConvertFromInvariantString" && md.Parameters.Count == 1);
				var convertFromInvariantStringReference = module.Import(convertFromInvariantStringDefinition);

				yield return Instruction.Create(OpCodes.Newobj, typeConverterCtorRef);
				yield return Instruction.Create(OpCodes.Ldstr, node.Value as string);

				if (isExtendedConverter)
				{
					foreach (var instruction in pushServiceProvider)
						yield return instruction;
				}

				yield return Instruction.Create(OpCodes.Callvirt, convertFromInvariantStringReference);

				if (targetTypeRef.IsValueType && unboxValueTypes)
					yield return Instruction.Create(OpCodes.Unbox_Any, module.Import(targetTypeRef));

				//ConvertFrom returns an object, no need to Box
				yield break;
			}
			var originalTypeRef = targetTypeRef;
			var isNullable = false;
			MethodReference nullableCtor = null;
			if (targetTypeRef.Resolve().FullName == "System.Nullable`1")
			{
				var nullableTypeRef = targetTypeRef;
				targetTypeRef = ((GenericInstanceType)targetTypeRef).GenericArguments[0];
				isNullable = true;
				nullableCtor = originalTypeRef.GetMethods(md => md.IsConstructor && md.Parameters.Count == 1, module).Single().Item1;
				nullableCtor = nullableCtor.ResolveGenericParameters(nullableTypeRef, module);
			}

			var implicitOperator = module.TypeSystem.String.GetImplicitOperatorTo(targetTypeRef, module);

			//Obvious Built-in conversions
			if (targetTypeRef.Resolve().BaseType != null && targetTypeRef.Resolve().BaseType.FullName == "System.Enum")
				yield return PushParsedEnum(targetTypeRef, str, node);
			else if (targetTypeRef.FullName == "System.Char")
				yield return Instruction.Create(OpCodes.Ldc_I4, Char.Parse(str));
			else if (targetTypeRef.FullName == "System.SByte")
				yield return Instruction.Create(OpCodes.Ldc_I4, SByte.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.Int16")
				yield return Instruction.Create(OpCodes.Ldc_I4, Int16.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.Int32")
				yield return Instruction.Create(OpCodes.Ldc_I4, Int32.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.Int64")
				yield return Instruction.Create(OpCodes.Ldc_I8, Int64.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.Byte")
				yield return Instruction.Create(OpCodes.Ldc_I4, Byte.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.UInt16")
				yield return Instruction.Create(OpCodes.Ldc_I4, unchecked((int)UInt16.Parse(str, CultureInfo.InvariantCulture)));
			else if (targetTypeRef.FullName == "System.UInt32")
				yield return Instruction.Create(OpCodes.Ldc_I4, UInt32.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.UInt64")
				yield return Instruction.Create(OpCodes.Ldc_I8, unchecked((long)UInt64.Parse(str, CultureInfo.InvariantCulture)));
			else if (targetTypeRef.FullName == "System.Single")
				yield return Instruction.Create(OpCodes.Ldc_R4, Single.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.Double")
				yield return Instruction.Create(OpCodes.Ldc_R8, Double.Parse(str, CultureInfo.InvariantCulture));
			else if (targetTypeRef.FullName == "System.Boolean") {
				if (Boolean.Parse(str))
					yield return Instruction.Create(OpCodes.Ldc_I4_1);
				else
					yield return Instruction.Create(OpCodes.Ldc_I4_0);
			} else if (targetTypeRef.FullName == "System.TimeSpan") {
				var ts = TimeSpan.Parse(str, CultureInfo.InvariantCulture);
				var ticks = ts.Ticks;
				var timeSpanCtor =
					module.Import(typeof(TimeSpan))
						.Resolve()
						.Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 1);
				var timeSpanCtorRef = module.Import(timeSpanCtor);

				yield return Instruction.Create(OpCodes.Ldc_I8, ticks);
				yield return Instruction.Create(OpCodes.Newobj, timeSpanCtorRef);
			} else if (targetTypeRef.FullName == "System.DateTime") {
				var dt = DateTime.Parse(str, CultureInfo.InvariantCulture);
				var ticks = dt.Ticks;
				var dateTimeCtor =
					module.Import(typeof(DateTime))
						.Resolve()
						.Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 1);
				var dateTimeCtorRef = module.Import(dateTimeCtor);

				yield return Instruction.Create(OpCodes.Ldc_I8, ticks);
				yield return Instruction.Create(OpCodes.Newobj, dateTimeCtorRef);
			} else if (targetTypeRef.FullName == "System.String" && str.StartsWith("{}", StringComparison.Ordinal))
				yield return Instruction.Create(OpCodes.Ldstr, str.Substring(2));
			else if (targetTypeRef.FullName == "System.String")
				yield return Instruction.Create(OpCodes.Ldstr, str);
			else if (targetTypeRef.FullName == "System.Object")
				yield return Instruction.Create(OpCodes.Ldstr, str);
			else if (targetTypeRef.FullName == "System.Decimal") {
				decimal outdecimal;
				if (decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out outdecimal)) {
					var vardef = new VariableDefinition(context.Body.Method.Module.Import(typeof(decimal)));
					context.Body.Variables.Add(vardef);
					//Use an extra temp var so we can push the value to the stack, just like other cases
					//					IL_0003:  ldstr "adecimal"
					//					IL_0008:  ldc.i4.s 0x6f
					//					IL_000a:  call class [mscorlib]System.Globalization.CultureInfo class [mscorlib]System.Globalization.CultureInfo::get_InvariantCulture()
					//					IL_000f:  ldloca.s 0
					//					IL_0011:  call bool valuetype [mscorlib]System.Decimal::TryParse(string, valuetype [mscorlib]System.Globalization.NumberStyles, class [mscorlib]System.IFormatProvider, [out] valuetype [mscorlib]System.Decimal&)
					//					IL_0016:  pop
					yield return Instruction.Create(OpCodes.Ldstr, str);
					yield return Instruction.Create(OpCodes.Ldc_I4, 0x6f); //NumberStyles.Number
					var getInvariantInfo =
						context.Body.Method.Module.Import(typeof(CultureInfo))
							.Resolve()
							.Properties.FirstOrDefault(pd => pd.Name == "InvariantCulture")
							.GetMethod;
					var getInvariant = context.Body.Method.Module.Import(getInvariantInfo);
					yield return Instruction.Create(OpCodes.Call, getInvariant);
					yield return Instruction.Create(OpCodes.Ldloca, vardef);
					var tryParseInfo =
						context.Body.Method.Module.Import(typeof(decimal))
							.Resolve()
							.Methods.FirstOrDefault(md => md.Name == "TryParse" && md.Parameters.Count == 4);
					var tryParse = context.Body.Method.Module.Import(tryParseInfo);
					yield return Instruction.Create(OpCodes.Call, tryParse);
					yield return Instruction.Create(OpCodes.Pop);
					yield return Instruction.Create(OpCodes.Ldloc, vardef);
				} else {
					yield return Instruction.Create(OpCodes.Ldc_I4_0);
					var decimalctorinfo =
						context.Body.Method.Module.Import(typeof(decimal))
							.Resolve()
							.Methods.FirstOrDefault(
								md => md.IsConstructor && md.Parameters.Count == 1 && md.Parameters[0].ParameterType.FullName == "System.Int32");
					var decimalctor = context.Body.Method.Module.Import(decimalctorinfo);
					yield return Instruction.Create(OpCodes.Newobj, decimalctor);
				}
			} else if (implicitOperator != null) {
				yield return Instruction.Create(OpCodes.Ldstr, node.Value as string);
				yield return Instruction.Create(OpCodes.Call, module.Import(implicitOperator));
			} else
				yield return Instruction.Create(OpCodes.Ldnull);

			if (isNullable)
				yield return Instruction.Create(OpCodes.Newobj, module.Import(nullableCtor));
			if (originalTypeRef.IsValueType && boxValueTypes)
				yield return Instruction.Create(OpCodes.Box, module.Import(originalTypeRef));
		}

		static Instruction PushParsedEnum(TypeReference enumRef, string value, IXmlLineInfo lineInfo)
		{
			var enumDef = enumRef.Resolve();
			if (!enumDef.IsEnum)
				throw new InvalidOperationException();

			// The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
			// https://msdn.microsoft.com/en-us/library/sbbt4032.aspx
			byte b = 0; sbyte sb = 0; short s = 0; ushort us = 0;
			int i = 0; uint ui = 0; long l = 0; ulong ul = 0;
			bool found = false;
			TypeReference typeRef = null;

			foreach (var field in enumDef.Fields)
				if (field.Name == "value__")
					typeRef = field.FieldType;

			if (typeRef == null)
				throw new ArgumentException();

			foreach (var v in value.Split(',')) {
				foreach (var field in enumDef.Fields) {
					if (field.Name == "value__")
						continue;
					if (field.Name == v.Trim()) {
						switch (typeRef.FullName) {
						case "System.Byte":
							b |= (byte)field.Constant;
							break;
						case "System.SByte":
							if (found)
								throw new XamlParseException($"Multi-valued enums are not valid on sbyte enum types", lineInfo);
							sb = (sbyte)field.Constant;
							break;
						case "System.Int16":
							s |= (short)field.Constant;
							break;
						case "System.UInt16":
							us |= (ushort)field.Constant;
							break;
						case "System.Int32":
							i |= (int)field.Constant;
							break;
						case "System.UInt32":
							ui |= (uint)field.Constant;
							break;
						case "System.Int64":
							l |= (long)field.Constant;
							break;
						case "System.UInt64":
							ul |= (ulong)field.Constant;
							break;
						}
						found = true;
					}
				}
			}

			if (!found)
				throw new XamlParseException($"Enum value not found for {value}", lineInfo);
				
			switch (typeRef.FullName) {
			case "System.Byte":
				return Instruction.Create(OpCodes.Ldc_I4, (int)b);
			case "System.SByte":
				return Instruction.Create(OpCodes.Ldc_I4, (int)sb);
			case "System.Int16":
				return Instruction.Create(OpCodes.Ldc_I4, (int)s);
			case "System.UInt16":
				return Instruction.Create(OpCodes.Ldc_I4, (int)us);
			case "System.Int32":
				return Instruction.Create(OpCodes.Ldc_I4, (int)i);
			case "System.UInt32":
				return Instruction.Create(OpCodes.Ldc_I4, (uint)ui);
			case "System.Int64":
				return Instruction.Create(OpCodes.Ldc_I4, (long)l);
			case "System.UInt64":
				return Instruction.Create(OpCodes.Ldc_I4, (ulong)ul);
			default:
				throw new XamlParseException($"Enum value not found for {value}", lineInfo);
			}
		}

		public static IEnumerable<Instruction> PushXmlLineInfo(this INode node, ILContext context)
		{
			var module = context.Body.Method.Module;

			var xmlLineInfo = node as IXmlLineInfo;
			if (xmlLineInfo == null)
			{
				yield return Instruction.Create(OpCodes.Ldnull);
				yield break;
			}
			MethodReference ctor;
			if (xmlLineInfo.HasLineInfo())
			{
				yield return Instruction.Create(OpCodes.Ldc_I4, xmlLineInfo.LineNumber);
				yield return Instruction.Create(OpCodes.Ldc_I4, xmlLineInfo.LinePosition);
				ctor = module.Import(typeof (XmlLineInfo).GetConstructor(new[] { typeof (int), typeof (int) }));
			}
			else
				ctor = module.Import(typeof (XmlLineInfo).GetConstructor(new Type[] { }));
			yield return Instruction.Create(OpCodes.Newobj, ctor);
		}

		public static IEnumerable<Instruction> PushParentObjectsArray(this INode node, ILContext context)
		{
			var module = context.Body.Method.Module;

			var nodes = new List<IElementNode>();
			INode n = node.Parent;
			while (n != null)
			{
				var en = n as IElementNode;
				if (en != null && context.Variables.ContainsKey(en))
					nodes.Add(en);
				n = n.Parent;
			}

			if (nodes.Count == 0 && context.ParentContextValues == null)
			{
				yield return Instruction.Create(OpCodes.Ldnull);
				yield break;
			}

			if (nodes.Count == 0)
			{
				yield return Instruction.Create(OpCodes.Ldarg_0);
				yield return Instruction.Create(OpCodes.Ldfld, context.ParentContextValues);
				yield break;
			}

			//Compute parent object length
			if (context.ParentContextValues != null)
			{
				yield return Instruction.Create(OpCodes.Ldarg_0);
				yield return Instruction.Create(OpCodes.Ldfld, context.ParentContextValues);
				yield return Instruction.Create(OpCodes.Ldlen);
				yield return Instruction.Create(OpCodes.Conv_I4);
			}
			else
				yield return Instruction.Create(OpCodes.Ldc_I4_0);
			var parentObjectLength = new VariableDefinition(module.TypeSystem.Int32);
			context.Body.Variables.Add(parentObjectLength);
			yield return Instruction.Create(OpCodes.Stloc, parentObjectLength);

			//Create the final array
			yield return Instruction.Create(OpCodes.Ldloc, parentObjectLength);
			yield return Instruction.Create(OpCodes.Ldc_I4, nodes.Count);
			yield return Instruction.Create(OpCodes.Add);
			yield return Instruction.Create(OpCodes.Newarr, module.TypeSystem.Object);
			var finalArray = new VariableDefinition(module.Import(typeof (object[])));
			context.Body.Variables.Add(finalArray);
			yield return Instruction.Create(OpCodes.Stloc, finalArray);

			//Copy original array to final
			if (context.ParentContextValues != null)
			{
				yield return Instruction.Create(OpCodes.Ldarg_0);
				yield return Instruction.Create(OpCodes.Ldfld, context.ParentContextValues); //sourceArray
				yield return Instruction.Create(OpCodes.Ldc_I4_0); //sourceIndex
				yield return Instruction.Create(OpCodes.Ldloc, finalArray); //destinationArray
				yield return Instruction.Create(OpCodes.Ldc_I4, nodes.Count); //destinationIndex
				yield return Instruction.Create(OpCodes.Ldloc, parentObjectLength); //length
				var arrayCopy =
					module.Import(typeof (Array))
						.Resolve()
						.Methods.First(
							md =>
								md.Name == "Copy" && md.Parameters.Count == 5 &&
								md.Parameters[1].ParameterType.FullName == module.TypeSystem.Int32.FullName);
				yield return Instruction.Create(OpCodes.Call, module.Import(arrayCopy));
			}

			//Add nodes to array
			yield return Instruction.Create(OpCodes.Ldloc, finalArray);
			if (nodes.Count > 0)
			{
				for (var i = 0; i < nodes.Count; i++)
				{
					var en = nodes[i];
					yield return Instruction.Create(OpCodes.Dup);
					yield return Instruction.Create(OpCodes.Ldc_I4, i);
					yield return Instruction.Create(OpCodes.Ldloc, context.Variables[en]);
					if (context.Variables[en].VariableType.IsValueType)
						yield return Instruction.Create(OpCodes.Box, module.Import(context.Variables[en].VariableType));
					yield return Instruction.Create(OpCodes.Stelem_Ref);
				}
			}
		}

		static IEnumerable<Instruction> PushTargetProperty(FieldReference bpRef, PropertyReference propertyRef, TypeReference declaringTypeReference, ModuleDefinition module)
		{
			if (bpRef != null) {
				yield return Instruction.Create(OpCodes.Ldsfld, bpRef);
				yield break;
			}
			if (propertyRef != null) {
//				IL_0000:  ldtoken [mscorlib]System.String
//				IL_0005:  call class [mscorlib]System.Type class [mscorlib] System.Type::GetTypeFromHandle(valuetype [mscorlib] System.RuntimeTypeHandle)
//				IL_000a:  ldstr "Foo"
//				IL_000f:  callvirt instance class [mscorlib] System.Reflection.PropertyInfo class [mscorlib] System.Type::GetProperty(string)
				var getTypeFromHandle = module.Import(typeof(Type).GetMethod("GetTypeFromHandle", new [] { typeof(RuntimeTypeHandle) }));
				var getPropertyInfo = module.Import(typeof(Type).GetMethod("GetProperty", new [] { typeof(string) }));
				yield return Instruction.Create(OpCodes.Ldtoken, module.Import(declaringTypeReference ?? propertyRef.DeclaringType));
				yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle));
				yield return Instruction.Create(OpCodes.Ldstr, propertyRef.Name);
				yield return Instruction.Create(OpCodes.Callvirt, module.Import(getPropertyInfo));
				yield break;
			}
			yield return Instruction.Create(OpCodes.Ldnull);
			yield break;
		}

		public static IEnumerable<Instruction> PushServiceProvider(this INode node, ILContext context, FieldReference bpRef = null, PropertyReference propertyRef = null, TypeReference declaringTypeReference = null)
		{
			var module = context.Body.Method.Module;

#if NOSERVICEPROVIDER
			yield return Instruction.Create (OpCodes.Ldnull);
			yield break;
#endif

			var ctorinfo = typeof (XamlServiceProvider).GetConstructor(new Type[] { });
			var ctor = module.Import(ctorinfo);

			var addServiceInfo = typeof (XamlServiceProvider).GetMethod("Add", new[] { typeof (Type), typeof (object) });
			var addService = module.Import(addServiceInfo);

			var getTypeFromHandle =
				module.Import(typeof (Type).GetMethod("GetTypeFromHandle", new[] { typeof (RuntimeTypeHandle) }));
			var getAssembly = module.Import(typeof (Type).GetProperty("Assembly").GetMethod);

			yield return Instruction.Create(OpCodes.Newobj, ctor);

			//Add a SimpleValueTargetProvider
			var pushParentIl = node.PushParentObjectsArray(context).ToList();
			if (pushParentIl[pushParentIl.Count - 1].OpCode != OpCodes.Ldnull)
			{
				yield return Instruction.Create(OpCodes.Dup); //Keep the serviceProvider on the stack
				yield return Instruction.Create(OpCodes.Ldtoken, module.Import(typeof (IProvideValueTarget)));
				yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle));

				foreach (var instruction in pushParentIl)
					yield return instruction;

				foreach (var instruction in PushTargetProperty(bpRef, propertyRef, declaringTypeReference, module))
					yield return instruction;

				var targetProviderCtor =
					module.Import(typeof (SimpleValueTargetProvider).GetConstructor(new[] { typeof (object[]), typeof(object) }));
				yield return Instruction.Create(OpCodes.Newobj, targetProviderCtor);
				yield return Instruction.Create(OpCodes.Callvirt, addService);
			}

			//Add a NamescopeProvider
			if (context.Scopes.ContainsKey(node))
			{
				yield return Instruction.Create(OpCodes.Dup); //Dupicate the serviceProvider
				yield return Instruction.Create(OpCodes.Ldtoken, module.Import(typeof (INameScopeProvider)));
				yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle));
				var namescopeProviderCtor = module.Import(typeof (NameScopeProvider).GetConstructor(new Type[] { }));
				yield return Instruction.Create(OpCodes.Newobj, namescopeProviderCtor);
				yield return Instruction.Create(OpCodes.Dup); //Duplicate the namescopeProvider
				var setNamescope = module.Import(typeof (NameScopeProvider).GetProperty("NameScope").GetSetMethod());

				yield return Instruction.Create(OpCodes.Ldloc, context.Scopes[node].Item1);
				yield return Instruction.Create(OpCodes.Callvirt, setNamescope);
				yield return Instruction.Create(OpCodes.Callvirt, addService);
			}

			//Add a XamlTypeResolver
			if (node.NamespaceResolver != null)
			{
				yield return Instruction.Create(OpCodes.Dup); //Dupicate the serviceProvider
				yield return Instruction.Create(OpCodes.Ldtoken, module.Import(typeof (IXamlTypeResolver)));
				yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle));
				var xmlNamespaceResolverCtor = module.Import(typeof (XmlNamespaceResolver).GetConstructor(new Type[] { }));
				var addNamespace = module.Import(typeof (XmlNamespaceResolver).GetMethod("Add"));
				yield return Instruction.Create(OpCodes.Newobj, xmlNamespaceResolverCtor);
				foreach (var kvp in node.NamespaceResolver.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml))
				{
					yield return Instruction.Create(OpCodes.Dup); //dup the resolver
					yield return Instruction.Create(OpCodes.Ldstr, kvp.Key);
					yield return Instruction.Create(OpCodes.Ldstr, kvp.Value);
					yield return Instruction.Create(OpCodes.Callvirt, addNamespace);
				}
				yield return Instruction.Create(OpCodes.Ldtoken, context.Body.Method.DeclaringType);
				yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle));
				yield return Instruction.Create(OpCodes.Callvirt, getAssembly);
				var xtr = module.Import(typeof (XamlTypeResolver)).Resolve();
				var xamlTypeResolverCtor = module.Import(xtr.Methods.First(md => md.IsConstructor && md.Parameters.Count == 2));
				yield return Instruction.Create(OpCodes.Newobj, xamlTypeResolverCtor);
				yield return Instruction.Create(OpCodes.Callvirt, addService);
			}

			if (node is IXmlLineInfo)
			{
				yield return Instruction.Create(OpCodes.Dup); //Dupicate the serviceProvider
				yield return Instruction.Create(OpCodes.Ldtoken, module.Import(typeof (IXmlLineInfoProvider)));
				yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle));

				foreach (var instruction in node.PushXmlLineInfo(context))
					yield return instruction;

				var lip = module.Import(typeof (XmlLineInfoProvider)).Resolve();
				var lineInfoProviderCtor = module.Import(lip.Methods.First(md => md.IsConstructor && md.Parameters.Count == 1));
				yield return Instruction.Create(OpCodes.Newobj, lineInfoProviderCtor);
				yield return Instruction.Create(OpCodes.Callvirt, addService);
			}
		}
	}
}