示例#1
0
        IEnumerable <Instruction> PushCtorArguments(MethodReference ctorinfo, ElementNode enode)
        {
            foreach (var parameter in ctorinfo.Parameters)
            {
                var propname =
                    parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "System.Maui.ParameterAttribute")
                    .ConstructorArguments.First()
                    .Value as string;
                var node = enode.Properties[new XmlName("", propname)];
                if (!enode.SkipProperties.Contains(new XmlName("", propname)))
                {
                    enode.SkipProperties.Add(new XmlName("", propname));
                }
                VariableDefinition vardef;
                ValueNode          vnode = null;

                if (node is IElementNode && (vardef = Context.Variables[node as IElementNode]) != null)
                {
                    foreach (var instruction in vardef.LoadAs(parameter.ParameterType.ResolveGenericParameters(ctorinfo), Module))
                    {
                        yield return(instruction);
                    }
                }
                else if ((vnode = node as ValueNode) != null)
                {
                    foreach (var instruction in vnode.PushConvertedValue(Context,
                                                                         parameter.ParameterType,
                                                                         new ICustomAttributeProvider[] { parameter, parameter.ParameterType.ResolveCached() },
                                                                         enode.PushServiceProvider(Context), false, true))
                    {
                        yield return(instruction);
                    }
                }
            }
        }
示例#2
0
        IEnumerable <Instruction> PushCtorArguments(MethodDefinition ctorinfo, ElementNode enode)
        {
            foreach (var parameter in ctorinfo.Parameters)
            {
                var propname =
                    parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "Xamarin.Forms.ParameterAttribute")
                    .ConstructorArguments.First()
                    .Value as string;
                var node = enode.Properties[new XmlName("", propname)];
                if (!enode.SkipProperties.Contains(new XmlName("", propname)))
                {
                    enode.SkipProperties.Add(new XmlName("", propname));
                }
                VariableDefinition vardef;
                ValueNode          vnode = null;

                if (node is IElementNode && (vardef = Context.Variables[node as IElementNode]) != null)
                {
                    yield return(Instruction.Create(OpCodes.Ldloc, vardef));
                }
                else if ((vnode = node as ValueNode) != null)
                {
                    foreach (var instruction in vnode.PushConvertedValue(Context,
                                                                         parameter.ParameterType,
                                                                         new ICustomAttributeProvider[] { parameter, parameter.ParameterType.Resolve() },
                                                                         enode.PushServiceProvider(Context), false, true))
                    {
                        yield return(instruction);
                    }
                }
            }
        }
示例#3
0
        IEnumerable <Instruction> PushCtorXArguments(MethodReference factoryCtorInfo, ElementNode enode)
        {
            if (!enode.Properties.ContainsKey(XmlName.xArguments))
            {
                yield break;
            }

            var arguments = new List <INode>();
            var node      = enode.Properties[XmlName.xArguments] as ElementNode;

            if (node != null)
            {
                arguments.Add(node);
            }
            var list = enode.Properties[XmlName.xArguments] as ListNode;

            if (list != null)
            {
                foreach (var n in list.CollectionItems)
                {
                    arguments.Add(n);
                }
            }

            for (var i = 0; i < factoryCtorInfo.Parameters.Count; i++)
            {
                var parameter = factoryCtorInfo.Parameters[i];
                var arg       = arguments[i];
                VariableDefinition vardef;
                ValueNode          vnode = null;

                if (arg is IElementNode && (vardef = Context.Variables[arg as IElementNode]) != null)
                {
                    foreach (var instruction in vardef.LoadAs(parameter.ParameterType.ResolveGenericParameters(factoryCtorInfo), Module))
                    {
                        yield return(instruction);
                    }
                }
                else if ((vnode = arg as ValueNode) != null)
                {
                    foreach (var instruction in vnode.PushConvertedValue(Context,
                                                                         parameter.ParameterType,
                                                                         new ICustomAttributeProvider[] { parameter, parameter.ParameterType.ResolveCached() },
                                                                         enode.PushServiceProvider(Context), false, true))
                    {
                        yield return(instruction);
                    }
                }
            }
        }
示例#4
0
        IEnumerable <Instruction> PushCtorDefaultArguments(MethodDefinition factoryCtorInfo, ElementNode enode)
        {
            var arguments = new List <INode>();

            for (var i = 0; i < factoryCtorInfo.Parameters.Count; i++)
            {
                var parameter = factoryCtorInfo.Parameters[i];

                ValueNode arg = new ValueNode(parameter.Constant?.ToString(), enode.NamespaceResolver);

                if (arg != null)
                {
                    foreach (var instruction in arg.PushConvertedValue(Context,
                                                                       parameter.ParameterType,
                                                                       new ICustomAttributeProvider[] { parameter, parameter.ParameterType.ResolveCached() },
                                                                       enode.PushServiceProvider(Context), false, true))
                    {
                        yield return(instruction);
                    }
                }
            }
        }
示例#5
0
        public void Visit(ElementNode node, INode parentNode)
        {
            var            typeref = Module.Import(node.XmlType.GetTypeReference(Module, node));
            TypeDefinition typedef = typeref.Resolve();

            if (IsXaml2009LanguagePrimitive(node))
            {
                var vardef = new VariableDefinition(typeref);
                Context.Variables [node] = vardef;
                Context.Body.Variables.Add(vardef);

                Context.IL.Append(PushValueFromLanguagePrimitive(typedef, node));
                Context.IL.Emit(OpCodes.Stloc, vardef);
                return;
            }

            if (typeref.FullName == "Xamarin.Forms.Xaml.StaticExtension")
            {
                var markupProvider = new StaticExtension();

                var il = markupProvider.ProvideValue(node, Module, Context, out typeref);
                typeref = Module.Import(typeref);

                var vardef = new VariableDefinition(typeref);
                Context.Variables [node] = vardef;
                Context.Body.Variables.Add(vardef);

                Context.IL.Append(il);
                Context.IL.Emit(OpCodes.Stloc, vardef);

                //clean the node as it has been fully exhausted
                node.Properties.Clear();
                node.CollectionItems.Clear();
                return;
            }

            MethodDefinition factoryCtorInfo       = null;
            MethodDefinition factoryMethodInfo     = null;
            MethodDefinition parameterizedCtorInfo = null;
            MethodDefinition ctorInfo = null;

            if (node.Properties.ContainsKey(XmlName.xArguments) && !node.Properties.ContainsKey(XmlName.xFactoryMethod))
            {
                factoryCtorInfo = typedef.AllMethods().FirstOrDefault(md => md.IsConstructor &&
                                                                      !md.IsStatic &&
                                                                      md.HasParameters &&
                                                                      md.MatchXArguments(node, Module, Context));
                if (factoryCtorInfo == null)
                {
                    throw new XamlParseException(
                              string.Format("No constructors found for {0} with matching x:Arguments", typedef.FullName), node);
                }
                ctorInfo = factoryCtorInfo;
                if (!typedef.IsValueType)                 //for ctor'ing typedefs, we first have to ldloca before the params
                {
                    Context.IL.Append(PushCtorXArguments(factoryCtorInfo, node));
                }
            }
            else if (node.Properties.ContainsKey(XmlName.xFactoryMethod))
            {
                var factoryMethod = (string)(node.Properties [XmlName.xFactoryMethod] as ValueNode).Value;
                factoryMethodInfo = typedef.AllMethods().FirstOrDefault(md => !md.IsConstructor &&
                                                                        md.Name == factoryMethod &&
                                                                        md.IsStatic &&
                                                                        md.MatchXArguments(node, Module, Context));
                if (factoryMethodInfo == null)
                {
                    throw new XamlParseException(
                              String.Format("No static method found for {0}::{1} ({2})", typedef.FullName, factoryMethod, null), node);
                }
                Context.IL.Append(PushCtorXArguments(factoryMethodInfo, node));
            }
            if (ctorInfo == null && factoryMethodInfo == null)
            {
                parameterizedCtorInfo = typedef.Methods.FirstOrDefault(md => md.IsConstructor &&
                                                                       !md.IsStatic &&
                                                                       md.HasParameters &&
                                                                       md.Parameters.All(
                                                                           pd =>
                                                                           pd.CustomAttributes.Any(
                                                                               ca =>
                                                                               ca.AttributeType.FullName ==
                                                                               "Xamarin.Forms.ParameterAttribute")));
            }
            if (parameterizedCtorInfo != null && ValidateCtorArguments(parameterizedCtorInfo, node))
            {
                ctorInfo = parameterizedCtorInfo;
//				IL_0000:  ldstr "foo"
                Context.IL.Append(PushCtorArguments(parameterizedCtorInfo, node));
            }
            ctorInfo = ctorInfo ?? typedef.Methods.FirstOrDefault(md => md.IsConstructor && !md.HasParameters && !md.IsStatic);

            var ctorinforef          = ctorInfo?.ResolveGenericParameters(typeref, Module);
            var factorymethodinforef = factoryMethodInfo?.ResolveGenericParameters(typeref, Module);
            var implicitOperatorref  = typedef.Methods.FirstOrDefault(md =>
                                                                      md.IsPublic &&
                                                                      md.IsStatic &&
                                                                      md.IsSpecialName &&
                                                                      md.Name == "op_Implicit" && md.Parameters [0].ParameterType.FullName == "System.String");

            if (ctorinforef != null || factorymethodinforef != null || typedef.IsValueType)
            {
                VariableDefinition vardef = new VariableDefinition(typeref);
                Context.Variables [node] = vardef;
                Context.Body.Variables.Add(vardef);

                ValueNode vnode = null;
                if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&
                    vardef.VariableType.IsValueType)
                {
                    //<Color>Purple</Color>
                    Context.IL.Append(vnode.PushConvertedValue(Context, typeref, new ICustomAttributeProvider [] { typedef },
                                                               node.PushServiceProvider(Context), false, true));
                    Context.IL.Emit(OpCodes.Stloc, vardef);
                }
                else if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&
                         implicitOperatorref != null)
                {
                    //<FileImageSource>path.png</FileImageSource>
                    var implicitOperator = Module.Import(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.Stloc, vardef);
                }
                else if (!typedef.IsValueType)
                {
                    var ctor = Module.Import(ctorinforef);
//					IL_0001:  newobj instance void class [Xamarin.Forms.Core]Xamarin.Forms.Button::'.ctor'()
//					IL_0006:  stloc.0
                    Context.IL.Emit(OpCodes.Newobj, ctor);
                    Context.IL.Emit(OpCodes.Stloc, vardef);
                }
                else if (ctorInfo != null && node.Properties.ContainsKey(XmlName.xArguments) &&
                         !node.Properties.ContainsKey(XmlName.xFactoryMethod) && ctorInfo.MatchXArguments(node, Module, Context))
                {
//					IL_0008:  ldloca.s 1
//					IL_000a:  ldc.i4.1
//					IL_000b:  call instance void valuetype Test/Foo::'.ctor'(bool)

                    var ctor = Module.Import(ctorinforef);
                    Context.IL.Emit(OpCodes.Ldloca, vardef);
                    Context.IL.Append(PushCtorXArguments(factoryCtorInfo, node));
                    Context.IL.Emit(OpCodes.Call, ctor);
                }
                else
                {
//					IL_0000:  ldloca.s 0
//					IL_0002:  initobj Test/Foo
                    Context.IL.Emit(OpCodes.Ldloca, vardef);
                    Context.IL.Emit(OpCodes.Initobj, Module.Import(typedef));
                }

                if (typeref.FullName == "Xamarin.Forms.Xaml.TypeExtension")
                {
                    var visitor = new SetPropertiesVisitor(Context);
                    foreach (var cnode in node.Properties.Values.ToList())
                    {
                        cnode.Accept(visitor, node);
                    }
                    foreach (var cnode in node.CollectionItems)
                    {
                        cnode.Accept(visitor, node);
                    }

                    //As we're stripping the TypeExtension bare, keep the type if we need it later (hint: we do need it)
                    INode ntype;
                    if (!node.Properties.TryGetValue(new XmlName("", "TypeName"), out ntype))
                    {
                        ntype = node.CollectionItems [0];
                    }

                    var type   = ((ValueNode)ntype).Value as string;
                    var prefix = "";
                    if (type.Contains(":"))
                    {
                        prefix = type.Split(':') [0].Trim();
                        type   = type.Split(':') [1].Trim();
                    }
                    var namespaceuri = node.NamespaceResolver.LookupNamespace(prefix);
                    Context.TypeExtensions [node] = new XmlType(namespaceuri, type, null).GetTypeReference(Module, node);

                    if (!node.SkipProperties.Contains(new XmlName("", "TypeName")))
                    {
                        node.SkipProperties.Add(new XmlName("", "TypeName"));
                    }

                    var vardefref = new VariableDefinitionReference(vardef);
                    Context.IL.Append(SetPropertiesVisitor.ProvideValue(vardefref, Context, Module, node));
                    if (vardef != vardefref.VariableDefinition)
                    {
                        Context.Variables [node] = vardefref.VariableDefinition;
                        Context.Body.Variables.Add(vardefref.VariableDefinition);
                    }
                }

                if (typeref.FullName == "Xamarin.Forms.Xaml.ArrayExtension")
                {
                    var visitor = new SetPropertiesVisitor(Context);
                    foreach (var cnode in node.Properties.Values.ToList())
                    {
                        cnode.Accept(visitor, node);
                    }
                    foreach (var cnode in node.CollectionItems)
                    {
                        cnode.Accept(visitor, node);
                    }

                    var markupProvider = new ArrayExtension();

                    var il = markupProvider.ProvideValue(node, Module, Context, out typeref);

                    vardef = new VariableDefinition(typeref);
                    Context.Variables[node] = vardef;
                    Context.Body.Variables.Add(vardef);

                    Context.IL.Append(il);
                    Context.IL.Emit(OpCodes.Stloc, vardef);

                    //clean the node as it has been fully exhausted
                    node.Properties.Remove(new XmlName("", "Type"));
                    node.CollectionItems.Clear();
                }
            }
        }
示例#6
0
        public void Visit(ElementNode node, INode parentNode)
        {
            var            typeref = Module.ImportReference(node.XmlType.GetTypeReference(Module, node));
            TypeDefinition typedef = typeref.ResolveCached();

            if (IsXaml2009LanguagePrimitive(node))
            {
                var vardef = new VariableDefinition(typeref);
                Context.Variables [node] = vardef;
                Context.Body.Variables.Add(vardef);

                Context.IL.Append(PushValueFromLanguagePrimitive(typedef, node));
                Context.IL.Emit(OpCodes.Stloc, vardef);
                return;
            }

            //if this is a MarkupExtension that can be compiled directly, compile and returns the value
            var compiledMarkupExtensionName = typeref
                                              .GetCustomAttribute(Module, ("Xamarin.Forms.Core", "Xamarin.Forms.Xaml", "ProvideCompiledAttribute"))
                                              ?.ConstructorArguments?[0].Value as string;
            Type compiledMarkupExtensionType;
            ICompiledMarkupExtension markupProvider;

            if (compiledMarkupExtensionName != null &&
                (compiledMarkupExtensionType = Type.GetType(compiledMarkupExtensionName)) != null &&
                (markupProvider = Activator.CreateInstance(compiledMarkupExtensionType) as ICompiledMarkupExtension) != null)
            {
                var il = markupProvider.ProvideValue(node, Module, Context, out typeref);
                typeref = Module.ImportReference(typeref);

                var vardef = new VariableDefinition(typeref);
                Context.Variables[node] = vardef;
                Context.Body.Variables.Add(vardef);

                Context.IL.Append(il);
                Context.IL.Emit(OpCodes.Stloc, vardef);

                //clean the node as it has been fully exhausted
                foreach (var prop in node.Properties)
                {
                    if (!node.SkipProperties.Contains(prop.Key))
                    {
                        node.SkipProperties.Add(prop.Key);
                    }
                }
                node.CollectionItems.Clear();
                return;
            }

            MethodDefinition factoryCtorInfo       = null;
            MethodDefinition factoryMethodInfo     = null;
            MethodDefinition parameterizedCtorInfo = null;
            MethodDefinition ctorInfo = null;

            if (node.Properties.ContainsKey(XmlName.xArguments) && !node.Properties.ContainsKey(XmlName.xFactoryMethod))
            {
                factoryCtorInfo = typedef.AllMethods().FirstOrDefault(md => md.IsConstructor &&
                                                                      !md.IsStatic &&
                                                                      md.HasParameters &&
                                                                      md.MatchXArguments(node, typeref, Module, Context));
                if (factoryCtorInfo == null)
                {
                    throw new XamlParseException(
                              string.Format("No constructors found for {0} with matching x:Arguments", typedef.FullName), node);
                }
                ctorInfo = factoryCtorInfo;
                if (!typedef.IsValueType)                 //for ctor'ing typedefs, we first have to ldloca before the params
                {
                    Context.IL.Append(PushCtorXArguments(factoryCtorInfo, node));
                }
            }
            else if (node.Properties.ContainsKey(XmlName.xFactoryMethod))
            {
                var factoryMethod = (string)(node.Properties [XmlName.xFactoryMethod] as ValueNode).Value;
                factoryMethodInfo = typedef.AllMethods().FirstOrDefault(md => !md.IsConstructor &&
                                                                        md.Name == factoryMethod &&
                                                                        md.IsStatic &&
                                                                        md.MatchXArguments(node, typeref, Module, Context));
                if (factoryMethodInfo == null)
                {
                    throw new XamlParseException(
                              String.Format("No static method found for {0}::{1} ({2})", typedef.FullName, factoryMethod, null), node);
                }
                Context.IL.Append(PushCtorXArguments(factoryMethodInfo, node));
            }
            if (ctorInfo == null && factoryMethodInfo == null)
            {
                parameterizedCtorInfo = typedef.Methods.FirstOrDefault(md => md.IsConstructor &&
                                                                       !md.IsStatic &&
                                                                       md.HasParameters &&
                                                                       md.Parameters.All(
                                                                           pd =>
                                                                           pd.CustomAttributes.Any(
                                                                               ca =>
                                                                               ca.AttributeType.FullName ==
                                                                               "Xamarin.Forms.ParameterAttribute")));
            }
            string missingCtorParameter = null;

            if (parameterizedCtorInfo != null && ValidateCtorArguments(parameterizedCtorInfo, node, out missingCtorParameter))
            {
                ctorInfo = parameterizedCtorInfo;
//				IL_0000:  ldstr "foo"
                Context.IL.Append(PushCtorArguments(parameterizedCtorInfo, node));
            }
            ctorInfo = ctorInfo ?? typedef.Methods.FirstOrDefault(md => md.IsConstructor && !md.HasParameters && !md.IsStatic);
            if (parameterizedCtorInfo != null && ctorInfo == null)
            {
                //there was a parameterized ctor, we didn't use it
                throw new XamlParseException($"The Property '{missingCtorParameter}' is required to create a '{typedef.FullName}' object.", node);
            }
            var ctorinforef          = ctorInfo?.ResolveGenericParameters(typeref, Module);
            var factorymethodinforef = factoryMethodInfo?.ResolveGenericParameters(typeref, Module);
            var implicitOperatorref  = typedef.Methods.FirstOrDefault(md =>
                                                                      md.IsPublic &&
                                                                      md.IsStatic &&
                                                                      md.IsSpecialName &&
                                                                      md.Name == "op_Implicit" && md.Parameters [0].ParameterType.FullName == "System.String");

            if (ctorinforef != null || factorymethodinforef != null || typedef.IsValueType)
            {
                VariableDefinition vardef = new VariableDefinition(typeref);
                Context.Variables [node] = vardef;
                Context.Body.Variables.Add(vardef);

                ValueNode vnode = null;
                if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&
                    vardef.VariableType.IsValueType)
                {
                    //<Color>Purple</Color>
                    Context.IL.Append(vnode.PushConvertedValue(Context, typeref, new ICustomAttributeProvider [] { typedef },
                                                               node.PushServiceProvider(Context), false, true));
                    Context.IL.Emit(OpCodes.Stloc, vardef);
                }
                else if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&
                         implicitOperatorref != null)
                {
                    //<FileImageSource>path.png</FileImageSource>
                    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.ImportReference(factorymethodinforef));
                    Context.IL.Emit(OpCodes.Stloc, vardef);
                }
                else if (!typedef.IsValueType)
                {
                    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);
                    Context.IL.Emit(OpCodes.Stloc, vardef);
                }
                else if (ctorInfo != null && node.Properties.ContainsKey(XmlName.xArguments) &&
                         !node.Properties.ContainsKey(XmlName.xFactoryMethod) && ctorInfo.MatchXArguments(node, typeref, Module, Context))
                {
//					IL_0008:  ldloca.s 1
//					IL_000a:  ldc.i4.1
//					IL_000b:  call instance void valuetype Test/Foo::'.ctor'(bool)

                    var ctor = Module.ImportReference(ctorinforef);
                    Context.IL.Emit(OpCodes.Ldloca, vardef);
                    Context.IL.Append(PushCtorXArguments(factoryCtorInfo, node));
                    Context.IL.Emit(OpCodes.Call, ctor);
                }
                else
                {
//					IL_0000:  ldloca.s 0
//					IL_0002:  initobj Test/Foo
                    Context.IL.Emit(OpCodes.Ldloca, vardef);
                    Context.IL.Emit(OpCodes.Initobj, Module.ImportReference(typedef));
                }

                if (typeref.FullName == "Xamarin.Forms.Xaml.ArrayExtension")
                {
                    var visitor = new SetPropertiesVisitor(Context);
                    foreach (var cnode in node.Properties.Values.ToList())
                    {
                        cnode.Accept(visitor, node);
                    }
                    foreach (var cnode in node.CollectionItems)
                    {
                        cnode.Accept(visitor, node);
                    }

                    markupProvider = new ArrayExtension();

                    var il = markupProvider.ProvideValue(node, Module, Context, out typeref);

                    vardef = new VariableDefinition(typeref);
                    Context.Variables[node] = vardef;
                    Context.Body.Variables.Add(vardef);

                    Context.IL.Append(il);
                    Context.IL.Emit(OpCodes.Stloc, vardef);

                    //clean the node as it has been fully exhausted
                    foreach (var prop in node.Properties)
                    {
                        if (!node.SkipProperties.Contains(prop.Key))
                        {
                            node.SkipProperties.Add(prop.Key);
                        }
                    }
                    node.CollectionItems.Clear();

                    return;
                }
            }
        }
示例#7
0
        public static IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ILContext context,
                                                             ModuleDefinition module, ElementNode node)
        {
            GenericInstanceType   markupExtension;
            IList <TypeReference> genericArguments;

            if (vardefref.VariableDefinition.VariableType.FullName == "Xamarin.Forms.Xaml.ArrayExtension" &&
                vardefref.VariableDefinition.VariableType.ImplementsGenericInterface("Xamarin.Forms.Xaml.IMarkupExtension`1",
                                                                                     out markupExtension, out genericArguments))
            {
                var markExt          = markupExtension.Resolve();
                var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue");
                var provideValue     = module.Import(provideValueInfo);
                provideValue =
                    module.Import(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()));
                }
                else
                {
                    vardefref.VariableDefinition = new VariableDefinition(module.Import(genericArguments.First()));
                }
                yield return(Instruction.Create(OpCodes.Ldloc, context.Variables[node]));

                foreach (var instruction in node.PushServiceProvider(context))
                {
                    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.Stloc, vardefref.VariableDefinition));
            }
            else if (vardefref.VariableDefinition.VariableType.ImplementsGenericInterface("Xamarin.Forms.Xaml.IMarkupExtension`1",
                                                                                          out markupExtension, out genericArguments))
            {
                var markExt          = markupExtension.Resolve();
                var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue");
                var provideValue     = module.Import(provideValueInfo);
                provideValue =
                    module.Import(provideValue.ResolveGenericParameters(markupExtension, module));

                vardefref.VariableDefinition = new VariableDefinition(module.Import(genericArguments.First()));
                yield return(Instruction.Create(OpCodes.Ldloc, context.Variables[node]));

                foreach (var instruction in node.PushServiceProvider(context))
                {
                    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))))
            {
                var markExt          = module.Import(typeof(IMarkupExtension)).Resolve();
                var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue");
                var provideValue     = module.Import(provideValueInfo);

                vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object);
                yield return(Instruction.Create(OpCodes.Ldloc, context.Variables[node]));

                foreach (var instruction in node.PushServiceProvider(context))
                {
                    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(IValueProvider))))
            {
                var markExt          = module.Import(typeof(IValueProvider)).Resolve();
                var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue");
                var provideValue     = module.Import(provideValueInfo);

                vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object);
                yield return(Instruction.Create(OpCodes.Ldloc, context.Variables[node]));

                foreach (var instruction in node.PushServiceProvider(context))
                {
                    yield return(instruction);
                }
                yield return(Instruction.Create(OpCodes.Callvirt, provideValue));

                yield return(Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition));
            }
        }
示例#8
0
        IEnumerable <Instruction> PushCtorXArguments(MethodDefinition factoryCtorInfo, ElementNode enode)
        {
            if (!enode.Properties.ContainsKey(XmlName.xArguments))
            {
                yield break;
            }

            var arguments = new List <INode>();
            var node      = enode.Properties[XmlName.xArguments] as ElementNode;

            if (node != null)
            {
                node.Accept(new SetPropertiesVisitor(Context, true), null);
                arguments.Add(node);
            }

            var list = enode.Properties[XmlName.xArguments] as ListNode;

            if (list != null)
            {
                foreach (var n in list.CollectionItems)
                {
                    arguments.Add(n);
                }
            }

            for (var i = 0; i < arguments.Count; i++)
            {
                var parameter = factoryCtorInfo.Parameters[i];
                var arg       = arguments[i];
                VariableDefinition vardef;
                ValueNode          vnode = null;

                if (arg is IElementNode && (vardef = Context.Variables[arg as IElementNode]) != null)
                {
                    yield return(Instruction.Create(OpCodes.Ldloc, vardef));
                }
                else if ((vnode = arg as ValueNode) != null)
                {
                    foreach (var instruction in vnode.PushConvertedValue(Context,
                                                                         parameter.ParameterType,
                                                                         new ICustomAttributeProvider[] { parameter, parameter.ParameterType.ResolveCached() },
                                                                         enode.PushServiceProvider(Context), false, true))
                    {
                        yield return(instruction);
                    }
                }
            }

            for (var i = arguments.Count; i < factoryCtorInfo.Parameters.Count; i++)
            {
                var parameter = factoryCtorInfo.Parameters[i];
                var arg       = new ValueNode(parameter.Constant?.ToString(), node?.NamespaceResolver);

                foreach (var instruction in arg.PushConvertedValue(Context,
                                                                   parameter.ParameterType,
                                                                   new ICustomAttributeProvider[] { parameter, parameter.ParameterType.ResolveCached() },
                                                                   enode.PushServiceProvider(Context), false, true))
                {
                    yield return(instruction);
                }
            }
        }