예제 #1
0
        public void Visit(ElementNode node, INode parentNode)
        {
            XmlName propertyName;

            //Set ResourcesDictionaries to their parents
            if (IsResourceDictionary(node) && SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
            {
                if ((propertyName.LocalName == "Resources" || propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)))
                {
                    Context.IL.Append(SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node));
                    return;
                }
            }

            //Only proceed further if the node is a keyless RD
            if (parentNode is IElementNode &&
                IsResourceDictionary((IElementNode)parentNode) &&
                !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
            {
                node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
            }
            else if (parentNode is ListNode &&
                     IsResourceDictionary((IElementNode)parentNode.Parent) &&
                     !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
            {
                node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
            }
        }
예제 #2
0
        public void Visit(ListNode node, INode parentNode)
        {
            XmlName name;

            if (SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out name))
            {
                node.XmlName = name;
            }
        }
예제 #3
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, ("System.Maui.Core", "System.Maui.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.methodDef.IsConstructor &&
                                                                      !md.methodDef.IsStatic &&
                                                                      md.methodDef.HasParameters &&
                                                                      md.methodDef.MatchXArguments(node, typeref, Module, Context)).methodDef;
                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.ResolveGenericParameters(typeref, Module), node));
                }
            }
            else if (node.Properties.ContainsKey(XmlName.xFactoryMethod))
            {
                var factoryMethod = (string)(node.Properties [XmlName.xFactoryMethod] as ValueNode).Value;
                factoryMethodInfo = typedef.AllMethods().FirstOrDefault(md => !md.methodDef.IsConstructor &&
                                                                        md.methodDef.Name == factoryMethod &&
                                                                        md.methodDef.IsStatic &&
                                                                        md.methodDef.MatchXArguments(node, typeref, Module, Context)).methodDef;
                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.ResolveGenericParameters(typeref, Module), 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 ==
                                                                               "System.Maui.ParameterAttribute")));
            }
            string missingCtorParameter = null;

            if (parameterizedCtorInfo != null && ValidateCtorArguments(parameterizedCtorInfo, node, out missingCtorParameter))
            {
                ctorInfo = parameterizedCtorInfo;
//				IL_0000:  ldstr "foo"
                Context.IL.Append(PushCtorArguments(parameterizedCtorInfo.ResolveGenericParameters(typeref, Module), 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 (!typedef.IsValueType && ctorInfo == null && factoryMethodInfo == null)
            {
                throw new XamlParseException($"Missing default constructor for '{typedef.FullName}'.", node);
            }

            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 [System.Maui.Core]System.Maui.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(ctor, 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 == "System.Maui.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;
                }
            }
        }