예제 #1
0
        public EXamlCreateObject ProvideValue(IElementNode node, ModuleDefinition module, EXamlContext Context)
        {
            INode ntype;

            if (!node.Properties.TryGetValue(new XmlName("", "Member"), out ntype))
            {
                ntype = node.CollectionItems[0];
            }
            var member = ((ValueNode)ntype).Value as string;

            if (IsNullOrEmpty(member) || !member.Contains("."))
            {
                var lineInfo = node as IXmlLineInfo;
                throw new XamlParseException("Syntax for x:Static is [Member=][prefix:]typeName.staticMemberName", lineInfo);
            }

            var dotIdx     = member.LastIndexOf('.');
            var typename   = member.Substring(0, dotIdx);
            var membername = member.Substring(dotIdx + 1);

            var typeRef     = module.ImportReference(XmlTypeExtensions.GetTypeReference(typename, module, node as BaseNode));
            var fieldRef    = GetFieldReference(typeRef, membername, module);
            var propertyDef = GetPropertyDefinition(typeRef, membername, module);

            var ret = EXamlCreateObject.GetStaticInstance(Context, typeRef, fieldRef, propertyDef);

            if (null == ret)
            {
                throw new XamlParseException($"{membername} is not static member in type {typename}", node as IXmlLineInfo);
            }
            return(ret);
        }
예제 #2
0
        object GetValueFromLanguagePrimitive(TypeReference typeRef, ElementNode node)
        {
            var module   = Context.Module;
            var hasValue = node.CollectionItems.Count == 1 && node.CollectionItems[0] is ValueNode &&
                           ((ValueNode)node.CollectionItems[0]).Value is string;
            var    valueString = hasValue ? ((ValueNode)node.CollectionItems[0]).Value as string : string.Empty;
            object ret         = null;

            TypeDefinition typedef = typeRef.ResolveCached();

            switch (typedef.FullName)
            {
            case "System.SByte":
                if (hasValue && sbyte.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out sbyte outsbyte))
                {
                    ret = outsbyte;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.Int16":
                if (hasValue && short.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out short outshort))
                {
                    ret = outshort;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.Int32":
                if (hasValue && int.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out int outint))
                {
                    ret = outint;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.Int64":
                if (hasValue && long.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out long outlong))
                {
                    ret = outlong;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.Byte":
                if (hasValue && byte.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out byte outbyte))
                {
                    ret = outbyte;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.UInt16":
                if (hasValue && short.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out short outushort))
                {
                    ret = outushort;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.UInt32":
                if (hasValue && uint.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out uint outuint))
                {
                    ret = outuint;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.UInt64":
                if (hasValue && long.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out long outulong))
                {
                    ret = outulong;
                }
                else
                {
                    ret = 0;
                }
                break;

            case "System.Boolean":
                if (hasValue && bool.TryParse(valueString, out bool outbool))
                {
                    ret = outbool;
                }
                else
                {
                    ret = false;
                }
                break;

            case "System.String":
                ret = valueString;
                break;

            case "System.Object":
                var ctorinfo =
                    module.TypeSystem.Object.ResolveCached()
                    .Methods.FirstOrDefault(md => md.IsConstructor && !md.HasParameters);
                var ctor = module.ImportReference(ctorinfo);
                ret = Create(Newobj, ctor);
                break;

            case "System.Char":
                if (hasValue && char.TryParse(valueString, out char outchar))
                {
                    ret = outchar;
                }
                else
                {
                    ret = (char)0;
                }
                break;

            case "System.Decimal":
                decimal outdecimal;
                if (hasValue && decimal.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out outdecimal))
                {
                    ret = outdecimal;
                }
                else
                {
                    ret = (decimal)0;
                }
                break;

            case "System.Single":
                if (hasValue && float.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out float outfloat))
                {
                    ret = outfloat;
                }
                else
                {
                    ret = 0f;
                }
                break;

            case "System.Double":
                if (hasValue && double.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out double outdouble))
                {
                    ret = outdouble;
                }
                else
                {
                    ret = 0d;
                }
                break;

            case "System.TimeSpan":
                if (hasValue && TimeSpan.TryParse(valueString, CultureInfo.InvariantCulture, out TimeSpan outspan))
                {
                    ret = outspan;
                }
                else
                {
                    ret = null;
                }
                break;

            case "System.Uri":
                if (hasValue && Uri.TryCreate(valueString, UriKind.RelativeOrAbsolute, out Uri outuri))
                {
                    ret = outuri;
                }
                else
                {
                    ret = null;
                };
                break;

            default:
                ret = new EXamlCreateObject(Context, null, typeRef);
                break;
            }

            return(ret);
        }