Beispiel #1
0
        internal XPathSequenceType(Type clrType, string lexicalSequenceType, IDictionary <string, string> namespacesInScope)
        {
            if (clrType == null)
            {
                throw new ArgumentNullException("clrType");
            }

            this._ClrType = clrType;

            Type clrItemType = this._ClrType;

            if (this.ClrTypeIsEnumerable)
            {
                clrItemType = (clrItemType.IsArray) ? clrItemType.GetElementType()
               : (clrItemType.IsGenericType && clrItemType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) ? clrItemType.GetGenericArguments()[0]
               : typeof(object);
            }
            else if (this.ClrTypeIsNullableValueType)
            {
                clrItemType = Nullable.GetUnderlyingType(clrItemType);
            }

            if (this.ClrTypeIsEnumerable &&
                IsNullableValueType(clrItemType))
            {
                throw new ArgumentException("IEnumerable<Nullable<T>> or derived type is not allowed. Use IEnumerable<T> instead.", "clrType");
            }

            string lexicalItemType = null;

            if (lexicalSequenceType != null)
            {
                ParseSequenceType(lexicalSequenceType, out lexicalItemType);
            }

            if (!this.IsEmptySequence &&
                lexicalItemType == null &&
                clrType == typeof(void))
            {
                this._IsEmptySequence = true;
            }

            this._ItemType = new XPathItemType(clrItemType, lexicalItemType, namespacesInScope);
        }
Beispiel #2
0
        internal XPathSequenceType(Type clrType, string lexicalSequenceType, IDictionary<string, string> namespacesInScope)
        {
            if (clrType == null) throw new ArgumentNullException("clrType");

             this._ClrType = clrType;

             Type clrItemType = this._ClrType;

             if (this.ClrTypeIsEnumerable) {

            clrItemType = (clrItemType.IsArray) ? clrItemType.GetElementType()
               : (clrItemType.IsGenericType && clrItemType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ? clrItemType.GetGenericArguments()[0]
               : typeof(object);

             } else if (this.ClrTypeIsNullableValueType) {

            clrItemType = Nullable.GetUnderlyingType(clrItemType);
             }

             if (this.ClrTypeIsEnumerable
            && IsNullableValueType(clrItemType)) {

            throw new ArgumentException("IEnumerable<Nullable<T>> or derived type is not allowed. Use IEnumerable<T> instead.", "clrType");
             }

             string lexicalItemType = null;

             if (lexicalSequenceType != null) {
            ParseSequenceType(lexicalSequenceType, out lexicalItemType);
             }

             if (!this.IsEmptySequence
            && lexicalItemType == null
            && clrType == typeof(void)) {

            this._IsEmptySequence = true;
             }

             this._ItemType = new XPathItemType(clrItemType, lexicalItemType, namespacesInScope);
        }
        static CodeExpression GetXdmItemTypeExpression(XPathItemType itemType)
        {
            switch (itemType.Kind) {
            case XPathItemKind.AnyItem:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmAnyItemType)),
                  "Instance"
               );

            case XPathItemKind.Atomic:

               CodeExpression qnameRef = null;

               QName atomicSchemaType = GetAtomicSchemaType(itemType);

               if (atomicSchemaType.Uri == XMLSchemaNamespace) {

                  FieldInfo qnameField = typeof(QName).GetField("XS_" + atomicSchemaType.LocalName.ToUpperInvariant(), BindingFlags.Public | BindingFlags.Static);

                  if (qnameField != null) {
                     qnameRef = new CodeFieldReferenceExpression {
                        FieldName = qnameField.Name,
                        TargetObject = new CodeTypeReferenceExpression(typeof(QName))
                     };
                  }
               }

               if (qnameRef == null) {
                  qnameRef = new CodeObjectCreateExpression(
                     typeof(QName),
                     new CodePrimitiveExpression(atomicSchemaType.Uri),
                     new CodePrimitiveExpression(atomicSchemaType.LocalName)
                  );
               }

               return new CodeMethodInvokeExpression {
                  Method = new CodeMethodReferenceExpression {
                     MethodName = "BuiltInAtomicType",
                     TargetObject = new CodeTypeReferenceExpression(typeof(XdmAtomicType))
                  },
                  Parameters = {
                     qnameRef
                  }
               };

            case XPathItemKind.AnyNode:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmAnyNodeType)),
                  "Instance"
               );

            case XPathItemKind.Attribute:
            case XPathItemKind.SchemaAttribute:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmNodeKind)),
                  "Attribute"
               );

            case XPathItemKind.Comment:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmNodeKind)),
                  "Comment"
               );

            case XPathItemKind.Document:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmNodeKind)),
                  "Document"
               );

            case XPathItemKind.Element:
            case XPathItemKind.SchemaElement:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmNodeKind)),
                  "Element"
               );

            case XPathItemKind.ProcessingInstruction:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmNodeKind)),
                  "ProcessingInstruction"
               );

            case XPathItemKind.Text:
               return new CodeFieldReferenceExpression(
                  new CodeTypeReferenceExpression(typeof(XdmNodeKind)),
                  "Text"
               );

            default:
               throw new ArgumentException("itemType not supported.", "itemType");
             }
        }
        static QName GetAtomicSchemaType(XPathItemType itemType)
        {
            if (itemType.AtomicTypeOrNodeName != null) {
            return new QName(itemType.AtomicTypeOrNodeName);
             }

             Type clrType = itemType.ClrType;

             switch (Type.GetTypeCode(clrType)) {
            case TypeCode.Boolean:
               return QName.XS_BOOLEAN;

            case TypeCode.DateTime:
               return new QName(XMLSchemaNamespace, "xs:dateTime");

            case TypeCode.Decimal:
               return QName.XS_DECIMAL;

            case TypeCode.Double:
               return QName.XS_DOUBLE;

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
               return QName.XS_INTEGER;

            case TypeCode.Single:
               return QName.XS_FLOAT;

            default:
            case TypeCode.String:
               return QName.XS_STRING;

            case TypeCode.Object:

               if (clrType == typeof(QName)
                  || clrType == typeof(XmlQualifiedName)) {
                  return QName.XS_QNAME;
               }

               if (clrType == typeof(Uri)) {
                  return QName.XS_ANYURI;
               }

               return new QName(XMLSchemaNamespace, "xs:anyAtomicType");
             }
        }