diff --git a/.nuspec/Xamarin.Forms.Debug.targets b/.nuspec/Xamarin.Forms.Debug.targets index ce1977138..6ef2ffe38 100644 --- a/.nuspec/Xamarin.Forms.Debug.targets +++ b/.nuspec/Xamarin.Forms.Debug.targets @@ -63,8 +63,9 @@ diff --git a/Xamarin.Forms.Build.Tasks/BindablePropertyReferenceExtensions.cs b/Xamarin.Forms.Build.Tasks/BindablePropertyReferenceExtensions.cs index 5f1826db5..0edc5f59d 100644 --- a/Xamarin.Forms.Build.Tasks/BindablePropertyReferenceExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/BindablePropertyReferenceExtensions.cs @@ -26,7 +26,7 @@ namespace Xamarin.Forms.Build.Tasks md.IsStatic && md.IsPublic && md.Parameters.Count == 1 && - md.Parameters[0].ParameterType.InheritsFromOrImplements(module.Import(typeof(BindableObject))), module).SingleOrDefault()?.Item1; + md.Parameters[0].ParameterType.InheritsFromOrImplements(module.ImportReference(typeof(BindableObject))), module).SingleOrDefault()?.Item1; if (getter == null) throw new XamlParseException($"Missing a public static Get{bpName} or a public instance property getter for the attached property \"{bpRef.DeclaringType}.{bpRef.Name}\"", iXmlLineInfo); @@ -43,7 +43,7 @@ namespace Xamarin.Forms.Build.Tasks md.IsStatic && md.IsPublic && md.Parameters.Count == 1 && - md.Parameters[0].ParameterType.InheritsFromOrImplements(module.Import(typeof(BindableObject))), module).SingleOrDefault()?.Item1; + md.Parameters[0].ParameterType.InheritsFromOrImplements(module.ImportReference(typeof(BindableObject))), module).SingleOrDefault()?.Item1; var attributes = new List(); if (property != null && property.HasCustomAttributes) diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/BindablePropertyConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/BindablePropertyConverter.cs index c78e51840..b46d22dc5 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/BindablePropertyConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/BindablePropertyConverter.cs @@ -84,8 +84,8 @@ namespace Xamarin.Forms.Core.XamlC TypeReference declaringTypeReference; FieldReference bpRef = typeRef.GetField(fd => fd.Name == $"{propertyName}Property" && fd.IsStatic && fd.IsPublic, out declaringTypeReference); if (bpRef != null) { - bpRef = module.Import(bpRef.ResolveGenericParameters(declaringTypeReference)); - bpRef.FieldType = module.Import(bpRef.FieldType); + bpRef = module.ImportReference(bpRef.ResolveGenericParameters(declaringTypeReference)); + bpRef.FieldType = module.ImportReference(bpRef.FieldType); } return bpRef; } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/BindingTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/BindingTypeConverter.cs index 74990a86d..8c8f0411a 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/BindingTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/BindingTypeConverter.cs @@ -17,8 +17,8 @@ namespace Xamarin.Forms.Core.XamlC if (IsNullOrEmpty(value)) throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Binding)}", node); - var bindingCtor = module.Import(typeof(Binding)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 6); - var bindingCtorRef = module.Import(bindingCtor); + var bindingCtor = module.ImportReference(typeof(Binding)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 6); + var bindingCtorRef = module.ImportReference(bindingCtor); yield return Instruction.Create(OpCodes.Ldstr, value); yield return Instruction.Create(OpCodes.Ldc_I4, (int)BindingMode.Default); diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/BoundsTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/BoundsTypeConverter.cs index 1cc64fc55..88550bf14 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/BoundsTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/BoundsTypeConverter.cs @@ -65,8 +65,8 @@ namespace Xamarin.Forms.Core.XamlC yield return Instruction.Create(OpCodes.Ldc_R8, w); yield return Instruction.Create(OpCodes.Ldc_R8, h); - var rectangleCtor = module.Import(typeof(Rectangle)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 4); - var rectangleCtorRef = module.Import(rectangleCtor); + var rectangleCtor = module.ImportReference(typeof(Rectangle)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 4); + var rectangleCtorRef = module.ImportReference(rectangleCtor); yield return Instruction.Create(OpCodes.Newobj, rectangleCtorRef); } } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ColorTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ColorTypeConverter.cs index 395279540..cfb9d0165 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/ColorTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ColorTypeConverter.cs @@ -25,10 +25,10 @@ namespace Xamarin.Forms.Core.XamlC yield return Instruction.Create(OpCodes.Ldc_R8, color.G); yield return Instruction.Create(OpCodes.Ldc_R8, color.B); yield return Instruction.Create(OpCodes.Ldc_R8, color.A); - var colorCtor = module.Import(typeof(Color)).Resolve().Methods.FirstOrDefault( + var colorCtor = module.ImportReference(typeof(Color)).Resolve().Methods.FirstOrDefault( md => md.IsConstructor && md.Parameters.Count == 4 && md.Parameters.All(p => p.ParameterType.FullName == "System.Double")); - var colorCtorRef = module.Import(colorCtor); + var colorCtorRef = module.ImportReference(colorCtor); yield return Instruction.Create(OpCodes.Newobj, colorCtorRef); yield break; } @@ -36,14 +36,14 @@ namespace Xamarin.Forms.Core.XamlC if (parts.Length == 1 || (parts.Length == 2 && parts [0] == "Color")) { var color = parts [parts.Length - 1]; - var field = module.Import(typeof(Color)).Resolve().Fields.SingleOrDefault(fd => fd.Name == color && fd.IsStatic); + var field = module.ImportReference(typeof(Color)).Resolve().Fields.SingleOrDefault(fd => fd.Name == color && fd.IsStatic); if (field != null) { - yield return Instruction.Create(OpCodes.Ldsfld, module.Import(field)); + yield return Instruction.Create(OpCodes.Ldsfld, module.ImportReference(field)); yield break; } - var propertyGetter = module.Import(typeof(Color)).Resolve().Properties.SingleOrDefault(pd => pd.Name == color && pd.GetMethod.IsStatic)?.GetMethod; + var propertyGetter = module.ImportReference(typeof(Color)).Resolve().Properties.SingleOrDefault(pd => pd.Name == color && pd.GetMethod.IsStatic)?.GetMethod; if (propertyGetter != null) { - yield return Instruction.Create(OpCodes.Call, module.Import(propertyGetter)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(propertyGetter)); yield break; } } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs index 18b1810cc..e166247da 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs @@ -20,8 +20,8 @@ namespace Xamarin.Forms.Core.XamlC yield return Instruction.Create(OpCodes.Ldc_R8, size); - var constantDef = module.Import(typeof(Constraint)).Resolve().Methods.FirstOrDefault(md => md.IsStatic && md.Name == "Constant"); - var constantRef = module.Import(constantDef); + var constantDef = module.ImportReference(typeof(Constraint)).Resolve().Methods.FirstOrDefault(md => md.IsStatic && md.Name == "Constant"); + var constantRef = module.ImportReference(constantDef); yield return Instruction.Create(OpCodes.Call, constantRef); } } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ICompiledTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ICompiledTypeConverter.cs index 5ed88d0a5..d594cb736 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/ICompiledTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ICompiledTypeConverter.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; using Mono.Cecil.Cil; using Mono.Cecil; -using System.Xml; +using Xamarin.Forms.Xaml; +using System; namespace Xamarin.Forms.Xaml { @@ -9,4 +10,18 @@ namespace Xamarin.Forms.Xaml { IEnumerable ConvertFromString(string value, ModuleDefinition module, BaseNode node); } +} + +namespace Xamarin.Forms.Core.XamlC +{ + //only used in unit tests to make sure the compiled InitializeComponent is invoked + class IsCompiledTypeConverter : ICompiledTypeConverter + { + public IEnumerable ConvertFromString(string value, ModuleDefinition module, BaseNode node) + { + if (value != "IsCompiled?") + throw new Exception(); + yield return Instruction.Create(OpCodes.Ldc_I4_1); + } + } } \ No newline at end of file diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/LayoutOptionsConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/LayoutOptionsConverter.cs index e252ed9d1..d7c4597f6 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/LayoutOptionsConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/LayoutOptionsConverter.cs @@ -23,9 +23,9 @@ namespace Xamarin.Forms.Core.XamlC if (parts.Length == 1 || (parts.Length == 2 && parts [0] == "LayoutOptions")) { var options = parts [parts.Length - 1]; - var field = module.Import(typeof(LayoutOptions)).Resolve().Fields.SingleOrDefault(fd => fd.Name == options && fd.IsStatic); + var field = module.ImportReference(typeof(LayoutOptions)).Resolve().Fields.SingleOrDefault(fd => fd.Name == options && fd.IsStatic); if (field != null) { - yield return Instruction.Create(OpCodes.Ldsfld, module.Import(field)); + yield return Instruction.Create(OpCodes.Ldsfld, module.ImportReference(field)); yield break; } } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ListStringTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ListStringTypeConverter.cs index 62d7d69db..3f1c05697 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/ListStringTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ListStringTypeConverter.cs @@ -20,13 +20,13 @@ namespace Xamarin.Forms.Core.XamlC } var parts = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList(); - var listCtor = module.Import(typeof(List<>)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 1 && md.Parameters[0].ParameterType.FullName == "System.Int32"); - var listCtorRef = module.Import(listCtor); - listCtorRef = module.Import(listCtorRef.ResolveGenericParameters(module.Import(typeof(List)), module)); + var listCtor = module.ImportReference(typeof(List<>)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 1 && md.Parameters[0].ParameterType.FullName == "System.Int32"); + var listCtorRef = module.ImportReference(listCtor); + listCtorRef = module.ImportReference(listCtorRef.ResolveGenericParameters(module.ImportReference(typeof(List)), module)); - var adder = module.Import(typeof(ICollection<>)).Resolve().Methods.FirstOrDefault(md => md.Name == "Add" && md.Parameters.Count == 1); - var adderRef = module.Import(adder); - adderRef = module.Import(adderRef.ResolveGenericParameters(module.Import(typeof(ICollection)), module)); + var adder = module.ImportReference(typeof(ICollection<>)).Resolve().Methods.FirstOrDefault(md => md.Name == "Add" && md.Parameters.Count == 1); + var adderRef = module.ImportReference(adder); + adderRef = module.ImportReference(adderRef.ResolveGenericParameters(module.ImportReference(typeof(ICollection)), module)); yield return Instruction.Create(OpCodes.Ldc_I4, parts.Count); yield return Instruction.Create(OpCodes.Newobj, listCtorRef); diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/RectangleTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/RectangleTypeConverter.cs index a7eda95e1..1175dd221 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/RectangleTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/RectangleTypeConverter.cs @@ -40,8 +40,8 @@ namespace Xamarin.Forms.Core.XamlC yield return Instruction.Create(OpCodes.Ldc_R8, w); yield return Instruction.Create(OpCodes.Ldc_R8, h); - var rectangleCtor = module.Import(typeof(Rectangle)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 4); - var rectangleCtorRef = module.Import(rectangleCtor); + var rectangleCtor = module.ImportReference(typeof(Rectangle)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 4); + var rectangleCtorRef = module.ImportReference(rectangleCtor); yield return Instruction.Create(OpCodes.Newobj, rectangleCtorRef); } } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs index e16a8b01a..adca4d898 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs @@ -42,8 +42,8 @@ namespace Xamarin.Forms.Core.XamlC { foreach (var d in args) yield return Instruction.Create(OpCodes.Ldc_R8, d); - var thicknessCtor = module.Import(typeof(Thickness)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == args.Length); - var thicknessCtorRef = module.Import(thicknessCtor); + var thicknessCtor = module.ImportReference(typeof(Thickness)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == args.Length); + var thicknessCtorRef = module.ImportReference(thicknessCtor); yield return Instruction.Create(OpCodes.Newobj, thicknessCtorRef); } } diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/TypeTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/TypeTypeConverter.cs index 93c3ee581..b8f02cc17 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledConverters/TypeTypeConverter.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/TypeTypeConverter.cs @@ -31,9 +31,9 @@ namespace Xamarin.Forms.Core.XamlC if (typeRef == null) goto error; - var getTypeFromHandle = module.Import(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) })); - yield return Instruction.Create(OpCodes.Ldtoken, module.Import(typeRef)); - yield return Instruction.Create(OpCodes.Call, module.Import(getTypeFromHandle)); + var getTypeFromHandle = module.ImportReference(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) })); + yield return Instruction.Create(OpCodes.Ldtoken, module.ImportReference(typeRef)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(getTypeFromHandle)); yield break; error: diff --git a/Xamarin.Forms.Build.Tasks/CompiledMarkupExtensions/StaticExtension.cs b/Xamarin.Forms.Build.Tasks/CompiledMarkupExtensions/StaticExtension.cs index b7386a8b2..4565c64fe 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledMarkupExtensions/StaticExtension.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledMarkupExtensions/StaticExtension.cs @@ -26,7 +26,7 @@ namespace Xamarin.Forms.Build.Tasks var typename = member.Substring(0, dotIdx); var membername = member.Substring(dotIdx + 1); - var typeRef = module.Import(GetTypeReference(typename, module, node)); + var typeRef = module.ImportReference(GetTypeReference(typename, module, node)); var fieldRef = GetFieldReference(typeRef, membername, module); var propertyDef = GetPropertyDefinition(typeRef, membername, module); @@ -75,7 +75,7 @@ namespace Xamarin.Forms.Build.Tasks } memberRef = propertyDef.PropertyType; - var getterDef = module.Import(propertyDef.GetMethod); + var getterDef = module.ImportReference(propertyDef.GetMethod); return new [] { Instruction.Create(OpCodes.Call, getterDef) }; } @@ -105,8 +105,8 @@ namespace Xamarin.Forms.Build.Tasks fd.IsStatic && fd.IsPublic, out declaringTypeReference); if (fRef != null) { - fRef = module.Import(fRef.ResolveGenericParameters(declaringTypeReference)); - fRef.FieldType = module.Import(fRef.FieldType); + fRef = module.ImportReference(fRef.ResolveGenericParameters(declaringTypeReference)); + fRef.FieldType = module.ImportReference(fRef.FieldType); } return fRef; } diff --git a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs index 60c3328f4..e417f118f 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/SetterValueProvider.cs @@ -29,7 +29,7 @@ namespace Xamarin.Forms.Core.XamlC var bpRef = (new BindablePropertyConverter()).GetBindablePropertyFieldReference((string)bpNode.Value, module, bpNode); TypeReference _; - var setValueRef = module.Import(module.Import(typeof(Setter)).GetProperty(p => p.Name == "Value", out _).SetMethod); + var setValueRef = module.ImportReference(module.ImportReference(typeof(Setter)).GetProperty(p => p.Name == "Value", out _).SetMethod); //push the setter yield return Instruction.Create(OpCodes.Ldloc, vardefref.VariableDefinition); diff --git a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs index f355a5287..e00a1c116 100644 --- a/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs +++ b/Xamarin.Forms.Build.Tasks/CompiledValueProviders/TriggerValueProvider.cs @@ -29,7 +29,7 @@ namespace Xamarin.Forms.Core.XamlC var bpRef = (new BindablePropertyConverter()).GetBindablePropertyFieldReference((string)bpNode.Value, module, bpNode); TypeReference _; - var setValueRef = module.Import(module.Import(typeof(Trigger)).GetProperty(p => p.Name == "Value", out _).SetMethod); + var setValueRef = module.ImportReference(module.ImportReference(typeof(Trigger)).GetProperty(p => p.Name == "Value", out _).SetMethod); //push the setter yield return Instruction.Create(OpCodes.Ldloc, vardefref.VariableDefinition); diff --git a/Xamarin.Forms.Build.Tasks/CreateObjectVisitor.cs b/Xamarin.Forms.Build.Tasks/CreateObjectVisitor.cs index 3e3d10931..b73926fd4 100644 --- a/Xamarin.Forms.Build.Tasks/CreateObjectVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/CreateObjectVisitor.cs @@ -39,7 +39,7 @@ namespace Xamarin.Forms.Build.Tasks public void Visit(ElementNode node, INode parentNode) { - var typeref = Module.Import(node.XmlType.GetTypeReference(Module, node)); + var typeref = Module.ImportReference(node.XmlType.GetTypeReference(Module, node)); TypeDefinition typedef = typeref.Resolve(); if (IsXaml2009LanguagePrimitive(node)) { @@ -56,7 +56,7 @@ namespace Xamarin.Forms.Build.Tasks var markupProvider = new StaticExtension(); var il = markupProvider.ProvideValue(node, Module, Context, out typeref); - typeref = Module.Import(typeref); + typeref = Module.ImportReference(typeref); var vardef = new VariableDefinition(typeref); Context.Variables [node] = vardef; @@ -141,15 +141,15 @@ namespace Xamarin.Forms.Build.Tasks } else if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null && implicitOperatorref != null) { //path.png - var implicitOperator = Module.Import(implicitOperatorref); + var implicitOperator = Module.ImportReference(implicitOperatorref); Context.IL.Emit(OpCodes.Ldstr, ((ValueNode)(node.CollectionItems.First())).Value as string); Context.IL.Emit(OpCodes.Call, implicitOperator); Context.IL.Emit(OpCodes.Stloc, vardef); } else if (factorymethodinforef != null) { - Context.IL.Emit(OpCodes.Call, Module.Import(factorymethodinforef)); + Context.IL.Emit(OpCodes.Call, Module.ImportReference(factorymethodinforef)); Context.IL.Emit(OpCodes.Stloc, vardef); } else if (!typedef.IsValueType) { - var ctor = Module.Import(ctorinforef); + var ctor = Module.ImportReference(ctorinforef); // IL_0001: newobj instance void class [Xamarin.Forms.Core]Xamarin.Forms.Button::'.ctor'() // IL_0006: stloc.0 Context.IL.Emit(OpCodes.Newobj, ctor); @@ -160,7 +160,7 @@ namespace Xamarin.Forms.Build.Tasks // IL_000a: ldc.i4.1 // IL_000b: call instance void valuetype Test/Foo::'.ctor'(bool) - var ctor = Module.Import(ctorinforef); + var ctor = Module.ImportReference(ctorinforef); Context.IL.Emit(OpCodes.Ldloca, vardef); Context.IL.Append(PushCtorXArguments(factoryCtorInfo, node)); Context.IL.Emit(OpCodes.Call, ctor); @@ -168,7 +168,7 @@ namespace Xamarin.Forms.Build.Tasks // IL_0000: ldloca.s 0 // IL_0002: initobj Test/Foo Context.IL.Emit(OpCodes.Ldloca, vardef); - Context.IL.Emit(OpCodes.Initobj, Module.Import(typedef)); + Context.IL.Emit(OpCodes.Initobj, Module.ImportReference(typedef)); } if (typeref.FullName == "Xamarin.Forms.Xaml.TypeExtension") { @@ -433,7 +433,7 @@ namespace Xamarin.Forms.Build.Tasks var ctorinfo = Context.Body.Method.Module.TypeSystem.Object.Resolve() .Methods.FirstOrDefault(md => md.IsConstructor && !md.HasParameters); - var ctor = Context.Body.Method.Module.Import(ctorinfo); + var ctor = Context.Body.Method.Module.ImportReference(ctorinfo); yield return Instruction.Create(OpCodes.Newobj, ctor); break; case "System.Char": @@ -446,7 +446,7 @@ namespace Xamarin.Forms.Build.Tasks case "System.Decimal": decimal outdecimal; if (hasValue && decimal.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out outdecimal)) { - var vardef = new VariableDefinition(Context.Body.Method.Module.Import(typeof(decimal))); + var vardef = new VariableDefinition(Context.Body.Method.Module.ImportReference(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" @@ -458,29 +458,29 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldstr, valueString); yield return Instruction.Create(OpCodes.Ldc_I4, 0x6f); //NumberStyles.Number var getInvariantInfo = - Context.Body.Method.Module.Import(typeof(CultureInfo)) + Context.Body.Method.Module.ImportReference(typeof(CultureInfo)) .Resolve() .Properties.FirstOrDefault(pd => pd.Name == "InvariantCulture") .GetMethod; - var getInvariant = Context.Body.Method.Module.Import(getInvariantInfo); + var getInvariant = Context.Body.Method.Module.ImportReference(getInvariantInfo); yield return Instruction.Create(OpCodes.Call, getInvariant); yield return Instruction.Create(OpCodes.Ldloca, vardef); var tryParseInfo = - Context.Body.Method.Module.Import(typeof(decimal)) + Context.Body.Method.Module.ImportReference(typeof(decimal)) .Resolve() .Methods.FirstOrDefault(md => md.Name == "TryParse" && md.Parameters.Count == 4); - var tryParse = Context.Body.Method.Module.Import(tryParseInfo); + var tryParse = Context.Body.Method.Module.ImportReference(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)) + Context.Body.Method.Module.ImportReference(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); + var decimalctor = Context.Body.Method.Module.ImportReference(decimalctorinfo); yield return Instruction.Create(OpCodes.Newobj, decimalctor); } break; @@ -501,51 +501,51 @@ namespace Xamarin.Forms.Build.Tasks case "System.TimeSpan": TimeSpan outspan; if (hasValue && TimeSpan.TryParse(valueString, CultureInfo.InvariantCulture, out outspan)) { - var vardef = new VariableDefinition(Context.Body.Method.Module.Import(typeof(TimeSpan))); + var vardef = new VariableDefinition(Context.Body.Method.Module.ImportReference(typeof(TimeSpan))); Context.Body.Variables.Add(vardef); //Use an extra temp var so we can push the value to the stack, just like other cases yield return Instruction.Create(OpCodes.Ldstr, valueString); var getInvariantInfo = - Context.Body.Method.Module.Import(typeof(CultureInfo)) + Context.Body.Method.Module.ImportReference(typeof(CultureInfo)) .Resolve() .Properties.FirstOrDefault(pd => pd.Name == "InvariantCulture") .GetMethod; - var getInvariant = Context.Body.Method.Module.Import(getInvariantInfo); + var getInvariant = Context.Body.Method.Module.ImportReference(getInvariantInfo); yield return Instruction.Create(OpCodes.Call, getInvariant); yield return Instruction.Create(OpCodes.Ldloca, vardef); var tryParseInfo = - Context.Body.Method.Module.Import(typeof(TimeSpan)) + Context.Body.Method.Module.ImportReference(typeof(TimeSpan)) .Resolve() .Methods.FirstOrDefault(md => md.Name == "TryParse" && md.Parameters.Count == 3); - var tryParse = Context.Body.Method.Module.Import(tryParseInfo); + var tryParse = Context.Body.Method.Module.ImportReference(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_I8, 0L); var timespanctorinfo = - Context.Body.Method.Module.Import(typeof(TimeSpan)) + Context.Body.Method.Module.ImportReference(typeof(TimeSpan)) .Resolve() .Methods.FirstOrDefault( md => md.IsConstructor && md.Parameters.Count == 1 && md.Parameters [0].ParameterType.FullName == "System.Int64"); - var timespanctor = Context.Body.Method.Module.Import(timespanctorinfo); + var timespanctor = Context.Body.Method.Module.ImportReference(timespanctorinfo); yield return Instruction.Create(OpCodes.Newobj, timespanctor); } break; case "System.Uri": Uri outuri; if (hasValue && Uri.TryCreate(valueString, UriKind.RelativeOrAbsolute, out outuri)) { - var vardef = new VariableDefinition(Context.Body.Method.Module.Import(typeof(Uri))); + var vardef = new VariableDefinition(Context.Body.Method.Module.ImportReference(typeof(Uri))); Context.Body.Variables.Add(vardef); //Use an extra temp var so we can push the value to the stack, just like other cases yield return Instruction.Create(OpCodes.Ldstr, valueString); yield return Instruction.Create(OpCodes.Ldc_I4, (int)UriKind.RelativeOrAbsolute); yield return Instruction.Create(OpCodes.Ldloca, vardef); var tryCreateInfo = - Context.Body.Method.Module.Import(typeof(Uri)) + Context.Body.Method.Module.ImportReference(typeof(Uri)) .Resolve() .Methods.FirstOrDefault(md => md.Name == "TryCreate" && md.Parameters.Count == 3); - var tryCreate = Context.Body.Method.Module.Import(tryCreateInfo); + var tryCreate = Context.Body.Method.Module.ImportReference(tryCreateInfo); yield return Instruction.Create(OpCodes.Call, tryCreate); yield return Instruction.Create(OpCodes.Pop); yield return Instruction.Create(OpCodes.Ldloc, vardef); @@ -555,7 +555,7 @@ namespace Xamarin.Forms.Build.Tasks default: var defaultctorinfo = typedef.Methods.FirstOrDefault(md => md.IsConstructor && !md.HasParameters); if (defaultctorinfo != null) { - var defaultctor = Context.Body.Method.Module.Import(defaultctorinfo); + var defaultctor = Context.Body.Method.Module.ImportReference(defaultctorinfo); yield return Instruction.Create(OpCodes.Newobj, defaultctor); } else { //should never happen. but if it does, this prevents corrupting the IL stack diff --git a/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs b/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs index a8ef874af..1c7acac4a 100644 --- a/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs +++ b/Xamarin.Forms.Build.Tasks/DebugXamlCTask.cs @@ -2,16 +2,22 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; + using Mono.Cecil; using Mono.Cecil.Cil; +using Mono.Cecil.Mdb; +using Mono.Cecil.Pdb; using Mono.Cecil.Rocks; +using Xamarin.Forms.Xaml; + namespace Xamarin.Forms.Build.Tasks { public class DebugXamlCTask : XamlTask { - public override bool Execute(IList thrownExceptions) + public override bool Execute(out IList thrownExceptions) { + thrownExceptions = null; Logger = Logger ?? new Logger(null, Verbosity); Logger.LogLine(1, "Preparing debug code for xamlc"); Logger.LogLine(1, "\nAssembly: {0}", Assembly); @@ -37,109 +43,116 @@ namespace Xamarin.Forms.Build.Tasks // resolver.AddAssembly (p); } } - var assemblyDefinition = AssemblyDefinition.ReadAssembly(Assembly, new ReaderParameters - { - //ReadSymbols = DebugSymbols, + using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(Assembly, new ReaderParameters { + ReadWrite = true, + ReadSymbols = DebugSymbols, + SymbolReaderProvider = System.Type.GetType("Mono.Runtime") != null ? ((ISymbolReaderProvider)(new MdbReaderProvider())) : ((ISymbolReaderProvider)new PdbReaderProvider()), AssemblyResolver = resolver - }); + })) { + foreach (var module in assemblyDefinition.Modules) { + Logger.LogLine(2, " Module: {0}", module.Name); + foreach (var resource in module.Resources.OfType()) { + Logger.LogString(2, " Resource: {0}... ", resource.Name); + string classname; + if (!resource.IsXaml(out classname)) { + Logger.LogLine(2, "skipped."); + continue; + } else + Logger.LogLine(2, ""); + TypeDefinition typeDef = module.GetType(classname); + if (typeDef == null) { + Logger.LogLine(2, "no type found... skipped."); + continue; + } - foreach (var module in assemblyDefinition.Modules) - { - Logger.LogLine(2, " Module: {0}", module.Name); - foreach (var resource in module.Resources.OfType()) - { - Logger.LogString(2, " Resource: {0}... ", resource.Name); - string classname; - if (!resource.IsXaml(out classname)) - { - Logger.LogLine(2, "skipped."); - continue; - } - TypeDefinition typeDef = module.GetType(classname); - if (typeDef == null) - { - Logger.LogLine(2, "no type found... skipped."); - continue; - } - var initComp = typeDef.Methods.FirstOrDefault(md => md.Name == "InitializeComponent"); - if (initComp == null) - { - Logger.LogLine(2, "no InitializeComponent found... skipped."); - continue; - } - var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime"); - if (initCompRuntime == null) { - Logger.LogLine(2, "no __InitComponentRuntime found... duplicating."); - initCompRuntime = DuplicateMethodDef(typeDef, initComp, "__InitComponentRuntime"); - } + var initComp = typeDef.Methods.FirstOrDefault(md => md.Name == "InitializeComponent"); + if (initComp == null) { + Logger.LogLine(2, "no InitializeComponent found... skipped."); + continue; + } + var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime"); + if (initCompRuntime == null) { + Logger.LogString(2, " Creating empty {0}.__InitComponentRuntime ...", typeDef.Name); + initCompRuntime = new MethodDefinition("__InitComponentRuntime", initComp.Attributes, initComp.ReturnType); + Logger.LogLine(2, "done."); + Logger.LogString(2, " Copying body of InitializeComponent to __InitComponentRuntime ...", typeDef.Name); + initCompRuntime.Body = new MethodBody(initCompRuntime); + var iCRIl = initCompRuntime.Body.GetILProcessor(); + foreach (var instr in initComp.Body.Instructions) + iCRIl.Append(instr); + initComp.Body.Instructions.Clear(); + initComp.Body.GetILProcessor().Emit(OpCodes.Ret); + typeDef.Methods.Add(initCompRuntime); + Logger.LogLine(2, "done."); + } -// IL_0000: ldarg.0 -// IL_0001: callvirt instance void class [Xamarin.Forms.Core]Xamarin.Forms.ContentPage::'.ctor'() +// IL_0000: ldarg.0 +// IL_0001: callvirt instance void class [Xamarin.Forms.Core]Xamarin.Forms.ContentPage::'.ctor'() // -// IL_0006: nop -// IL_0007: ldarg.1 -// IL_0008: brfalse IL_0018 +// IL_0006: nop +// IL_0007: ldarg.1 +// IL_0008: brfalse IL_0018 // -// IL_000d: ldarg.0 -// IL_000e: callvirt instance void class Xamarin.Forms.Xaml.XamlcTests.MyPage::InitializeComponent() -// IL_0013: br IL_001e +// IL_000d: ldarg.0 +// IL_000e: callvirt instance void class Xamarin.Forms.Xaml.XamlcTests.MyPage::InitializeComponent() +// IL_0013: br IL_001e // -// IL_0018: ldarg.0 -// IL_0019: callvirt instance void class Xamarin.Forms.Xaml.XamlcTests.MyPage::__InitComponentRuntime() -// IL_001e: ret +// IL_0018: ldarg.0 +// IL_0019: callvirt instance void class Xamarin.Forms.Xaml.XamlcTests.MyPage::__InitComponentRuntime() +// IL_001e: ret - var altCtor = - typeDef.Methods.Where( - md => md.IsConstructor && md.Parameters.Count == 1 && md.Parameters[0].ParameterType == module.TypeSystem.Boolean) - .FirstOrDefault(); - if (altCtor != null) - Logger.LogString(2, " Replacing body of {0}.{0} (bool {1}) ... ", typeDef.Name, altCtor.Parameters[0].Name); - else - { - Logger.LogString(2, " Adding {0}.{0} (bool useCompiledXaml) ... ", typeDef.Name); - altCtor = new MethodDefinition(".ctor", - MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | - MethodAttributes.RTSpecialName, module.TypeSystem.Void); - altCtor.Parameters.Add(new ParameterDefinition("useCompiledXaml", ParameterAttributes.None, - module.TypeSystem.Boolean)); + var altCtor = + typeDef.Methods.Where( + md => md.IsConstructor && md.Parameters.Count == 1 && md.Parameters[0].ParameterType == module.TypeSystem.Boolean) + .FirstOrDefault(); + if (altCtor != null) + Logger.LogString(2, " Replacing body of {0}.{0} (bool {1}) ... ", typeDef.Name, altCtor.Parameters[0].Name); + else { + Logger.LogString(2, " Adding {0}.{0} (bool useCompiledXaml) ... ", typeDef.Name); + altCtor = new MethodDefinition(".ctor", + MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | + MethodAttributes.RTSpecialName, module.TypeSystem.Void); + altCtor.Parameters.Add(new ParameterDefinition("useCompiledXaml", ParameterAttributes.None, + module.TypeSystem.Boolean)); + } + + var body = new MethodBody(altCtor); + var il = body.GetILProcessor(); + var br2 = Instruction.Create(OpCodes.Ldarg_0); + var ret = Instruction.Create(OpCodes.Ret); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Callvirt, + module.ImportReference(typeDef.BaseType.Resolve().GetConstructors().First(c => c.HasParameters == false))); + + il.Emit(OpCodes.Nop); + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Brfalse, br2); + + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Callvirt, initComp); + il.Emit(OpCodes.Br, ret); + + il.Append(br2); + il.Emit(OpCodes.Callvirt, initCompRuntime); + il.Append(ret); + + altCtor.Body = body; + if (!typeDef.Methods.Contains(altCtor)) + typeDef.Methods.Add(altCtor); + Logger.LogLine(2, "done."); } - var body = new MethodBody(altCtor); - var il = body.GetILProcessor(); - var br2 = Instruction.Create(OpCodes.Ldarg_0); - var ret = Instruction.Create(OpCodes.Ret); - il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Callvirt, - module.Import(typeDef.BaseType.Resolve().GetConstructors().First(c => c.HasParameters == false))); - - il.Emit(OpCodes.Nop); - il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Brfalse, br2); - - il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Callvirt, initComp); - il.Emit(OpCodes.Br, ret); - - il.Append(br2); - il.Emit(OpCodes.Callvirt, initCompRuntime); - il.Append(ret); - - altCtor.Body = body; - if (!typeDef.Methods.Contains(altCtor)) - typeDef.Methods.Add(altCtor); - Logger.LogLine(2, "done."); + Logger.LogLine(2, ""); } - - Logger.LogLine(2, ""); + Logger.LogString(1, "Writing the assembly... "); + assemblyDefinition.Write(new WriterParameters { + SymbolWriterProvider = System.Type.GetType("Mono.Runtime") != null ? ((ISymbolWriterProvider)(new MdbWriterProvider())) : ((ISymbolWriterProvider)new MdbWriterProvider()), + WriteSymbols = DebugSymbols + }); } - Logger.LogString(1, "Writing the assembly... "); - assemblyDefinition.Write(Assembly, new WriterParameters - { - WriteSymbols = DebugSymbols - }); Logger.LogLine(1, "done."); return true; } } -} \ No newline at end of file +} diff --git a/Xamarin.Forms.Build.Tasks/MethodReferenceExtensions.cs b/Xamarin.Forms.Build.Tasks/MethodReferenceExtensions.cs index e3b2f3ab5..2c0d8ba94 100644 --- a/Xamarin.Forms.Build.Tasks/MethodReferenceExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/MethodReferenceExtensions.cs @@ -13,16 +13,16 @@ namespace Xamarin.Forms.Build.Tasks if (declaringTypeRef == null) throw new ArgumentNullException(nameof(declaringTypeRef)); - var reference = new MethodReference(self.Name, module.Import(self.ReturnType)) + var reference = new MethodReference(self.Name, module.ImportReference(self.ReturnType)) { - DeclaringType = module.Import(declaringTypeRef), + DeclaringType = module.ImportReference(declaringTypeRef), HasThis = self.HasThis, ExplicitThis = self.ExplicitThis, CallingConvention = self.CallingConvention }; foreach (var parameter in self.Parameters) { - var p = parameter.ParameterType.IsGenericParameter ? parameter.ParameterType : module.Import(parameter.ParameterType); + var p = parameter.ParameterType.IsGenericParameter ? parameter.ParameterType : module.ImportReference(parameter.ParameterType); reference.Parameters.Add(new ParameterDefinition(p)); } @@ -37,7 +37,7 @@ namespace Xamarin.Forms.Build.Tasks if (self.HasParameters) { for (var i = 0; i < self.Parameters.Count; i++) - self.Parameters[i].ParameterType = module.Import(self.Parameters[i].ParameterType); + self.Parameters[i].ParameterType = module.ImportReference(self.Parameters[i].ParameterType); } } diff --git a/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs b/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs index 08c102aee..ca0f9fd64 100644 --- a/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs @@ -52,7 +52,7 @@ namespace Xamarin.Forms.Build.Tasks 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; + var compiledConverterName = typeConverter?.GetCustomAttribute (module.ImportReference(typeof(ProvideCompiledAttribute)))?.ConstructorArguments?.First().Value as string; Type compiledConverterType; if (compiledConverterName != null && (compiledConverterType = Type.GetType (compiledConverterName)) != null) { var compiledConverter = Activator.CreateInstance (compiledConverterType); @@ -62,24 +62,24 @@ namespace Xamarin.Forms.Build.Tasks foreach (var i in instructions) yield return i; if (targetTypeRef.IsValueType && boxValueTypes) - yield return Instruction.Create (OpCodes.Box, module.Import (targetTypeRef)); + yield return Instruction.Create (OpCodes.Box, module.ImportReference (targetTypeRef)); yield break; } //If there's a [TypeConverter], use it if (typeConverter != null) { - var isExtendedConverter = typeConverter.ImplementsInterface(module.Import(typeof (IExtendedTypeConverter))); + var isExtendedConverter = typeConverter.ImplementsInterface(module.ImportReference(typeof (IExtendedTypeConverter))); var typeConverterCtor = typeConverter.Resolve().Methods.Single(md => md.IsConstructor && md.Parameters.Count == 0 && !md.IsStatic); - var typeConverterCtorRef = module.Import(typeConverterCtor); + var typeConverterCtorRef = module.ImportReference(typeConverterCtor); var convertFromInvariantStringDefinition = isExtendedConverter - ? module.Import(typeof (IExtendedTypeConverter)) + ? module.ImportReference(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); + var convertFromInvariantStringReference = module.ImportReference(convertFromInvariantStringDefinition); yield return Instruction.Create(OpCodes.Newobj, typeConverterCtorRef); yield return Instruction.Create(OpCodes.Ldstr, node.Value as string); @@ -93,7 +93,7 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Callvirt, convertFromInvariantStringReference); if (targetTypeRef.IsValueType && unboxValueTypes) - yield return Instruction.Create(OpCodes.Unbox_Any, module.Import(targetTypeRef)); + yield return Instruction.Create(OpCodes.Unbox_Any, module.ImportReference(targetTypeRef)); //ConvertFrom returns an object, no need to Box yield break; @@ -146,10 +146,10 @@ namespace Xamarin.Forms.Build.Tasks var ts = TimeSpan.Parse(str, CultureInfo.InvariantCulture); var ticks = ts.Ticks; var timeSpanCtor = - module.Import(typeof(TimeSpan)) + module.ImportReference(typeof(TimeSpan)) .Resolve() .Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 1); - var timeSpanCtorRef = module.Import(timeSpanCtor); + var timeSpanCtorRef = module.ImportReference(timeSpanCtor); yield return Instruction.Create(OpCodes.Ldc_I8, ticks); yield return Instruction.Create(OpCodes.Newobj, timeSpanCtorRef); @@ -157,10 +157,10 @@ namespace Xamarin.Forms.Build.Tasks var dt = DateTime.Parse(str, CultureInfo.InvariantCulture); var ticks = dt.Ticks; var dateTimeCtor = - module.Import(typeof(DateTime)) + module.ImportReference(typeof(DateTime)) .Resolve() .Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 1); - var dateTimeCtorRef = module.Import(dateTimeCtor); + var dateTimeCtorRef = module.ImportReference(dateTimeCtor); yield return Instruction.Create(OpCodes.Ldc_I8, ticks); yield return Instruction.Create(OpCodes.Newobj, dateTimeCtorRef); @@ -173,7 +173,7 @@ namespace Xamarin.Forms.Build.Tasks 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))); + var vardef = new VariableDefinition(context.Body.Method.Module.ImportReference(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" @@ -185,41 +185,41 @@ namespace Xamarin.Forms.Build.Tasks 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)) + context.Body.Method.Module.ImportReference(typeof(CultureInfo)) .Resolve() .Properties.FirstOrDefault(pd => pd.Name == "InvariantCulture") .GetMethod; - var getInvariant = context.Body.Method.Module.Import(getInvariantInfo); + var getInvariant = context.Body.Method.Module.ImportReference(getInvariantInfo); yield return Instruction.Create(OpCodes.Call, getInvariant); yield return Instruction.Create(OpCodes.Ldloca, vardef); var tryParseInfo = - context.Body.Method.Module.Import(typeof(decimal)) + context.Body.Method.Module.ImportReference(typeof(decimal)) .Resolve() .Methods.FirstOrDefault(md => md.Name == "TryParse" && md.Parameters.Count == 4); - var tryParse = context.Body.Method.Module.Import(tryParseInfo); + var tryParse = context.Body.Method.Module.ImportReference(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)) + context.Body.Method.Module.ImportReference(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); + var decimalctor = context.Body.Method.Module.ImportReference(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)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(implicitOperator)); } else yield return Instruction.Create(OpCodes.Ldnull); if (isNullable) - yield return Instruction.Create(OpCodes.Newobj, module.Import(nullableCtor)); + yield return Instruction.Create(OpCodes.Newobj, module.ImportReference(nullableCtor)); if (originalTypeRef.IsValueType && boxValueTypes) - yield return Instruction.Create(OpCodes.Box, module.Import(originalTypeRef)); + yield return Instruction.Create(OpCodes.Box, module.ImportReference(originalTypeRef)); } static Instruction PushParsedEnum(TypeReference enumRef, string value, IXmlLineInfo lineInfo) @@ -320,10 +320,10 @@ namespace Xamarin.Forms.Build.Tasks { 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) })); + ctor = module.ImportReference(typeof (XmlLineInfo).GetConstructor(new[] { typeof (int), typeof (int) })); } else - ctor = module.Import(typeof (XmlLineInfo).GetConstructor(new Type[] { })); + ctor = module.ImportReference(typeof (XmlLineInfo).GetConstructor(new Type[] { })); yield return Instruction.Create(OpCodes.Newobj, ctor); } @@ -373,7 +373,7 @@ namespace Xamarin.Forms.Build.Tasks 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[]))); + var finalArray = new VariableDefinition(module.ImportReference(typeof (object[]))); context.Body.Variables.Add(finalArray); yield return Instruction.Create(OpCodes.Stloc, finalArray); @@ -387,13 +387,13 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldc_I4, nodes.Count); //destinationIndex yield return Instruction.Create(OpCodes.Ldloc, parentObjectLength); //length var arrayCopy = - module.Import(typeof (Array)) + module.ImportReference(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)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(arrayCopy)); } //Add nodes to array @@ -407,7 +407,7 @@ namespace Xamarin.Forms.Build.Tasks 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.Box, module.ImportReference(context.Variables[en].VariableType)); yield return Instruction.Create(OpCodes.Stelem_Ref); } } @@ -424,12 +424,12 @@ namespace Xamarin.Forms.Build.Tasks // 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)); + var getTypeFromHandle = module.ImportReference(typeof(Type).GetMethod("GetTypeFromHandle", new [] { typeof(RuntimeTypeHandle) })); + var getPropertyInfo = module.ImportReference(typeof(Type).GetMethod("GetProperty", new [] { typeof(string) })); + yield return Instruction.Create(OpCodes.Ldtoken, module.ImportReference(declaringTypeReference ?? propertyRef.DeclaringType)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(getTypeFromHandle)); yield return Instruction.Create(OpCodes.Ldstr, propertyRef.Name); - yield return Instruction.Create(OpCodes.Callvirt, module.Import(getPropertyInfo)); + yield return Instruction.Create(OpCodes.Callvirt, module.ImportReference(getPropertyInfo)); yield break; } yield return Instruction.Create(OpCodes.Ldnull); @@ -446,14 +446,14 @@ namespace Xamarin.Forms.Build.Tasks #endif var ctorinfo = typeof (XamlServiceProvider).GetConstructor(new Type[] { }); - var ctor = module.Import(ctorinfo); + var ctor = module.ImportReference(ctorinfo); var addServiceInfo = typeof (XamlServiceProvider).GetMethod("Add", new[] { typeof (Type), typeof (object) }); - var addService = module.Import(addServiceInfo); + var addService = module.ImportReference(addServiceInfo); var getTypeFromHandle = - module.Import(typeof (Type).GetMethod("GetTypeFromHandle", new[] { typeof (RuntimeTypeHandle) })); - var getAssembly = module.Import(typeof (Type).GetProperty("Assembly").GetMethod); + module.ImportReference(typeof (Type).GetMethod("GetTypeFromHandle", new[] { typeof (RuntimeTypeHandle) })); + var getAssembly = module.ImportReference(typeof (Type).GetProperty("Assembly").GetMethod); yield return Instruction.Create(OpCodes.Newobj, ctor); @@ -462,8 +462,8 @@ namespace Xamarin.Forms.Build.Tasks 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)); + yield return Instruction.Create(OpCodes.Ldtoken, module.ImportReference(typeof (IProvideValueTarget))); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(getTypeFromHandle)); foreach (var instruction in pushParentIl) yield return instruction; @@ -472,7 +472,7 @@ namespace Xamarin.Forms.Build.Tasks yield return instruction; var targetProviderCtor = - module.Import(typeof (SimpleValueTargetProvider).GetConstructor(new[] { typeof (object[]), typeof(object) })); + module.ImportReference(typeof (SimpleValueTargetProvider).GetConstructor(new[] { typeof (object[]), typeof(object) })); yield return Instruction.Create(OpCodes.Newobj, targetProviderCtor); yield return Instruction.Create(OpCodes.Callvirt, addService); } @@ -481,12 +481,12 @@ namespace Xamarin.Forms.Build.Tasks 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.Ldtoken, module.ImportReference(typeof (INameScopeProvider))); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(getTypeFromHandle)); + var namescopeProviderCtor = module.ImportReference(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()); + var setNamescope = module.ImportReference(typeof (NameScopeProvider).GetProperty("NameScope").GetSetMethod()); yield return Instruction.Create(OpCodes.Ldloc, context.Scopes[node].Item1); yield return Instruction.Create(OpCodes.Callvirt, setNamescope); @@ -497,10 +497,10 @@ namespace Xamarin.Forms.Build.Tasks 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.Ldtoken, module.ImportReference(typeof (IXamlTypeResolver))); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(getTypeFromHandle)); + var xmlNamespaceResolverCtor = module.ImportReference(typeof (XmlNamespaceResolver).GetConstructor(new Type[] { })); + var addNamespace = module.ImportReference(typeof (XmlNamespaceResolver).GetMethod("Add")); yield return Instruction.Create(OpCodes.Newobj, xmlNamespaceResolverCtor); foreach (var kvp in node.NamespaceResolver.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml)) { @@ -510,10 +510,10 @@ namespace Xamarin.Forms.Build.Tasks 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.Call, module.ImportReference(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)); + var xtr = module.ImportReference(typeof (XamlTypeResolver)).Resolve(); + var xamlTypeResolverCtor = module.ImportReference(xtr.Methods.First(md => md.IsConstructor && md.Parameters.Count == 2)); yield return Instruction.Create(OpCodes.Newobj, xamlTypeResolverCtor); yield return Instruction.Create(OpCodes.Callvirt, addService); } @@ -521,17 +521,17 @@ namespace Xamarin.Forms.Build.Tasks 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)); + yield return Instruction.Create(OpCodes.Ldtoken, module.ImportReference(typeof (IXmlLineInfoProvider))); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(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)); + var lip = module.ImportReference(typeof (XmlLineInfoProvider)).Resolve(); + var lineInfoProviderCtor = module.ImportReference(lip.Methods.First(md => md.IsConstructor && md.Parameters.Count == 1)); yield return Instruction.Create(OpCodes.Newobj, lineInfoProviderCtor); yield return Instruction.Create(OpCodes.Callvirt, addService); } } } -} \ No newline at end of file +} diff --git a/Xamarin.Forms.Build.Tasks/Properties/AssemblyInfo.cs b/Xamarin.Forms.Build.Tasks/Properties/AssemblyInfo.cs index ea4bda348..454c5cb86 100644 --- a/Xamarin.Forms.Build.Tasks/Properties/AssemblyInfo.cs +++ b/Xamarin.Forms.Build.Tasks/Properties/AssemblyInfo.cs @@ -27,4 +27,5 @@ using System.Runtime.CompilerServices; //[assembly: AssemblyKeyFile("")] [assembly: InternalsVisibleTo("xamlg")] +[assembly: InternalsVisibleTo("xamlc")] [assembly: InternalsVisibleTo("Xamarin.Forms.Xaml.UnitTests")] \ No newline at end of file diff --git a/Xamarin.Forms.Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs index 674b1f35d..1c776ab8f 100644 --- a/Xamarin.Forms.Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetNamescopesAndRegisterNamesVisitor.cs @@ -44,7 +44,7 @@ namespace Xamarin.Forms.Build.Tasks namescopeVarDef = Context.Scopes[parentNode].Item1; namesInNamescope = Context.Scopes[parentNode].Item2; } - if (Context.Variables[node].VariableType.InheritsFromOrImplements(Context.Body.Method.Module.Import(typeof (BindableObject)))) + if (Context.Variables[node].VariableType.InheritsFromOrImplements(Context.Body.Method.Module.ImportReference(typeof (BindableObject)))) SetNameScope(node, namescopeVarDef); Context.Scopes[node] = new System.Tuple>(namescopeVarDef, namesInNamescope); } @@ -53,7 +53,7 @@ namespace Xamarin.Forms.Build.Tasks { var namescopeVarDef = CreateNamescope(); IList namesInNamescope = new List(); - if (Context.Variables[node].VariableType.InheritsFromOrImplements(Context.Body.Method.Module.Import(typeof (BindableObject)))) + if (Context.Variables[node].VariableType.InheritsFromOrImplements(Context.Body.Method.Module.ImportReference(typeof (BindableObject)))) SetNameScope(node, namescopeVarDef); Context.Scopes[node] = new System.Tuple>(namescopeVarDef, namesInNamescope); } @@ -91,12 +91,12 @@ namespace Xamarin.Forms.Build.Tasks VariableDefinition CreateNamescope() { var module = Context.Body.Method.Module; - var nsRef = module.Import(typeof (NameScope)); + var nsRef = module.ImportReference(typeof (NameScope)); var vardef = new VariableDefinition(nsRef); Context.Body.Variables.Add(vardef); var nsDef = nsRef.Resolve(); var ctorinfo = nsDef.Methods.First(md => md.IsConstructor && !md.HasParameters); - var ctor = module.Import(ctorinfo); + var ctor = module.ImportReference(ctorinfo); Context.IL.Emit(OpCodes.Newobj, ctor); Context.IL.Emit(OpCodes.Stloc, vardef); return vardef; @@ -105,10 +105,10 @@ namespace Xamarin.Forms.Build.Tasks void SetNameScope(ElementNode node, VariableDefinition ns) { var module = Context.Body.Method.Module; - var nsRef = module.Import(typeof (NameScope)); + var nsRef = module.ImportReference(typeof (NameScope)); var nsDef = nsRef.Resolve(); var setNSInfo = nsDef.Methods.First(md => md.Name == "SetNameScope" && md.IsStatic); - var setNS = module.Import(setNSInfo); + var setNS = module.ImportReference(setNSInfo); Context.IL.Emit(OpCodes.Ldloc, Context.Variables[node]); Context.IL.Emit(OpCodes.Ldloc, ns); Context.IL.Emit(OpCodes.Call, setNS); @@ -121,10 +121,10 @@ namespace Xamarin.Forms.Build.Tasks namesInNamescope.Add(str); var module = Context.Body.Method.Module; - var nsRef = module.Import(typeof (INameScope)); + var nsRef = module.ImportReference(typeof (INameScope)); var nsDef = nsRef.Resolve(); var registerInfo = nsDef.Methods.First(md => md.Name == "RegisterName" && md.Parameters.Count == 2); - var register = module.Import(registerInfo); + var register = module.ImportReference(registerInfo); Context.IL.Emit(OpCodes.Ldloc, namescopeVarDef); Context.IL.Emit(OpCodes.Ldstr, str); diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs index 8d087ab7b..608fab5b5 100644 --- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs @@ -128,14 +128,14 @@ namespace Xamarin.Forms.Build.Tasks // Collection element, implicit content, or implicit collection element. string contentProperty; var parentVar = Context.Variables[(IElementNode)parentNode]; - if (parentVar.VariableType.ImplementsInterface(Module.Import(typeof (IEnumerable)))) + if (parentVar.VariableType.ImplementsInterface(Module.ImportReference(typeof (IEnumerable)))) { var elementType = parentVar.VariableType; if (elementType.FullName != "Xamarin.Forms.ResourceDictionary" && elementType.Resolve().BaseType.FullName != "Xamarin.Forms.ResourceDictionary") { var adderTuple = elementType.GetMethods(md => md.Name == "Add" && md.Parameters.Count == 1, Module).First(); - var adderRef = Module.Import(adderTuple.Item1); - adderRef = Module.Import(adderRef.ResolveGenericParameters(adderTuple.Item2, Module)); + var adderRef = Module.ImportReference(adderTuple.Item1); + adderRef = Module.ImportReference(adderRef.ResolveGenericParameters(adderTuple.Item2, Module)); Context.IL.Emit(OpCodes.Ldloc, parentVar); Context.IL.Emit(OpCodes.Ldloc, vardef); @@ -179,13 +179,13 @@ namespace Xamarin.Forms.Build.Tasks if (property != null && (propertyGetter = property.GetMethod) != null && propertyGetter.IsPublic) { - var propertyGetterRef = Module.Import(propertyGetter); - propertyGetterRef = Module.Import(propertyGetterRef.ResolveGenericParameters(propertyDeclaringType, Module)); + var propertyGetterRef = Module.ImportReference(propertyGetter); + propertyGetterRef = Module.ImportReference(propertyGetterRef.ResolveGenericParameters(propertyDeclaringType, Module)); var propertyType = propertyGetterRef.ReturnType.ResolveGenericParameters(propertyDeclaringType); var adderTuple = propertyType.GetMethods(md => md.Name == "Add" && md.Parameters.Count == 1, Module).First(); - var adderRef = Module.Import(adderTuple.Item1); - adderRef = Module.Import(adderRef.ResolveGenericParameters(adderTuple.Item2, Module)); + var adderRef = Module.ImportReference(adderTuple.Item1); + adderRef = Module.ImportReference(adderRef.ResolveGenericParameters(adderTuple.Item2, Module)); Context.IL.Emit(OpCodes.Ldloc, parent); Context.IL.Emit(OpCodes.Callvirt, propertyGetterRef); @@ -256,23 +256,23 @@ namespace Xamarin.Forms.Build.Tasks { var markExt = markupExtension.Resolve(); var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue"); - var provideValue = module.Import(provideValueInfo); + var provideValue = module.ImportReference(provideValueInfo); provideValue = - module.Import(provideValue.ResolveGenericParameters(markupExtension, module)); + module.ImportReference(provideValue.ResolveGenericParameters(markupExtension, module)); var typeNode = node.Properties[new XmlName("", "Type")]; TypeReference arrayTypeRef; if (context.TypeExtensions.TryGetValue(typeNode, out arrayTypeRef)) - vardefref.VariableDefinition = new VariableDefinition(module.Import(arrayTypeRef.MakeArrayType())); + vardefref.VariableDefinition = new VariableDefinition(module.ImportReference(arrayTypeRef.MakeArrayType())); else - vardefref.VariableDefinition = new VariableDefinition(module.Import(genericArguments.First())); + vardefref.VariableDefinition = new VariableDefinition(module.ImportReference(genericArguments.First())); yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]); foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef)) yield return instruction; yield return Instruction.Create(OpCodes.Callvirt, provideValue); if (arrayTypeRef != null) - yield return Instruction.Create(OpCodes.Castclass, module.Import(arrayTypeRef.MakeArrayType())); + yield return Instruction.Create(OpCodes.Castclass, module.ImportReference(arrayTypeRef.MakeArrayType())); yield return Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition); } else if (vardefref.VariableDefinition.VariableType.ImplementsGenericInterface("Xamarin.Forms.Xaml.IMarkupExtension`1", @@ -284,22 +284,22 @@ namespace Xamarin.Forms.Build.Tasks var markExt = markupExtension.Resolve(); var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue"); - var provideValue = module.Import(provideValueInfo); + var provideValue = module.ImportReference(provideValueInfo); provideValue = - module.Import(provideValue.ResolveGenericParameters(markupExtension, module)); + module.ImportReference(provideValue.ResolveGenericParameters(markupExtension, module)); - vardefref.VariableDefinition = new VariableDefinition(module.Import(genericArguments.First())); + vardefref.VariableDefinition = new VariableDefinition(module.ImportReference(genericArguments.First())); yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]); foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef)) yield return instruction; yield return Instruction.Create(OpCodes.Callvirt, provideValue); yield return Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition); } - else if (context.Variables[node].VariableType.ImplementsInterface(module.Import(typeof (IMarkupExtension)))) + else if (context.Variables[node].VariableType.ImplementsInterface(module.ImportReference(typeof (IMarkupExtension)))) { - var markExt = module.Import(typeof (IMarkupExtension)).Resolve(); + var markExt = module.ImportReference(typeof (IMarkupExtension)).Resolve(); var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue"); - var provideValue = module.Import(provideValueInfo); + var provideValue = module.ImportReference(provideValueInfo); vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object); yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]); @@ -308,11 +308,11 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Callvirt, provideValue); yield return Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition); } - else if (context.Variables[node].VariableType.ImplementsInterface(module.Import(typeof (IValueProvider)))) + else if (context.Variables[node].VariableType.ImplementsInterface(module.ImportReference(typeof (IValueProvider)))) { var valueProviderType = context.Variables[node].VariableType; //If the IValueProvider has a ProvideCompiledAttribute that can be resolved, shortcut this - var compiledValueProviderName = valueProviderType?.GetCustomAttribute(module.Import(typeof(ProvideCompiledAttribute)))?.ConstructorArguments?[0].Value as string; + var compiledValueProviderName = valueProviderType?.GetCustomAttribute(module.ImportReference(typeof(ProvideCompiledAttribute)))?.ConstructorArguments?[0].Value as string; Type compiledValueProviderType; if (compiledValueProviderName != null && (compiledValueProviderType = Type.GetType(compiledValueProviderName)) != null) { var compiledValueProvider = Activator.CreateInstance(compiledValueProviderType); @@ -327,9 +327,9 @@ namespace Xamarin.Forms.Build.Tasks yield break; } - var valueProviderDef = module.Import(typeof (IValueProvider)).Resolve(); + var valueProviderDef = module.ImportReference(typeof (IValueProvider)).Resolve(); var provideValueInfo = valueProviderDef.Methods.First(md => md.Name == "ProvideValue"); - var provideValue = module.Import(provideValueInfo); + var provideValue = module.ImportReference(provideValueInfo); vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object); yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]); @@ -372,16 +372,16 @@ namespace Xamarin.Forms.Build.Tasks var properties = ParsePath(path, tSourceRef, node as IXmlLineInfo, context.Module); var tPropertyRef = properties != null && properties.Any() ? properties.Last().Item1.PropertyType : tSourceRef; - var funcRef = context.Module.Import(context.Module.Import(typeof(Func<,>)).MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); - var actionRef = context.Module.Import(context.Module.Import(typeof(Action<,>)).MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); - var funcObjRef = context.Module.Import(context.Module.Import(typeof(Func<,>)).MakeGenericInstanceType(new [] { tSourceRef, context.Module.TypeSystem.Object })); - var tupleRef = context.Module.Import(context.Module.Import(typeof(Tuple<,>)).MakeGenericInstanceType(new [] { funcObjRef, context.Module.TypeSystem.String})); - var typedBindingRef = context.Module.Import(context.Module.Import(typeof(TypedBinding<,>)).MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef})); + var funcRef = context.Module.ImportReference(context.Module.ImportReference(typeof(Func<,>)).MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); + var actionRef = context.Module.ImportReference(context.Module.ImportReference(typeof(Action<,>)).MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); + var funcObjRef = context.Module.ImportReference(context.Module.ImportReference(typeof(Func<,>)).MakeGenericInstanceType(new [] { tSourceRef, context.Module.TypeSystem.Object })); + var tupleRef = context.Module.ImportReference(context.Module.ImportReference(typeof(Tuple<,>)).MakeGenericInstanceType(new [] { funcObjRef, context.Module.TypeSystem.String})); + var typedBindingRef = context.Module.ImportReference(context.Module.ImportReference(typeof(TypedBinding<,>)).MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef})); TypeReference _; - var ctorInfo = context.Module.Import(typedBindingRef.Resolve().Methods.FirstOrDefault(md => md.IsConstructor && !md.IsStatic && md.Parameters.Count == 3 )); + var ctorInfo = context.Module.ImportReference(typedBindingRef.Resolve().Methods.FirstOrDefault(md => md.IsConstructor && !md.IsStatic && md.Parameters.Count == 3 )); var ctorinforef = ctorInfo.MakeGeneric(typedBindingRef, funcRef, actionRef, tupleRef); - var setTypedBinding = context.Module.Import(typeof(BindingExtension)).GetProperty(pd => pd.Name == "TypedBinding", out _).SetMethod; + var setTypedBinding = context.Module.ImportReference(typeof(BindingExtension)).GetProperty(pd => pd.Name == "TypedBinding", out _).SetMethod; yield return Instruction.Create(OpCodes.Ldloc, bindingExt); foreach (var instruction in CompiledBindingGetGetter(tSourceRef, tPropertyRef, properties, node, context)) @@ -390,8 +390,8 @@ namespace Xamarin.Forms.Build.Tasks yield return instruction; foreach (var instruction in CompiledBindingGetHandlers(tSourceRef, tPropertyRef, properties, node, context)) yield return instruction; - yield return Instruction.Create(OpCodes.Newobj, context.Module.Import(ctorinforef)); - yield return Instruction.Create(OpCodes.Callvirt, context.Module.Import(setTypedBinding)); + yield return Instruction.Create(OpCodes.Newobj, context.Module.ImportReference(ctorinforef)); + yield return Instruction.Create(OpCodes.Callvirt, context.Module.ImportReference(setTypedBinding)); } static IList> ParsePath(string path, TypeReference tSourceRef, IXmlLineInfo lineInfo, ModuleDefinition module) @@ -431,7 +431,7 @@ namespace Xamarin.Forms.Build.Tasks previousPartTypeRef = property.PropertyType; } if (indexArg != null) { - var defaultMemberAttribute = previousPartTypeRef.GetCustomAttribute(module.Import(typeof(System.Reflection.DefaultMemberAttribute))); + var defaultMemberAttribute = previousPartTypeRef.GetCustomAttribute(module.ImportReference(typeof(System.Reflection.DefaultMemberAttribute))); var indexerName = defaultMemberAttribute?.ConstructorArguments?.FirstOrDefault().Value as string ?? "Item"; var indexer = previousPartTypeRef.GetProperty(pd => pd.Name == indexerName && pd.GetMethod != null && pd.GetMethod.IsPublic, out _); properties.Add(new Tuple(indexer, indexArg)); @@ -456,7 +456,7 @@ namespace Xamarin.Forms.Build.Tasks // } var module = context.Module; - var compilerGeneratedCtor = module.Import(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; + var compilerGeneratedCtor = module.ImportReference(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; var getter = new MethodDefinition($"<{context.Body.Method.Name}>typedBindingsM__{typedBindingCount++}", MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Static, tPropertyRef) { @@ -464,7 +464,7 @@ namespace Xamarin.Forms.Build.Tasks new ParameterDefinition(tSourceRef) }, CustomAttributes = { - new CustomAttribute (context.Module.Import(compilerGeneratedCtor)) + new CustomAttribute (context.Module.ImportReference(compilerGeneratedCtor)) } }; var il = getter.Body.GetILProcessor(); @@ -484,7 +484,7 @@ namespace Xamarin.Forms.Build.Tasks il.Emit(OpCodes.Ldc_I4, index); } } - il.Emit(OpCodes.Callvirt, module.Import(property.GetMethod)); + il.Emit(OpCodes.Callvirt, module.ImportReference(property.GetMethod)); } } @@ -492,9 +492,9 @@ namespace Xamarin.Forms.Build.Tasks context.Body.Method.DeclaringType.Methods.Add(getter); - var funcRef = module.Import(typeof(Func<,>)); - funcRef = module.Import(funcRef.MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); - var funcCtor = module.Import(funcRef.Resolve().GetConstructors().First()); + var funcRef = module.ImportReference(typeof(Func<,>)); + funcRef = module.ImportReference(funcRef.MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); + var funcCtor = module.ImportReference(funcRef.Resolve().GetConstructors().First()); funcCtor = funcCtor.MakeGeneric(funcRef, new [] { tSourceRef, tPropertyRef }); // IL_0007: ldnull @@ -503,7 +503,7 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldnull); yield return Instruction.Create(OpCodes.Ldftn, getter); - yield return Instruction.Create(OpCodes.Newobj, module.Import(funcCtor)); + yield return Instruction.Create(OpCodes.Newobj, module.ImportReference(funcCtor)); } static IEnumerable CompiledBindingGetSetter(TypeReference tSourceRef, TypeReference tPropertyRef, IList> properties, ElementNode node, ILContext context) @@ -525,7 +525,7 @@ namespace Xamarin.Forms.Build.Tasks // } var module = context.Module; - var compilerGeneratedCtor = module.Import(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; + var compilerGeneratedCtor = module.ImportReference(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; var setter = new MethodDefinition($"<{context.Body.Method.Name}>typedBindingsM__{typedBindingCount++}", MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Static, module.TypeSystem.Void) { @@ -534,7 +534,7 @@ namespace Xamarin.Forms.Build.Tasks new ParameterDefinition(tPropertyRef) }, CustomAttributes = { - new CustomAttribute (module.Import(compilerGeneratedCtor)) + new CustomAttribute (module.ImportReference(compilerGeneratedCtor)) } }; @@ -560,7 +560,7 @@ namespace Xamarin.Forms.Build.Tasks il.Emit(OpCodes.Ldc_I4, index); } } - il.Emit(OpCodes.Callvirt, module.Import(property.GetMethod)); + il.Emit(OpCodes.Callvirt, module.ImportReference(property.GetMethod)); } var indexer = properties.Last().Item2; @@ -575,14 +575,14 @@ namespace Xamarin.Forms.Build.Tasks } } il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Callvirt, module.Import(setterRef)); + il.Emit(OpCodes.Callvirt, module.ImportReference(setterRef)); il.Emit(OpCodes.Ret); context.Body.Method.DeclaringType.Methods.Add(setter); - var actionRef = module.Import(typeof(Action<,>)); - actionRef = module.Import(actionRef.MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); - var actionCtor = module.Import(actionRef.Resolve().GetConstructors().First()); + var actionRef = module.ImportReference(typeof(Action<,>)); + actionRef = module.ImportReference(actionRef.MakeGenericInstanceType(new [] { tSourceRef, tPropertyRef })); + var actionCtor = module.ImportReference(actionRef.Resolve().GetConstructors().First()); actionCtor = actionCtor.MakeGeneric(actionRef, new [] { tSourceRef, tPropertyRef }); // IL_0024: ldnull @@ -590,7 +590,7 @@ namespace Xamarin.Forms.Build.Tasks // IL_002b: newobj instance void class [mscorlib]System.Action`2::'.ctor'(object, native int) yield return Instruction.Create(OpCodes.Ldnull); yield return Instruction.Create(OpCodes.Ldftn, setter); - yield return Instruction.Create(OpCodes.Newobj, module.Import(actionCtor)); + yield return Instruction.Create(OpCodes.Newobj, module.ImportReference(actionCtor)); } static IEnumerable CompiledBindingGetHandlers(TypeReference tSourceRef, TypeReference tPropertyRef, IList> properties, ElementNode node, ILContext context) @@ -609,7 +609,7 @@ namespace Xamarin.Forms.Build.Tasks // } var module = context.Module; - var compilerGeneratedCtor = module.Import(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; + var compilerGeneratedCtor = module.ImportReference(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; var partGetters = new List(); if (properties == null || properties.Count == 0) { @@ -624,7 +624,7 @@ namespace Xamarin.Forms.Build.Tasks new ParameterDefinition(tSourceRef) }, CustomAttributes = { - new CustomAttribute (context.Module.Import(compilerGeneratedCtor)) + new CustomAttribute (context.Module.ImportReference(compilerGeneratedCtor)) } }; var il = partGetter.Body.GetILProcessor(); @@ -643,18 +643,18 @@ namespace Xamarin.Forms.Build.Tasks il.Emit(OpCodes.Ldc_I4, index); } } - il.Emit(OpCodes.Callvirt, module.Import(property.GetMethod)); + il.Emit(OpCodes.Callvirt, module.ImportReference(property.GetMethod)); } il.Emit(OpCodes.Ret); context.Body.Method.DeclaringType.Methods.Add(partGetter); partGetters.Add(partGetter); } - var funcObjRef = context.Module.Import(module.Import(typeof(Func<,>)).MakeGenericInstanceType(new [] { tSourceRef, module.TypeSystem.Object })); - var tupleRef = context.Module.Import(module.Import(typeof(Tuple<,>)).MakeGenericInstanceType(new [] { funcObjRef, module.TypeSystem.String })); - var funcCtor = module.Import(funcObjRef.Resolve().GetConstructors().First()); + var funcObjRef = context.Module.ImportReference(module.ImportReference(typeof(Func<,>)).MakeGenericInstanceType(new [] { tSourceRef, module.TypeSystem.Object })); + var tupleRef = context.Module.ImportReference(module.ImportReference(typeof(Tuple<,>)).MakeGenericInstanceType(new [] { funcObjRef, module.TypeSystem.String })); + var funcCtor = module.ImportReference(funcObjRef.Resolve().GetConstructors().First()); funcCtor = funcCtor.MakeGeneric(funcObjRef, new [] { tSourceRef, module.TypeSystem.Object }); - var tupleCtor = module.Import(tupleRef.Resolve().GetConstructors().First()); + var tupleCtor = module.ImportReference(tupleRef.Resolve().GetConstructors().First()); tupleCtor = tupleCtor.MakeGeneric(tupleRef, new [] { funcObjRef, module.TypeSystem.String}); // IL_003a: ldc.i4.2 @@ -686,9 +686,9 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldnull); yield return Instruction.Create(OpCodes.Ldftn, partGetters [i]); - yield return Instruction.Create(OpCodes.Newobj, module.Import(funcCtor)); + yield return Instruction.Create(OpCodes.Newobj, module.ImportReference(funcCtor)); yield return Instruction.Create(OpCodes.Ldstr, properties [i].Item1.Name); - yield return Instruction.Create(OpCodes.Newobj, module.Import(tupleCtor)); + yield return Instruction.Create(OpCodes.Newobj, module.ImportReference(tupleCtor)); yield return Instruction.Create(OpCodes.Stelem_Ref); } } @@ -740,8 +740,8 @@ namespace Xamarin.Forms.Build.Tasks fd.IsStatic && fd.IsPublic, out declaringTypeReference); if (bpRef != null) { - bpRef = module.Import(bpRef.ResolveGenericParameters(declaringTypeReference)); - bpRef.FieldType = module.Import(bpRef.FieldType); + bpRef = module.ImportReference(bpRef.ResolveGenericParameters(declaringTypeReference)); + bpRef.FieldType = module.ImportReference(bpRef.FieldType); } return bpRef; } @@ -791,10 +791,10 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldftn, handler); //FIXME: eventually get the right ctor instead fo the First() one, just in case another one could exists (not even sure it's possible). - var ctor = module.Import(eventinfo.EventType.Resolve().GetConstructors().First()); + var ctor = module.ImportReference(eventinfo.EventType.Resolve().GetConstructors().First()); ctor = ctor.ResolveGenericParameters(eventinfo.EventType, module); - yield return Instruction.Create(OpCodes.Newobj, module.Import(ctor)); - yield return Instruction.Create(OpCodes.Callvirt, module.Import(eventinfo.AddMethod)); + yield return Instruction.Create(OpCodes.Newobj, module.ImportReference(ctor)); + yield return Instruction.Create(OpCodes.Callvirt, module.ImportReference(eventinfo.AddMethod)); } static bool CanSetDynamicResource(FieldReference bpRef, INode valueNode, ILContext context) @@ -815,14 +815,14 @@ namespace Xamarin.Forms.Build.Tasks { var module = context.Body.Method.Module; var varValue = context.Variables [elementNode]; - var setDynamicResource = module.Import(typeof(IDynamicResourceHandler)).Resolve().Methods.First(m => m.Name == "SetDynamicResource"); + var setDynamicResource = module.ImportReference(typeof(IDynamicResourceHandler)).Resolve().Methods.First(m => m.Name == "SetDynamicResource"); var getKey = typeof(DynamicResource).GetProperty("Key").GetMethod; yield return Instruction.Create(OpCodes.Ldloc, parent); yield return Instruction.Create(OpCodes.Ldsfld, bpRef); yield return Instruction.Create(OpCodes.Ldloc, varValue); - yield return Instruction.Create(OpCodes.Callvirt, module.Import(getKey)); - yield return Instruction.Create(OpCodes.Callvirt, module.Import(setDynamicResource)); + yield return Instruction.Create(OpCodes.Callvirt, module.ImportReference(getKey)); + yield return Instruction.Create(OpCodes.Callvirt, module.ImportReference(setDynamicResource)); } static bool CanSetBinding(FieldReference bpRef, INode valueNode, ILContext context) @@ -838,18 +838,18 @@ namespace Xamarin.Forms.Build.Tasks VariableDefinition varValue; if (!context.Variables.TryGetValue(valueNode as IElementNode, out varValue)) return false; - var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(module.Import(typeof(BindingBase)), module); + var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(module.ImportReference(typeof(BindingBase)), module); if (implicitOperator != null) return true; - return varValue.VariableType.InheritsFromOrImplements(module.Import(typeof(BindingBase))); + return varValue.VariableType.InheritsFromOrImplements(module.ImportReference(typeof(BindingBase))); } static IEnumerable SetBinding(VariableDefinition parent, FieldReference bpRef, IElementNode elementNode, IXmlLineInfo iXmlLineInfo, ILContext context) { var module = context.Body.Method.Module; var varValue = context.Variables [elementNode]; - var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(module.Import(typeof(BindingBase)), module); + var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(module.ImportReference(typeof(BindingBase)), module); //TODO: check if parent is a BP var setBinding = typeof(BindableObject).GetMethod("SetBinding", new [] { typeof(BindableProperty), typeof(BindingBase) }); @@ -859,8 +859,8 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldloc, varValue); if (implicitOperator != null) // IL_000f: call !0 class [Xamarin.Forms.Core]Xamarin.Forms.OnPlatform`1::op_Implicit(class [Xamarin.Forms.Core]Xamarin.Forms.OnPlatform`1) - yield return Instruction.Create(OpCodes.Call, module.Import(implicitOperator)); - yield return Instruction.Create(OpCodes.Callvirt, module.Import(setBinding)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(implicitOperator)); + yield return Instruction.Create(OpCodes.Callvirt, module.ImportReference(setBinding)); } static bool CanSetValue(FieldReference bpRef, bool attached, INode node, IXmlLineInfo iXmlLineInfo, ILContext context) @@ -918,14 +918,14 @@ namespace Xamarin.Forms.Build.Tasks var implicitOperator = varDef.VariableType.GetImplicitOperatorTo(bpTypeRef, module); yield return Instruction.Create(OpCodes.Ldloc, varDef); if (implicitOperator != null) { - yield return Instruction.Create(OpCodes.Call, module.Import(implicitOperator)); - varType = module.Import(bpTypeRef); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(implicitOperator)); + varType = module.ImportReference(bpTypeRef); } if (varType.IsValueType) yield return Instruction.Create(OpCodes.Box, varType); } - yield return Instruction.Create(OpCodes.Callvirt, module.Import(setValue)); + yield return Instruction.Create(OpCodes.Callvirt, module.ImportReference(setValue)); } static bool CanSet(VariableDefinition parent, string localName, INode node, ILContext context) @@ -975,8 +975,8 @@ namespace Xamarin.Forms.Build.Tasks // IL_0008: ldstr "foo" // IL_000d: callvirt instance void class [Xamarin.Forms.Core]Xamarin.Forms.Label::set_Text(string) - module.Import(parent.VariableType.Resolve()); - var propertySetterRef = module.Import(module.Import(propertySetter).ResolveGenericParameters(declaringTypeReference, module)); + module.ImportReference(parent.VariableType.Resolve()); + var propertySetterRef = module.ImportReference(module.ImportReference(propertySetter).ResolveGenericParameters(declaringTypeReference, module)); propertySetterRef.ImportTypes(module); var propertyType = property.ResolveGenericPropertyType(declaringTypeReference); var valueNode = node as ValueNode; @@ -1001,9 +1001,9 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldloc, vardef); if (implicitOperator != null) { // IL_000f: call !0 class [Xamarin.Forms.Core]Xamarin.Forms.OnPlatform`1::op_Implicit(class [Xamarin.Forms.Core]Xamarin.Forms.OnPlatform`1) - yield return Instruction.Create(OpCodes.Call, module.Import(implicitOperator)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(implicitOperator)); } else if (!vardef.VariableType.IsValueType && propertyType.IsValueType) - yield return Instruction.Create(OpCodes.Unbox_Any, module.Import(propertyType)); + yield return Instruction.Create(OpCodes.Unbox_Any, module.ImportReference(propertyType)); else if (vardef.VariableType.IsValueType && propertyType.FullName == "System.Object") yield return Instruction.Create(OpCodes.Box, vardef.VariableType); if (parent.VariableType.IsValueType) @@ -1028,8 +1028,8 @@ namespace Xamarin.Forms.Build.Tasks return false; var vardef = context.Variables [elementNode]; - var propertyGetterRef = module.Import(propertyGetter); - propertyGetterRef = module.Import(propertyGetterRef.ResolveGenericParameters(declaringTypeReference, module)); + var propertyGetterRef = module.ImportReference(propertyGetter); + propertyGetterRef = module.ImportReference(propertyGetterRef.ResolveGenericParameters(declaringTypeReference, module)); var propertyType = propertyGetterRef.ReturnType.ResolveGenericParameters(declaringTypeReference); //TODO check md.Parameters[0] type @@ -1048,13 +1048,13 @@ namespace Xamarin.Forms.Build.Tasks var propertyGetter = property.GetMethod; var elementNode = node as IElementNode; var vardef = context.Variables [elementNode]; - var propertyGetterRef = module.Import(propertyGetter); - propertyGetterRef = module.Import(propertyGetterRef.ResolveGenericParameters(declaringTypeReference, module)); + var propertyGetterRef = module.ImportReference(propertyGetter); + propertyGetterRef = module.ImportReference(propertyGetterRef.ResolveGenericParameters(declaringTypeReference, module)); var propertyType = propertyGetterRef.ReturnType.ResolveGenericParameters(declaringTypeReference); //TODO check md.Parameters[0] type var adderTuple = propertyType.GetMethods(md => md.Name == "Add" && md.Parameters.Count == 1, module).FirstOrDefault(); - var adderRef = module.Import(adderTuple.Item1); - adderRef = module.Import(adderRef.ResolveGenericParameters(adderTuple.Item2, module)); + var adderRef = module.ImportReference(adderTuple.Item1); + adderRef = module.ImportReference(adderRef.ResolveGenericParameters(adderTuple.Item2, module)); var childType = GetParameterType(adderRef.Parameters [0]); var implicitOperator = vardef.VariableType.GetImplicitOperatorTo(childType, module); @@ -1062,7 +1062,7 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Callvirt, propertyGetterRef); yield return Instruction.Create(OpCodes.Ldloc, vardef); if (implicitOperator != null) - yield return Instruction.Create(OpCodes.Call, module.Import(implicitOperator)); + yield return Instruction.Create(OpCodes.Call, module.ImportReference(implicitOperator)); if (implicitOperator == null && vardef.VariableType.IsValueType && !childType.IsValueType) yield return Instruction.Create(OpCodes.Box, vardef.VariableType); yield return Instruction.Create(OpCodes.Callvirt, adderRef); @@ -1105,7 +1105,7 @@ namespace Xamarin.Forms.Build.Tasks var module = parentContext.Module; - var compilerGeneratedCtor = module.Import(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; + var compilerGeneratedCtor = module.ImportReference(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)).GetMethods(md => md.IsConstructor, module).First().Item1; var anonType = new TypeDefinition( null, "<" + parentContext.Body.Method.Name + ">_anonXamlCDataTemplate_" + dtcount++, @@ -1115,7 +1115,7 @@ namespace Xamarin.Forms.Build.Tasks { BaseType = module.TypeSystem.Object, CustomAttributes = { - new CustomAttribute (module.Import(compilerGeneratedCtor)) + new CustomAttribute (module.ImportReference(compilerGeneratedCtor)) } }; @@ -1127,7 +1127,7 @@ namespace Xamarin.Forms.Build.Tasks module.TypeSystem.Object); anonType.Methods.Add(loadTemplate); - var parentValues = new FieldDefinition("parentValues", FieldAttributes.Assembly, module.Import(typeof (object[]))); + var parentValues = new FieldDefinition("parentValues", FieldAttributes.Assembly, module.ImportReference(typeof (object[]))); anonType.Fields.Add(parentValues); TypeReference rootType = null; @@ -1179,19 +1179,19 @@ namespace Xamarin.Forms.Build.Tasks //SetDataTemplate parentIl.Emit(OpCodes.Ldftn, loadTemplate); - var funcObjRef = module.Import(typeof(Func)); + var funcObjRef = module.ImportReference(typeof(Func)); var funcCtor = funcObjRef .Resolve() .Methods.First(md => md.IsConstructor && md.Parameters.Count == 2) .ResolveGenericParameters(funcObjRef, module); - parentIl.Emit(OpCodes.Newobj, module.Import(funcCtor)); + parentIl.Emit(OpCodes.Newobj, module.ImportReference(funcCtor)); #pragma warning disable 0612 var propertySetter = - module.Import(typeof (IDataTemplate)).Resolve().Properties.First(p => p.Name == "LoadTemplate").SetMethod; + module.ImportReference(typeof (IDataTemplate)).Resolve().Properties.First(p => p.Name == "LoadTemplate").SetMethod; #pragma warning restore 0612 - parentContext.IL.Emit(OpCodes.Callvirt, module.Import(propertySetter)); + parentContext.IL.Emit(OpCodes.Callvirt, module.ImportReference(propertySetter)); loadTemplate.Body.Optimize(); } diff --git a/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs index 4c2fc05cd..c5be1fbd1 100644 --- a/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetResourcesVisitor.cs @@ -59,7 +59,7 @@ namespace Xamarin.Forms.Build.Tasks { // Collection element, implicit content, or implicit collection element. var parentVar = Context.Variables[(IElementNode)parentNode]; - if (parentVar.VariableType.ImplementsInterface(Module.Import(typeof (IEnumerable)))) + if (parentVar.VariableType.ImplementsInterface(Module.ImportReference(typeof (IEnumerable)))) { if ((parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" || parentVar.VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary") && @@ -84,8 +84,8 @@ namespace Xamarin.Forms.Build.Tasks Context.IL.Emit(OpCodes.Ldloc, parentVar); Context.IL.Emit(OpCodes.Ldloc, Context.Variables[node]); Context.IL.Emit(OpCodes.Callvirt, - Module.Import( - Module.Import(typeof (ResourceDictionary)) + Module.ImportReference( + Module.ImportReference(typeof (ResourceDictionary)) .Resolve() .Methods.Single(md => md.Name == "Add" && md.Parameters.Count == 1))); } @@ -115,10 +115,10 @@ namespace Xamarin.Forms.Build.Tasks var varDef = Context.Variables[node]; Context.IL.Emit(OpCodes.Ldloc, varDef); if (varDef.VariableType.IsValueType) - Context.IL.Emit(OpCodes.Box, Module.Import(varDef.VariableType)); + Context.IL.Emit(OpCodes.Box, Module.ImportReference(varDef.VariableType)); Context.IL.Emit(OpCodes.Callvirt, - Module.Import( - Module.Import(typeof (ResourceDictionary)) + Module.ImportReference( + Module.ImportReference(typeof (ResourceDictionary)) .Resolve() .Methods.Single(md => md.Name == "Add" && md.Parameters.Count == 2))); } diff --git a/Xamarin.Forms.Build.Tasks/TypeDefinitionExtensions.cs b/Xamarin.Forms.Build.Tasks/TypeDefinitionExtensions.cs index 51b3fdbf2..bd0b4b49a 100644 --- a/Xamarin.Forms.Build.Tasks/TypeDefinitionExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/TypeDefinitionExtensions.cs @@ -20,7 +20,7 @@ namespace Xamarin.Forms.Build.Tasks public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType, Type parentType) { var module = targetType.Module; - var voidType = module.Import(typeof (void)); + var voidType = module.ImportReference(typeof (void)); var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | @@ -35,7 +35,7 @@ namespace Xamarin.Forms.Build.Tasks if (objectConstructor == null) objectConstructor = typeof (object).GetConstructor(new Type[0]); - var baseConstructor = module.Import(objectConstructor); + var baseConstructor = module.ImportReference(objectConstructor); var ctor = new MethodDefinition(".ctor", methodAttributes, voidType) { diff --git a/Xamarin.Forms.Build.Tasks/TypeReferenceExtensions.cs b/Xamarin.Forms.Build.Tasks/TypeReferenceExtensions.cs index a347c2ff9..3b69bfd5d 100644 --- a/Xamarin.Forms.Build.Tasks/TypeReferenceExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/TypeReferenceExtensions.cs @@ -85,7 +85,7 @@ namespace Xamarin.Forms.Build.Tasks public static bool ImplementsInterface(this TypeReference typeRef, TypeReference @interface) { var typeDef = typeRef.Resolve(); - if (typeDef.Interfaces.Any(tr => tr.FullName == @interface.FullName)) + if (typeDef.Interfaces.Any(tr => tr.InterfaceType.FullName == @interface.FullName)) return true; var baseTypeRef = typeDef.BaseType; if (baseTypeRef != null && baseTypeRef.FullName != "System.Object") @@ -99,13 +99,13 @@ namespace Xamarin.Forms.Build.Tasks interfaceReference = null; genericArguments = null; var typeDef = typeRef.Resolve(); - TypeReference iface; + InterfaceImplementation iface; if ((iface = typeDef.Interfaces.FirstOrDefault(tr => - tr.FullName.StartsWith(@interface, StringComparison.Ordinal) && - tr.IsGenericInstance && (tr as GenericInstanceType).HasGenericArguments)) != null) + tr.InterfaceType.FullName.StartsWith(@interface, StringComparison.Ordinal) && + tr.InterfaceType.IsGenericInstance && (tr.InterfaceType as GenericInstanceType).HasGenericArguments)) != null) { - interfaceReference = iface as GenericInstanceType; - genericArguments = (iface as GenericInstanceType).GenericArguments; + interfaceReference = iface.InterfaceType as GenericInstanceType; + genericArguments = (iface.InterfaceType as GenericInstanceType).GenericArguments; return true; } var baseTypeRef = typeDef.BaseType; @@ -157,7 +157,7 @@ namespace Xamarin.Forms.Build.Tasks var typeDef = typeRef.Resolve(); if (TypeRefComparer.Default.Equals(typeDef, baseClass.Resolve())) return true; - if (typeDef.Interfaces.Any(ir => TypeRefComparer.Default.Equals(ir, baseClass))) + if (typeDef.Interfaces.Any(ir => TypeRefComparer.Default.Equals(ir.InterfaceType, baseClass))) return true; if (typeDef.BaseType == null) return false; @@ -201,7 +201,7 @@ namespace Xamarin.Forms.Build.Tasks { foreach (var face in typeDef.Interfaces) { - var m = face.GetMethod(predicate); + var m = face.InterfaceType.GetMethod(predicate); if (m != null) return m; } @@ -226,13 +226,13 @@ namespace Xamarin.Forms.Build.Tasks { foreach (var face in typeDef.Interfaces) { - if (face.IsGenericInstance && typeRef is GenericInstanceType) + if (face.InterfaceType.IsGenericInstance && typeRef is GenericInstanceType) { int i = 0; foreach (var arg in ((GenericInstanceType)typeRef).GenericArguments) - ((GenericInstanceType)face).GenericArguments[i++] = module.Import(arg); + ((GenericInstanceType)face.InterfaceType).GenericArguments[i++] = module.ImportReference(arg); } - foreach (var tuple in face.GetMethods(predicate, module)) + foreach (var tuple in face.InterfaceType.GetMethods(predicate, module)) yield return tuple; } yield break; @@ -253,7 +253,7 @@ namespace Xamarin.Forms.Build.Tasks foreach (var op in implicitOperators) { var cast = op.Item1; var opDeclTypeRef = op.Item2; - var castDef = module.Import(cast).ResolveGenericParameters(opDeclTypeRef, module); + var castDef = module.ImportReference(cast).ResolveGenericParameters(opDeclTypeRef, module); var returnType = castDef.ReturnType; if (returnType.IsGenericParameter) returnType = ((GenericInstanceType)opDeclTypeRef).GenericArguments [((GenericParameter)returnType).Position]; diff --git a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj index 173473d0a..c1c62eff5 100644 --- a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj +++ b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj @@ -52,16 +52,16 @@ - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.Mdb.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.Pdb.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Rocks.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.Rocks.dll @@ -118,9 +118,6 @@ - - - {9DB2F292-8034-4E06-89AD-98BBDA4306B9} @@ -145,4 +142,7 @@ + + + diff --git a/Xamarin.Forms.Build.Tasks/XamlCTask.cs b/Xamarin.Forms.Build.Tasks/XamlCTask.cs index 4e2029c4a..051eac0fb 100644 --- a/Xamarin.Forms.Build.Tasks/XamlCTask.cs +++ b/Xamarin.Forms.Build.Tasks/XamlCTask.cs @@ -2,9 +2,12 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; + using Mono.Cecil; using Mono.Cecil.Cil; -using Mono.Cecil.Rocks; +using Mono.Cecil.Mdb; +using Mono.Cecil.Pdb; + using Xamarin.Forms.Xaml; namespace Xamarin.Forms.Build.Tasks @@ -23,9 +26,12 @@ namespace Xamarin.Forms.Build.Tasks } internal string Type { get; set; } + internal MethodDefinition InitCompForType { get; private set; } + internal bool ReadOnly { get; set; } - public override bool Execute(IList thrownExceptions) + public override bool Execute(out IList thrownExceptions) { + thrownExceptions = null; Logger = Logger ?? new Logger(null, Verbosity); Logger.LogLine(1, "Compiling Xaml"); Logger.LogLine(1, "\nAssembly: {0}", Assembly); @@ -64,175 +70,175 @@ namespace Xamarin.Forms.Build.Tasks } } - var assemblyDefinition = AssemblyDefinition.ReadAssembly(Path.GetFullPath(Assembly), new ReaderParameters - { + var readerParameters = new ReaderParameters { AssemblyResolver = resolver, - ReadSymbols = DebugSymbols - }); + ReadWrite = !ReadOnly, + ReadSymbols = DebugSymbols, + SymbolReaderProvider = DebugSymbols ? (System.Type.GetType("Mono.Runtime") != null ? ((ISymbolReaderProvider)(new MdbReaderProvider())) : ((ISymbolReaderProvider)new PdbReaderProvider())) : null, + }; - CustomAttribute xamlcAttr; - if (assemblyDefinition.HasCustomAttributes && - (xamlcAttr = - assemblyDefinition.CustomAttributes.FirstOrDefault( - ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) - { - var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; - if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) - skipassembly = true; - if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) - skipassembly = false; - } - - foreach (var module in assemblyDefinition.Modules) - { - var skipmodule = skipassembly; - if (module.HasCustomAttributes && - (xamlcAttr = - module.CustomAttributes.FirstOrDefault( - ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) - { + using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(Path.GetFullPath(Assembly),readerParameters)) { + CustomAttribute xamlcAttr; + if (assemblyDefinition.HasCustomAttributes && + (xamlcAttr = + assemblyDefinition.CustomAttributes.FirstOrDefault( + ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) { var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) - skipmodule = true; + skipassembly = true; if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) - skipmodule = false; + skipassembly = false; } - Logger.LogLine(2, " Module: {0}", module.Name); - var resourcesToPrune = new List(); - foreach (var resource in module.Resources.OfType()) - { - Logger.LogString(2, " Resource: {0}... ", resource.Name); - string classname; - if (!resource.IsXaml(out classname)) - { - Logger.LogLine(2, "skipped."); - continue; - } - TypeDefinition typeDef = module.GetType(classname); - if (typeDef == null) - { - Logger.LogLine(2, "no type found... skipped."); - continue; - } - var skiptype = skipmodule; - if (typeDef.HasCustomAttributes && - (xamlcAttr = - typeDef.CustomAttributes.FirstOrDefault( - ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) - { + foreach (var module in assemblyDefinition.Modules) { + var skipmodule = skipassembly; + if (module.HasCustomAttributes && + (xamlcAttr = + module.CustomAttributes.FirstOrDefault( + ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) { var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) - skiptype = true; + skipmodule = true; if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) - skiptype = false; + skipmodule = false; } - if (Type != null) - skiptype = !(Type == classname); + Logger.LogLine(2, " Module: {0}", module.Name); + var resourcesToPrune = new List(); + foreach (var resource in module.Resources.OfType()) { + Logger.LogString(2, " Resource: {0}... ", resource.Name); + string classname; + if (!resource.IsXaml(out classname)) { + Logger.LogLine(2, "skipped."); + continue; + } + TypeDefinition typeDef = module.GetType(classname); + if (typeDef == null) { + Logger.LogLine(2, "no type found... skipped."); + continue; + } + var skiptype = skipmodule; + if (typeDef.HasCustomAttributes && + (xamlcAttr = + typeDef.CustomAttributes.FirstOrDefault( + ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) { + var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; + if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) + skiptype = true; + if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) + skiptype = false; + } - if (skiptype) - { - Logger.LogLine(2, "Has XamlCompilationAttribute set to Skip and not Compile... skipped"); - continue; - } + if (Type != null) + skiptype = !(Type == classname); - var initComp = typeDef.Methods.FirstOrDefault(md => md.Name == "InitializeComponent"); - if (initComp == null) - { - Logger.LogLine(2, "no InitializeComponent found... skipped."); - continue; - } - Logger.LogLine(2, ""); + if (skiptype) { + Logger.LogLine(2, "Has XamlCompilationAttribute set to Skip and not Compile... skipped"); + continue; + } - CustomAttribute xamlFilePathAttr; - var xamlFilePath = typeDef.HasCustomAttributes && (xamlFilePathAttr = typeDef.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlFilePathAttribute")) != null ? - (string)xamlFilePathAttr.ConstructorArguments [0].Value : - resource.Name; + var initComp = typeDef.Methods.FirstOrDefault(md => md.Name == "InitializeComponent"); + if (initComp == null) { + Logger.LogLine(2, "no InitializeComponent found... skipped."); + continue; + } + Logger.LogLine(2, ""); - var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime"); - if (initCompRuntime != null) - Logger.LogLine(2, " __InitComponentRuntime already exists... not duplicating"); - else { - Logger.LogString(2, " Duplicating {0}.InitializeComponent () into {0}.__InitComponentRuntime ... ", typeDef.Name); - initCompRuntime = DuplicateMethodDef(typeDef, initComp, "__InitComponentRuntime"); + CustomAttribute xamlFilePathAttr; + var xamlFilePath = typeDef.HasCustomAttributes && (xamlFilePathAttr = typeDef.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlFilePathAttribute")) != null ? + (string)xamlFilePathAttr.ConstructorArguments[0].Value : + resource.Name; + + var initCompRuntime = typeDef.Methods.FirstOrDefault(md => md.Name == "__InitComponentRuntime"); + if (initCompRuntime != null) + Logger.LogLine(2, " __InitComponentRuntime already exists... not creating"); + else { + Logger.LogString(2, " Creating empty {0}.__InitComponentRuntime ...", typeDef.Name); + initCompRuntime = new MethodDefinition("__InitComponentRuntime", initComp.Attributes, initComp.ReturnType); + Logger.LogLine(2, "done."); + Logger.LogString(2, " Copying body of InitializeComponent to __InitComponentRuntime ...", typeDef.Name); + initCompRuntime.Body = new MethodBody(initCompRuntime); + var iCRIl = initCompRuntime.Body.GetILProcessor(); + foreach (var instr in initComp.Body.Instructions) + iCRIl.Append(instr); + initComp.Body.Instructions.Clear(); + initComp.Body.GetILProcessor().Emit(OpCodes.Ret); + typeDef.Methods.Add(initCompRuntime); + Logger.LogLine(2, "done."); + } + + Logger.LogString(2, " Parsing Xaml... "); + var rootnode = ParseXaml(resource.GetResourceStream(), typeDef); + if (rootnode == null) { + Logger.LogLine(2, "failed."); + continue; + } Logger.LogLine(2, "done."); + + hasCompiledXamlResources = true; + + Logger.LogString(2, " Replacing {0}.InitializeComponent ()... ", typeDef.Name); + Exception e; + if (!TryCoreCompile(initComp, initCompRuntime, rootnode, out e)) { + success = false; + Logger.LogLine(2, "failed."); + (thrownExceptions = thrownExceptions ?? new List()).Add(e); + Logger.LogException(null, null, null, xamlFilePath, e); + Logger.LogLine(4, e.StackTrace); + continue; + } + if (Type != null) + InitCompForType = initComp; + + Logger.LogLine(2, "done."); + + if (OptimizeIL) { + Logger.LogString(2, " Optimizing IL... "); + initComp.Body.Optimize(); + Logger.LogLine(2, "done"); + } + + if (outputGeneratedILAsCode) + Logger.LogLine(2, " Decompiling option has been removed. Use a 3rd party decompiler to admire the beauty of the IL generated"); + + resourcesToPrune.Add(resource); + } + if (!KeepXamlResources) { + if (resourcesToPrune.Any()) + Logger.LogLine(2, " Removing compiled xaml resources"); + foreach (var resource in resourcesToPrune) { + Logger.LogString(2, " Removing {0}... ", resource.Name); + module.Resources.Remove(resource); + Logger.LogLine(2, "done"); + } } - Logger.LogString(2, " Parsing Xaml... "); - var rootnode = ParseXaml(resource.GetResourceStream(), typeDef); - if (rootnode == null) - { - Logger.LogLine(2, "failed."); - continue; - } - Logger.LogLine(2, "done."); - - hasCompiledXamlResources = true; - - Logger.LogString(2, " Replacing {0}.InitializeComponent ()... ", typeDef.Name); - Exception e; - if (!TryCoreCompile(initComp, initCompRuntime, rootnode, out e)) { - success = false; - Logger.LogLine(2, "failed."); - thrownExceptions?.Add(e); - Logger.LogException(null, null, null, xamlFilePath, e); - Logger.LogLine(4, e.StackTrace); - continue; - } - Logger.LogLine(2, "done."); - - if (OptimizeIL) - { - Logger.LogString(2, " Optimizing IL... "); - initComp.Body.Optimize(); - Logger.LogLine(2, "done"); - } - - if (outputGeneratedILAsCode) - Logger.LogLine(2, " Decompiling option has been removed. Use a 3rd party decompiler to admire the beauty of the IL generated"); - - resourcesToPrune.Add(resource); - } - if (!KeepXamlResources) - { - if (resourcesToPrune.Any()) - Logger.LogLine(2, " Removing compiled xaml resources"); - foreach (var resource in resourcesToPrune) - { - Logger.LogString(2, " Removing {0}... ", resource.Name); - module.Resources.Remove(resource); - Logger.LogLine(2, "done"); - } + Logger.LogLine(2, ""); } - Logger.LogLine(2, ""); - } + if (!hasCompiledXamlResources) { + Logger.LogLine(1, "No compiled resources. Skipping writing assembly."); + return success; + } - if (!hasCompiledXamlResources) - { - Logger.LogLine(1, "No compiled resources. Skipping writing assembly."); - return success; + if (ReadOnly) + return success; + + Logger.LogString(1, "Writing the assembly... "); + try { + assemblyDefinition.Write(new WriterParameters { + WriteSymbols = DebugSymbols, + SymbolWriterProvider = DebugSymbols ? (System.Type.GetType("Mono.Runtime") != null ? ((ISymbolWriterProvider)(new MdbWriterProvider())) : ((ISymbolWriterProvider)new MdbWriterProvider())): null, + }); + Logger.LogLine(1, "done."); + } catch (Exception e) { + Logger.LogLine(1, "failed."); + Logger.LogException(null, null, null, null, e); + (thrownExceptions = thrownExceptions ?? new List()).Add(e); + Logger.LogLine(4, e.StackTrace); + success = false; + } } - - Logger.LogString(1, "Writing the assembly... "); - try - { - assemblyDefinition.Write(Assembly, new WriterParameters - { - WriteSymbols = DebugSymbols - }); - Logger.LogLine(1, "done."); - } - catch (Exception e) - { - Logger.LogLine(1, "failed."); - Logger.LogException(null, null, null, null, e); - thrownExceptions?.Add(e); - Logger.LogLine(4, e.StackTrace); - success = false; - } - return success; } @@ -258,7 +264,8 @@ namespace Xamarin.Forms.Build.Tasks // IL_0031: nop var nop = Instruction.Create(OpCodes.Nop); - var getXamlFileProvider = body.Method.Module.Import(body.Method.Module.Import(typeof(Xamarin.Forms.Xaml.Internals.XamlLoader)) + + var getXamlFileProvider = body.Method.Module.ImportReference(body.Method.Module.ImportReference(typeof(Xamarin.Forms.Xaml.Internals.XamlLoader)) .Resolve() .Properties.FirstOrDefault(pd => pd.Name == "XamlFileProvider") .GetMethod); @@ -266,14 +273,14 @@ namespace Xamarin.Forms.Build.Tasks il.Emit(OpCodes.Brfalse, nop); il.Emit(OpCodes.Call, getXamlFileProvider); il.Emit(OpCodes.Ldarg_0); - var getType = body.Method.Module.Import(body.Method.Module.Import(typeof(object)) + var getType = body.Method.Module.ImportReference(body.Method.Module.ImportReference(typeof(object)) .Resolve() .Methods.FirstOrDefault(md => md.Name == "GetType")); il.Emit(OpCodes.Call, getType); - var func = body.Method.Module.Import(body.Method.Module.Import(typeof(Func)) + var func = body.Method.Module.ImportReference(body.Method.Module.ImportReference(typeof(Func)) .Resolve() .Methods.FirstOrDefault(md => md.Name == "Invoke")); - func = func.ResolveGenericParameters(body.Method.Module.Import(typeof(Func)), body.Method.Module); + func = func.ResolveGenericParameters(body.Method.Module.ImportReference(typeof(Func)), body.Method.Module); il.Emit(OpCodes.Callvirt, func); il.Emit(OpCodes.Brfalse, nop); il.Emit(OpCodes.Ldarg_0); diff --git a/Xamarin.Forms.Build.Tasks/XamlTask.cs b/Xamarin.Forms.Build.Tasks/XamlTask.cs index d4864ac3a..0456d9f3f 100644 --- a/Xamarin.Forms.Build.Tasks/XamlTask.cs +++ b/Xamarin.Forms.Build.Tasks/XamlTask.cs @@ -10,6 +10,7 @@ using Microsoft.Build.Utilities; using Mono.Cecil; using Xamarin.Forms.Xaml; +using Mono.Cecil.Cil; namespace Xamarin.Forms.Build.Tasks { @@ -31,18 +32,11 @@ namespace Xamarin.Forms.Build.Tasks public override bool Execute() { Logger = new Logger(Log, Verbosity); - return Execute(null); + IList _; + return Execute(out _); } - public abstract bool Execute(IList thrownExceptions); - - protected static MethodDefinition DuplicateMethodDef(TypeDefinition typeDef, MethodDefinition methodDef, string newName) - { - var dup = new MethodDefinition(newName, methodDef.Attributes, methodDef.ReturnType); - dup.Body = methodDef.Body; - typeDef.Methods.Add(dup); - return dup; - } + public abstract bool Execute(out IList thrownExceptions); internal static ILRootNode ParseXaml(Stream stream, TypeReference typeReference) { diff --git a/Xamarin.Forms.Build.Tasks/XmlTypeExtensions.cs b/Xamarin.Forms.Build.Tasks/XmlTypeExtensions.cs index 7682ec860..59b72acc9 100644 --- a/Xamarin.Forms.Build.Tasks/XmlTypeExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/XmlTypeExtensions.cs @@ -91,7 +91,7 @@ namespace Xamarin.Forms.Build.Tasks var assemblydefinition = module.Assembly.Name.Name == asm.AssemblyName ? module.Assembly : - module.AssemblyResolver.Resolve(asm.AssemblyName); + module.AssemblyResolver.Resolve(AssemblyNameReference.Parse(asm.AssemblyName)); type = assemblydefinition.MainModule.GetType(asm.ClrNamespace, name); if (type == null) @@ -108,14 +108,14 @@ namespace Xamarin.Forms.Build.Tasks if (type != null && typeArguments != null && type.HasGenericParameters) { type = - module.Import(type) + module.ImportReference(type) .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, module, xmlInfo)).ToArray()); } if (type == null) throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo); - return module.Import(type); + return module.ImportReference(type); } } } \ No newline at end of file diff --git a/Xamarin.Forms.Build.Tasks/packages.config b/Xamarin.Forms.Build.Tasks/packages.config index 0bc4be162..a37a1b2bd 100644 --- a/Xamarin.Forms.Build.Tasks/packages.config +++ b/Xamarin.Forms.Build.Tasks/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting.xaml b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting.xaml new file mode 100644 index 000000000..d008ed5ee --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting.xaml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting.xaml.cs new file mode 100644 index 000000000..250eed724 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting.xaml.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using Mono.Cecil; +using Mono.Cecil.Cil; +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; +using Xamarin.Forms.Xaml; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + [XamlCompilation(XamlCompilationOptions.Skip)] + public partial class DefaultCtorRouting : ContentPage + { + [TypeConverter(typeof(IsCompiledTypeConverter))] + public bool IsCompiled { get; set; } + + public DefaultCtorRouting() + { + InitializeComponent(); + } + + [TestFixture] + class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + + [Test] + public void ShouldntBeCompiled() + { + var p = new DefaultCtorRouting(); + Assert.False(p.IsCompiled); + } + } + } + + [ProvideCompiled("Xamarin.Forms.Core.XamlC.IsCompiledTypeConverter")] + class IsCompiledTypeConverter : TypeConverter, ICompiledTypeConverter + { + public override object ConvertFromInvariantString(string value) + { + if (value != "IsCompiled?") + throw new Exception(); + return false; + } + + public IEnumerable ConvertFromString(string value, ModuleDefinition module, BaseNode node) + { + if (value != "IsCompiled?") + throw new Exception(); + yield return Instruction.Create(OpCodes.Ldc_I4_1); + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting2.xaml b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting2.xaml new file mode 100644 index 000000000..f2ccba8bb --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting2.xaml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting2.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting2.xaml.cs new file mode 100644 index 000000000..052547709 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/DefaultCtorRouting2.xaml.cs @@ -0,0 +1,57 @@ +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class DefaultCtorRouting2 : ContentPage + { + [TypeConverter(typeof(IsCompiledTypeConverter))] + public bool IsCompiled { get; set; } + + public DefaultCtorRouting2() + { + InitializeComponent(); + } + + [TestFixture] + class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + Internals.XamlLoader.XamlFileProvider = null; + } + + [Test] + public void ShouldBeCompiled() + { + var p = new DefaultCtorRouting2(); + Assert.True(p.IsCompiled); + } + + [Test] + public void ShouldntBeCompiled() + { + Internals.XamlLoader.XamlFileProvider = (t) => { + if (t == typeof(DefaultCtorRouting2)) + return @" + +"; + return null; + }; + var p = new DefaultCtorRouting2(); + Assert.False(p.IsCompiled); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingCtor.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingCtor.xaml.cs index 6771edf69..919d654bb 100644 --- a/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingCtor.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingCtor.xaml.cs @@ -12,6 +12,11 @@ namespace Xamarin.Forms.Xaml.UnitTests InitializeComponent(); } + public FactoryMethodMissingCtor(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + [TestFixture] public class Tests { @@ -28,7 +33,7 @@ namespace Xamarin.Forms.Xaml.UnitTests if (useCompiledXaml) Assert.Throws(new XamlParseExceptionConstraint(7, 4), () => MockCompiler.Compile(typeof(FactoryMethodMissingCtor))); else - Assert.Throws(() => new FactoryMethodMissingCtor()); + Assert.Throws(() => new FactoryMethodMissingCtor(useCompiledXaml)); } } } diff --git a/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingMethod.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingMethod.xaml.cs index ecc404870..962070f30 100644 --- a/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingMethod.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/FactoryMethodMissingMethod.xaml.cs @@ -12,6 +12,11 @@ namespace Xamarin.Forms.Xaml.UnitTests InitializeComponent(); } + public FactoryMethodMissingMethod(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + [TestFixture] public class Tests { @@ -28,7 +33,7 @@ namespace Xamarin.Forms.Xaml.UnitTests if (useCompiledXaml) Assert.Throws(new XamlParseExceptionConstraint(8, 4), () => MockCompiler.Compile(typeof(FactoryMethodMissingMethod))); else - Assert.Throws(() => new FactoryMethodMissingMethod()); + Assert.Throws(() => new FactoryMethodMissingMethod(useCompiledXaml)); } } } diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43450.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43450.xaml.cs index b3346b37f..6c0c42f5f 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43450.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz43450.xaml.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; namespace Xamarin.Forms.Xaml.UnitTests { @@ -18,6 +19,18 @@ namespace Xamarin.Forms.Xaml.UnitTests [TestFixture] class Tests { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + [TestCase(true)] [TestCase(false)] public void DoesNotAllowGridRowDefinition(bool useCompiledXaml) diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported008.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported008.xaml.cs index d5f84d414..6e5dfc5e5 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported008.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported008.xaml.cs @@ -4,7 +4,6 @@ using Xamarin.Forms.Core.UnitTests; namespace Xamarin.Forms.Xaml.UnitTests { - [XamlCompilation(XamlCompilationOptions.Skip)] public partial class Unreported008 : ContentPage { public Unreported008() diff --git a/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs b/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs index 6e63f6a4e..79f840c5f 100644 --- a/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs +++ b/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Xamarin.Forms.Build.Tasks; +using Mono.Cecil; namespace Xamarin.Forms.Xaml.UnitTests { @@ -10,6 +11,13 @@ namespace Xamarin.Forms.Xaml.UnitTests { public static void Compile(Type type) { + MethodDefinition _; + Compile(type, out _); + } + + public static void Compile(Type type, out MethodDefinition methdoDefinition) + { + methdoDefinition = null; var assembly = type.Assembly.Location; var refs = from an in type.Assembly.GetReferencedAssemblies() let a = System.Reflection.Assembly.Load(an) @@ -20,12 +28,19 @@ namespace Xamarin.Forms.Xaml.UnitTests ReferencePath = string.Join(";", refs), KeepXamlResources = true, OptimizeIL = true, + DebugSymbols = false, + ReadOnly = true, Type = type.FullName }; - var exceptions = new List(); - if (!xamlc.Execute(exceptions) && exceptions.Any()) - throw exceptions [0]; + IList exceptions; + if (xamlc.Execute(out exceptions) || exceptions == null || !exceptions.Any()) { + methdoDefinition = xamlc.InitCompForType; + return; + } + if (exceptions.Count > 1) + throw new AggregateException(exceptions); + throw exceptions[0]; } } } \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index 5649861ba..e6c63d7a1 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -57,16 +57,16 @@ - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.Mdb.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.Pdb.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Rocks.dll + ..\packages\Mono.Cecil.0.10.0-beta2\lib\net40\Mono.Cecil.Rocks.dll @@ -427,6 +427,12 @@ Bz44213.xaml + + DefaultCtorRouting.xaml + + + DefaultCtorRouting2.xaml + @@ -775,6 +781,12 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + + + MSBuild:UpdateDesignTimeXaml + diff --git a/Xamarin.Forms.Xaml.UnitTests/XamlC/FieldReferenceExtensionsTests.cs b/Xamarin.Forms.Xaml.UnitTests/XamlC/FieldReferenceExtensionsTests.cs index 79144f98b..6abf1e27b 100644 --- a/Xamarin.Forms.Xaml.UnitTests/XamlC/FieldReferenceExtensionsTests.cs +++ b/Xamarin.Forms.Xaml.UnitTests/XamlC/FieldReferenceExtensionsTests.cs @@ -37,7 +37,7 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests [Test] public void ResolveGenericsOnNonGenericDoesNotThrow () { - var type = module.Import (typeof (NonGenericClass)); + var type = module.ImportReference (typeof (NonGenericClass)); TypeReference declaringTypeReference; FieldDefinition field = type.GetField (fd => fd.Name == "Field", out declaringTypeReference); Assert.DoesNotThrow (() => field.ResolveGenericParameters (declaringTypeReference)); @@ -46,7 +46,7 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests [Test] public void NonGenericFieldOnGenericType () { - var type = module.Import (typeof (Inheritor)); + var type = module.ImportReference (typeof (Inheritor)); TypeReference declaringTypeReference; FieldDefinition field = type.GetField (fd => fd.Name == "NonGenericField", out declaringTypeReference); Assert.AreEqual ("NonGenericField", field.Name); @@ -61,7 +61,7 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests [Test] public void GenericFieldOnGenericType () { - var type = module.Import (typeof (Inheritor)); + var type = module.ImportReference (typeof (Inheritor)); TypeReference declaringTypeReference; FieldDefinition field1 = type.GetField (fd => fd.Name == "GenericField1", out declaringTypeReference); diff --git a/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodReferenceExtensionsTests.cs b/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodReferenceExtensionsTests.cs index ab7925ac4..3bc9da2ca 100644 --- a/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodReferenceExtensionsTests.cs +++ b/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodReferenceExtensionsTests.cs @@ -24,13 +24,13 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests [Test] public void ResolveRowDefAdder () { - var propertyType = module.Import(typeof (RowDefinitionCollection)); + var propertyType = module.ImportReference(typeof (RowDefinitionCollection)); var adderT = propertyType.GetMethods ((md, tr) => md.Name == "Add", module).Single (); var adder = adderT.Item1; var ptype = adderT.Item2; Assert.AreEqual ("System.Void Xamarin.Forms.DefinitionCollection`1::Add(T)", adder.FullName); Assert.AreEqual ("Xamarin.Forms.DefinitionCollection`1", ptype.FullName); - var adderRef = module.Import (adder); + var adderRef = module.ImportReference (adder); Assert.AreEqual ("System.Void Xamarin.Forms.DefinitionCollection`1::Add(T)", adderRef.FullName); adderRef = adderRef.ResolveGenericParameters (ptype, module); Assert.AreEqual ("System.Void Xamarin.Forms.DefinitionCollection`1::Add(T)", adderRef.FullName); @@ -40,16 +40,16 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests public void GenericGetter () { TypeReference declaringTypeReference; - var type = module.Import (typeof (StackLayout)); + var type = module.ImportReference (typeof (StackLayout)); var property = type.GetProperty (pd => pd.Name == "Children", out declaringTypeReference); Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Layout`1::Children()", property.FullName); Assert.AreEqual ("Xamarin.Forms.Layout`1", declaringTypeReference.FullName); var propertyGetter = property.GetMethod; Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Layout`1::get_Children()", propertyGetter.FullName); - var propertyGetterRef = module.Import (propertyGetter); + var propertyGetterRef = module.ImportReference (propertyGetter); Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Layout`1::get_Children()", propertyGetterRef.FullName); - propertyGetterRef = module.Import (propertyGetterRef.ResolveGenericParameters (declaringTypeReference, module)); + propertyGetterRef = module.ImportReference (propertyGetterRef.ResolveGenericParameters (declaringTypeReference, module)); Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Layout`1::get_Children()", propertyGetterRef.FullName); var returnType = propertyGetterRef.ReturnType.ResolveGenericParameters (declaringTypeReference); Assert.AreEqual ("System.Collections.Generic.IList`1", returnType.FullName); @@ -59,16 +59,16 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests public void GetterWithGenericReturnType () { TypeReference declaringTypeReference; - var type = module.Import (typeof (Style)); + var type = module.ImportReference (typeof (Style)); var property = type.GetProperty (pd => pd.Name == "Setters", out declaringTypeReference); Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Style::Setters()", property.FullName); Assert.AreEqual ("Xamarin.Forms.Style", declaringTypeReference.FullName); var propertyGetter = property.GetMethod; Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Style::get_Setters()", propertyGetter.FullName); - var propertyGetterRef = module.Import (propertyGetter); + var propertyGetterRef = module.ImportReference (propertyGetter); Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Style::get_Setters()", propertyGetterRef.FullName); - propertyGetterRef = module.Import (propertyGetterRef.ResolveGenericParameters (declaringTypeReference, module)); + propertyGetterRef = module.ImportReference (propertyGetterRef.ResolveGenericParameters (declaringTypeReference, module)); Assert.AreEqual ("System.Collections.Generic.IList`1 Xamarin.Forms.Style::get_Setters()", propertyGetterRef.FullName); var returnType = propertyGetterRef.ReturnType.ResolveGenericParameters (declaringTypeReference); Assert.AreEqual ("System.Collections.Generic.IList`1", returnType.FullName); @@ -77,13 +77,13 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests [Test] public void ResolveChildren () { - var propertyType = module.Import (typeof (IList)); + var propertyType = module.ImportReference (typeof (IList)); var adderT = propertyType.GetMethods (md => md.Name == "Add" && md.Parameters.Count == 1, module).Single (); var adder = adderT.Item1; var ptype = adderT.Item2; Assert.AreEqual ("System.Void System.Collections.Generic.ICollection`1::Add(T)", adder.FullName); Assert.AreEqual ("System.Collections.Generic.ICollection`1", ptype.FullName); - var adderRef = module.Import (adder); + var adderRef = module.ImportReference (adder); Assert.AreEqual ("System.Void System.Collections.Generic.ICollection`1::Add(T)", adderRef.FullName); adderRef = adderRef.ResolveGenericParameters (ptype, module); Assert.AreEqual ("System.Void System.Collections.Generic.ICollection`1::Add(T)", adderRef.FullName); diff --git a/Xamarin.Forms.Xaml.UnitTests/XamlC/TypeReferenceExtensionsTests.cs b/Xamarin.Forms.Xaml.UnitTests/XamlC/TypeReferenceExtensionsTests.cs index 3b6f293f8..9aa03b9ec 100644 --- a/Xamarin.Forms.Xaml.UnitTests/XamlC/TypeReferenceExtensionsTests.cs +++ b/Xamarin.Forms.Xaml.UnitTests/XamlC/TypeReferenceExtensionsTests.cs @@ -97,7 +97,7 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests [TestCase(typeof(Bar), typeof(Foo), ExpectedResult = true)] public bool TestInheritsFromOrImplements(Type typeRef, Type baseClass) { - return TypeReferenceExtensions.InheritsFromOrImplements(module.Import(typeRef), module.Import(baseClass)); + return TypeReferenceExtensions.InheritsFromOrImplements(module.ImportReference(typeRef), module.ImportReference(baseClass)); } [Test] diff --git a/Xamarin.Forms.Xaml.UnitTests/packages.config b/Xamarin.Forms.Xaml.UnitTests/packages.config index 83b8c2ffc..e1ee78719 100644 --- a/Xamarin.Forms.Xaml.UnitTests/packages.config +++ b/Xamarin.Forms.Xaml.UnitTests/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.Xamlc/Xamlc.cs b/Xamarin.Forms.Xaml.Xamlc/Xamlc.cs index ec99b71a6..77301b041 100644 --- a/Xamarin.Forms.Xaml.Xamlc/Xamlc.cs +++ b/Xamarin.Forms.Xaml.Xamlc/Xamlc.cs @@ -62,9 +62,11 @@ namespace Xamarin.Forms.Xaml KeepXamlResources = keep, OptimizeIL = optimize, DependencyPaths = paths, - ReferencePath = refs + ReferencePath = refs, + DebugSymbols = true, }; - xamlc.Execute(null); + IList _; + xamlc.Execute(out _); } static void ShowHelp(OptionSet ops) diff --git a/Xamarin.Forms.Xaml/XamlLoader.cs b/Xamarin.Forms.Xaml/XamlLoader.cs index dcab4e94f..7fee40da9 100644 --- a/Xamarin.Forms.Xaml/XamlLoader.cs +++ b/Xamarin.Forms.Xaml/XamlLoader.cs @@ -52,7 +52,7 @@ namespace Xamarin.Forms.Xaml { var xaml = GetXamlForType(callingType); if (string.IsNullOrEmpty(xaml)) - throw new XamlParseException(string.Format("No embeddedresources found for {0}", callingType), new XmlLineInfo()); + throw new XamlParseException(string.Format("No embeddedresource found for {0}", callingType), new XmlLineInfo()); Load(view, xaml); }