ValueAs() 공개 메소드

public ValueAs ( Type type, IXmlNamespaceResolver nsResolver ) : object
type Type
nsResolver IXmlNamespaceResolver
리턴 object
예제 #1
0
        //------------------------------------------------------------------------
        // External type to external type
        //------------------------------------------------------------------------

        internal static XmlAtomicValue ConvertToType(XmlAtomicValue value, XmlQueryType destinationType) {
            Debug.Assert(destinationType.IsStrict && destinationType.IsAtomicValue, "Can only convert to strict atomic type.");

            // This conversion matrix should match the one in XmlILVisitor.GetXsltConvertMethod
            switch (destinationType.TypeCode) {
                case XmlTypeCode.Boolean:
                    switch (value.XmlType.TypeCode) {
                        case XmlTypeCode.Boolean:
                        case XmlTypeCode.Double:
                        case XmlTypeCode.String:
                            return new XmlAtomicValue(destinationType.SchemaType, ToBoolean(value));
                    }
                    break;

                case XmlTypeCode.DateTime:
                    if (value.XmlType.TypeCode == XmlTypeCode.String)
                        return new XmlAtomicValue(destinationType.SchemaType, ToDateTime(value.Value));
                    break;

                case XmlTypeCode.Decimal:
                    if (value.XmlType.TypeCode == XmlTypeCode.Double)
                        return new XmlAtomicValue(destinationType.SchemaType, ToDecimal(value.ValueAsDouble));
                    break;

                case XmlTypeCode.Double:
                    switch (value.XmlType.TypeCode) {
                        case XmlTypeCode.Boolean:
                        case XmlTypeCode.Double:
                        case XmlTypeCode.String:
                            return new XmlAtomicValue(destinationType.SchemaType, ToDouble(value));

                        case XmlTypeCode.Decimal:
                            return new XmlAtomicValue(destinationType.SchemaType, ToDouble((decimal) value.ValueAs(DecimalType, null)));

                        case XmlTypeCode.Int:
                        case XmlTypeCode.Long:
                            return new XmlAtomicValue(destinationType.SchemaType, ToDouble(value.ValueAsLong));
                    }
                    break;

                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                    if (value.XmlType.TypeCode == XmlTypeCode.Double)
                        return new XmlAtomicValue(destinationType.SchemaType, ToLong(value.ValueAsDouble));
                    break;

                case XmlTypeCode.String:
                    switch (value.XmlType.TypeCode) {
                        case XmlTypeCode.Boolean:
                        case XmlTypeCode.Double:
                        case XmlTypeCode.String:
                            return new XmlAtomicValue(destinationType.SchemaType, ToString(value));

                        case XmlTypeCode.DateTime:
                            return new XmlAtomicValue(destinationType.SchemaType, ToString(value.ValueAsDateTime));
                    }
                    break;
            }

            Debug.Fail("Conversion from " + value.XmlType.QualifiedName.Name + " to " + destinationType + " is not supported.");
            return value;
        }
        /// <summary>
        /// Fold a XsltConvert applied to a Literal into another Literal.  If the fold results in some kind of
        /// conversion error, or if the QilExpression cannot represent the result as a Literal, return an unfolded
        /// XsltConvert expression.
        /// </summary>
        private QilNode FoldXsltConvert(QilNode ndLiteral, XmlQueryType typTarget) {
            try {
                if (typTarget.IsAtomicValue) {
                    // Convert the literal to an XmlAtomicValue
                    XmlAtomicValue value = new XmlAtomicValue(ndLiteral.XmlType.SchemaType, ExtractLiteralValue(ndLiteral));
                    value = XsltConvert.ConvertToType(value, typTarget);

                    if (typTarget == TypeFactory.StringX)
                        return this.f.LiteralString(value.Value);
                    else if (typTarget == TypeFactory.IntX)
                        return this.f.LiteralInt32(value.ValueAsInt);
                    else if (typTarget == TypeFactory.IntegerX)
                        return this.f.LiteralInt64(value.ValueAsLong);
                    else if (typTarget == TypeFactory.DecimalX)
                        return this.f.LiteralDecimal((decimal) value.ValueAs(XsltConvert.DecimalType));
                    else if (typTarget == TypeFactory.DoubleX)
                        return this.f.LiteralDouble(value.ValueAsDouble);
                    else if (typTarget == TypeFactory.BooleanX)
                        return value.ValueAsBoolean ? this.f.True() : this.f.False();
                }
            }
            catch (OverflowException) {}
            catch (FormatException) {}

            // Conversion error or QilExpression cannot represent resulting literal
            return this.f.XsltConvert(ndLiteral, typTarget);
        }