public static TypeReference GetTypeReference(string xmlType, ModuleDefinition module, BaseNode iNode)
        {
            var split = xmlType.Split(':');

            if (split.Length > 2)
            {
                throw new XamlParseException($"Type \"{xmlType}\" is invalid", iNode);
            }

            string prefix, name;

            if (split.Length == 2)
            {
                prefix = split [0];
                name   = split [1];
            }
            else
            {
                prefix = "";
                name   = split [0];
            }
            var namespaceuri = iNode.NamespaceResolver.LookupNamespace(prefix) ?? "";

            return(XmlTypeExtensions.GetTypeReference(namespaceuri, name, module, iNode));
        }
Пример #2
0
        List <object> GetCtorArguments(MethodDefinition ctorinfo, ElementNode enode, EXamlContext context)
        {
            List <object> ret = null;

            foreach (var parameter in ctorinfo.Parameters)
            {
                var propname =
                    parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "Tizen.NUI.Binding.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));
                }

                if (node is ValueNode valueNode)
                {
                    var valueType = parameter.ParameterType;

                    if ("System.Type" == valueType.FullName)
                    {
                        var typeRef = XmlTypeExtensions.GetTypeReference(valueNode.Value as string, Module, node as BaseNode, XmlTypeExtensions.ModeOfGetType.Both);
                        context.Values[node] = new EXamlCreateObject(context, typeRef);
                    }
                    else
                    {
                        var converterType = valueNode.GetConverterType(new ICustomAttributeProvider[] { parameter, parameter.ParameterType.ResolveCached() });

                        if (null != converterType)
                        {
                            var converterValue = new EXamlValueConverterFromString(context, converterType.Resolve(), valueNode.Value as string);
                            context.Values[node] = new EXamlCreateObject(context, converterValue, valueType);
                        }
                        else
                        {
                            context.Values[node] = valueNode.GetBaseValue(context, valueType);
                        }
                    }

                    if (null == ret)
                    {
                        ret = new List <object>();
                    }

                    ret.Add(context.Values[node]);
                }
            }

            return(ret);
        }
        public FieldReference GetBindablePropertyFieldReference(string value, ModuleDefinition module, BaseNode node)
        {
            FieldReference bpRef = null;
            string         typeName = null, propertyName = null;

            var parts = value.Split('.');

            if (parts.Length == 1)
            {
                var parent = node.Parent?.Parent as IElementNode ?? (node.Parent?.Parent as IListNode)?.Parent as IElementNode;
                if ((node.Parent as ElementNode)?.XmlType.NamespaceUri == XamlParser.XFUri &&
                    ((node.Parent as ElementNode)?.XmlType.Name == "Setter" || (node.Parent as ElementNode)?.XmlType.Name == "PropertyCondition"))
                {
                    if (parent.XmlType.NamespaceUri == XamlParser.XFUri &&
                        (parent.XmlType.Name == "Trigger" || parent.XmlType.Name == "DataTrigger" || parent.XmlType.Name == "MultiTrigger" || parent.XmlType.Name == "Style"))
                    {
                        var ttnode = (parent as ElementNode).Properties [new XmlName("", "TargetType")];
                        if (ttnode is ValueNode)
                        {
                            typeName = (ttnode as ValueNode).Value as string;
                        }
                        else if (ttnode is IElementNode)
                        {
                            typeName = ((ttnode as IElementNode).CollectionItems.FirstOrDefault() as ValueNode)?.Value as string ?? ((ttnode as IElementNode).Properties [new XmlName("", "TypeName")] as ValueNode)?.Value as string;
                        }
                    }
                }
                else if ((node.Parent as ElementNode)?.XmlType.NamespaceUri == XamlParser.XFUri && (node.Parent as ElementNode)?.XmlType.Name == "Trigger")
                {
                    typeName = ((node.Parent as ElementNode).Properties [new XmlName("", "TargetType")] as ValueNode).Value as string;
                }
                propertyName = parts [0];
            }
            else if (parts.Length == 2)
            {
                typeName     = parts [0];
                propertyName = parts [1];
            }
            else
            {
                throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(BindableProperty)}", node);
            }

            if (typeName == null || propertyName == null)
            {
                throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(BindableProperty)}", node);
            }

            var typeRef = XmlTypeExtensions.GetTypeReference(typeName, module, node);

            if (typeRef == null)
            {
                throw new XamlParseException($"Can't resolve {typeName}", node);
            }
            bpRef = GetBindablePropertyFieldReference(typeRef, propertyName, module);
            if (bpRef == null)
            {
                throw new XamlParseException($"Can't resolve {propertyName} on {typeRef.Name}", node);
            }
            return(bpRef);
        }
Пример #4
0
        public FieldReference GetBindablePropertyFieldReference(string value, ModuleDefinition module, BaseNode node)
        {
            FieldReference bpRef = null;
            string         typeName = null, propertyName = null;

            var parts = value.Split('.');

            if (parts.Length == 1)
            {
                var parent = node.Parent?.Parent as IElementNode ?? (node.Parent?.Parent as IListNode)?.Parent as IElementNode;
                if ((node.Parent as ElementNode)?.XmlType.NamespaceUri == XamlParser.XFUri &&
                    ((node.Parent as ElementNode)?.XmlType.Name == nameof(Setter) ||
                     (node.Parent as ElementNode)?.XmlType.Name == nameof(PropertyCondition)))
                {
                    if (parent.XmlType.NamespaceUri == XamlParser.XFUri &&
                        (parent.XmlType.Name == nameof(Trigger) ||
                         parent.XmlType.Name == nameof(DataTrigger) ||
                         parent.XmlType.Name == nameof(MultiTrigger) ||
                         parent.XmlType.Name == nameof(Style)))
                    {
                        var ttnode = (parent as ElementNode).Properties[new XmlName("", "TargetType")];
                        if (ttnode is ValueNode)
                        {
                            typeName = (ttnode as ValueNode).Value as string;
                        }
                        else if (ttnode is IElementNode)
                        {
                            typeName = ((ttnode as IElementNode).CollectionItems.FirstOrDefault() as ValueNode)?.Value as string ?? ((ttnode as IElementNode).Properties[new XmlName("", "TypeName")] as ValueNode)?.Value as string;
                        }
                    }
                    else if (parent.XmlType.NamespaceUri == XamlParser.XFUri && parent.XmlType.Name == nameof(VisualState))
                    {
                        typeName = FindTypeNameForVisualState(parent, node);
                    }
                }
                else if ((node.Parent as ElementNode)?.XmlType.NamespaceUri == XamlParser.XFUri && (node.Parent as ElementNode)?.XmlType.Name == nameof(Trigger))
                {
                    typeName = ((node.Parent as ElementNode).Properties[new XmlName("", "TargetType")] as ValueNode).Value as string;
                }
                propertyName = parts[0];
            }
            else if (parts.Length == 2)
            {
                typeName     = parts[0];
                propertyName = parts[1];
            }
            else
            {
                throw new BuildException(Conversion, node, null, value, typeof(BindableProperty));
            }

            if (typeName == null || propertyName == null)
            {
                throw new BuildException(Conversion, node, null, value, typeof(BindableProperty));
            }

            var typeRef = XmlTypeExtensions.GetTypeReference(typeName, module, node);

            if (typeRef == null)
            {
                throw new BuildException(TypeResolution, node, null, typeName);
            }

            bpRef = GetBindablePropertyFieldReference(typeRef, propertyName, module);
            if (bpRef == null)
            {
                throw new BuildException(PropertyResolution, node, null, propertyName, typeRef.Name);
            }
            return(bpRef);
        }
Пример #5
0
        public IEnumerable <Instruction> ConvertFromString(string value, ILContext context, BaseNode node)
        {
            var module = context.Module;

            EmbeddedResource matchedResource = null;

            foreach (var resource in module.Resources.OfType <EmbeddedResource>())
            {
                if (resource.Name.StartsWith(context.EmbeddedResourceNameSpace) && resource.Name.EndsWith(value))
                {
                    matchedResource = resource;
                    break;
                }
            }

            if (null == matchedResource)
            {
                foreach (var resource in module.Resources.OfType <EmbeddedResource>())
                {
                    if (resource.Name.EndsWith(value))
                    {
                        matchedResource = resource;
                        break;
                    }
                }
            }

            if (null != matchedResource)
            {
                string classname;
                if (matchedResource.IsResourceDictionaryXaml(module, out classname))
                {
                    int lastIndex     = classname.LastIndexOf('.');
                    var realClassName = classname.Substring(lastIndex + 1);
                    var typeref       = XmlTypeExtensions.GetTypeReference(realClassName, module, node, XmlTypeExtensions.ModeOfGetType.Both);

                    var typeName             = matchedResource.Name.Replace('.', '_');
                    var typeDefOfGetResource = module.Types.FirstOrDefault(type => type.FullName == "GetResource." + typeName);
                    if (null != typeDefOfGetResource)
                    {
                        module.Types.Remove(typeDefOfGetResource);
                        typeDefOfGetResource = null;
                    }

                    if (null == typeDefOfGetResource)
                    {
                        typeDefOfGetResource          = new TypeDefinition("GetResource", typeName, TypeAttributes.NotPublic);
                        typeDefOfGetResource.BaseType = typeref;
                        module.Types.Add(typeDefOfGetResource);

                        typeDefOfGetResource.AddDefaultConstructor(typeref);
                    }

                    var methodName          = "GetResource";
                    var methodOfGetResource = typeDefOfGetResource.Methods.FirstOrDefault(m => m.Name == methodName);

                    if (null == methodOfGetResource)
                    {
                        methodOfGetResource = new MethodDefinition(methodName, MethodAttributes.Public, typeref);
                        typeDefOfGetResource.Methods.Add(methodOfGetResource);
                    }

                    var constructor = typeDefOfGetResource.Methods.FirstOrDefault(m => m.IsConstructor);

                    if (null != constructor)
                    {
                        constructor.Body.Instructions.Insert(constructor.Body.Instructions.Count - 1, Instruction.Create(OpCodes.Ldarg_0));
                        constructor.Body.Instructions.Insert(constructor.Body.Instructions.Count - 1, Instruction.Create(OpCodes.Call, methodOfGetResource));
                        constructor.Body.Instructions.Insert(constructor.Body.Instructions.Count - 1, Instruction.Create(OpCodes.Pop));
                    }

                    var rootnode = XamlTask.ParseXaml(matchedResource.GetResourceStream(), typeref);

                    Exception exception;
                    TryCoreCompile(methodOfGetResource, rootnode, context.EmbeddedResourceNameSpace, out exception);

                    yield return(Create(Newobj, constructor));
                }
            }
        }