internal TypeDesc(string name, string fullName, XmlSchemaType dataType, TypeKind kind, TypeDesc baseTypeDesc, TypeFlags flags, string formatterName)
 {
     this.name = name.Replace('+', '.');
     this.fullName = fullName.Replace('+', '.');
     this.kind = kind;
     this.baseTypeDesc = baseTypeDesc;
     this.flags = flags;
     this.isXsdType = kind == TypeKind.Primitive;
     if (this.isXsdType)
     {
         this.weight = 1;
     }
     else if (kind == TypeKind.Enum)
     {
         this.weight = 2;
     }
     else if (this.kind == TypeKind.Root)
     {
         this.weight = -1;
     }
     else
     {
         this.weight = (baseTypeDesc == null) ? 0 : (baseTypeDesc.Weight + 1);
     }
     this.dataType = dataType;
     this.formatterName = formatterName;
 }
 internal FieldModel(string name, Type fieldType, TypeDesc fieldTypeDesc, bool checkSpecified, bool checkShouldPersist, bool readOnly)
 {
     this.fieldTypeDesc = fieldTypeDesc;
     this.name = name;
     this.fieldType = fieldType;
     this.checkSpecified = checkSpecified ? SpecifiedAccessor.ReadWrite : SpecifiedAccessor.None;
     this.checkShouldPersist = checkShouldPersist;
     this.readOnly = readOnly;
 }
示例#3
0
文件: types.cs 项目: ArildF/masters
 public TypeDesc(string name, string fullName, TypeKind kind, TypeDesc baseTypeDesc, TypeFlags flags) {
     this.name = name.Replace('+', '.');
     this.fullName = fullName.Replace('+', '.');
     this.kind = kind;
     if (baseTypeDesc != null) {
         this.baseTypeDesc = baseTypeDesc;
     }
     this.flags = flags;
     this.isXsdType = kind == TypeKind.Primitive;
 }
 internal string GetStringForArrayMember(string arrayName, string subscript, TypeDesc arrayTypeDesc)
 {
     if (!arrayTypeDesc.UseReflection)
     {
         return (arrayName + "[" + subscript + "]");
     }
     string typeFullName = arrayTypeDesc.IsCollection ? arrayTypeDesc.CSharpName : typeof(Array).FullName;
     string reflectionVariable = this.GetReflectionVariable(typeFullName, "0");
     return (reflectionVariable + "[" + arrayName + ", " + subscript + "]");
 }
 internal XmlSerializationILGen(TypeScope[] scopes, string access, string className) {
     this.scopes = scopes;
     if (scopes.Length > 0) {
         stringTypeDesc = scopes[0].GetTypeDesc(typeof(string));
         qnameTypeDesc = scopes[0].GetTypeDesc(typeof(XmlQualifiedName));
     }
     this.raCodeGen = new ReflectionAwareILGen();
     this.className = className;
     System.Diagnostics.Debug.Assert(access == "public");
     this.typeAttributes = TypeAttributes.Public;
 }
 internal XmlSerializationCodeGen(IndentedWriter writer, TypeScope[] scopes, string access, string className) {
     this.writer = writer;
     this.scopes = scopes;
     if (scopes.Length > 0) {
         stringTypeDesc = scopes[0].GetTypeDesc(typeof(string));
         qnameTypeDesc = scopes[0].GetTypeDesc(typeof(XmlQualifiedName));
     }
     this.raCodeGen = new ReflectionAwareCodeGen(writer);
     this.className = className;
     this.access = access;
 }
 private string GetNullableType(TypeDesc td)
 {
     if (td.IsMappedType || (!td.IsValueType && (base.Elements[0].IsSoap || (td.ArrayElementTypeDesc == null))))
     {
         return td.FullName;
     }
     if (td.ArrayElementTypeDesc != null)
     {
         return (this.GetNullableType(td.ArrayElementTypeDesc) + "[]");
     }
     return ("System.Nullable`1[" + td.FullName + "]");
 }
 private void AddElementMetadata(CodeAttributeDeclarationCollection metadata, string elementName, TypeDesc typeDesc, bool isNullable)
 {
     CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(SoapElementAttribute).FullName);
     if (elementName != null)
     {
         declaration.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(elementName)));
     }
     if ((typeDesc != null) && typeDesc.IsAmbiguousDataType)
     {
         declaration.Arguments.Add(new CodeAttributeArgument("DataType", new CodePrimitiveExpression(typeDesc.DataType.Name)));
     }
     if (isNullable)
     {
         declaration.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(true)));
     }
     metadata.Add(declaration);
 }
 private static void AddNonXsdPrimitive(Type type, string dataTypeName, string ns, string formatterName, XmlQualifiedName baseTypeName, XmlSchemaFacet[] facets, TypeFlags flags)
 {
     XmlSchemaSimpleType dataType = new XmlSchemaSimpleType {
         Name = dataTypeName
     };
     XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction {
         BaseTypeName = baseTypeName
     };
     foreach (XmlSchemaFacet facet in facets)
     {
         restriction.Facets.Add(facet);
     }
     dataType.Content = restriction;
     TypeDesc desc = new TypeDesc(type, false, dataType, formatterName, flags);
     if (primitiveTypes[type] == null)
     {
         primitiveTypes.Add(type, desc);
     }
     primitiveDataTypes.Add(dataType, desc);
     primitiveNames.Add(dataTypeName, ns, desc);
 }
 internal FieldModel(MemberInfo memberInfo, Type fieldType, TypeDesc fieldTypeDesc)
 {
     this.name = memberInfo.Name;
     this.fieldType = fieldType;
     this.fieldTypeDesc = fieldTypeDesc;
     this.checkShouldPersist = memberInfo.DeclaringType.GetMethod("ShouldSerialize" + memberInfo.Name, new Type[0]) != null;
     FieldInfo field = memberInfo.DeclaringType.GetField(memberInfo.Name + "Specified");
     if (field != null)
     {
         if (field.FieldType != typeof(bool))
         {
             throw new InvalidOperationException(Res.GetString("XmlInvalidSpecifiedType", new object[] { field.Name, field.FieldType.FullName, typeof(bool).FullName }));
         }
         this.checkSpecified = field.IsInitOnly ? SpecifiedAccessor.ReadOnly : SpecifiedAccessor.ReadWrite;
     }
     else
     {
         PropertyInfo property = memberInfo.DeclaringType.GetProperty(memberInfo.Name + "Specified");
         if (property != null)
         {
             if (StructModel.CheckPropertyRead(property))
             {
                 this.checkSpecified = property.CanWrite ? SpecifiedAccessor.ReadWrite : SpecifiedAccessor.ReadOnly;
             }
             if ((this.checkSpecified != SpecifiedAccessor.None) && (property.PropertyType != typeof(bool)))
             {
                 throw new InvalidOperationException(Res.GetString("XmlInvalidSpecifiedType", new object[] { property.Name, property.PropertyType.FullName, typeof(bool).FullName }));
             }
         }
     }
     if (memberInfo is PropertyInfo)
     {
         this.readOnly = !((PropertyInfo) memberInfo).CanWrite;
         this.isProperty = true;
     }
     else if (memberInfo is FieldInfo)
     {
         this.readOnly = ((FieldInfo) memberInfo).IsInitOnly;
     }
 }
        bool CanOptimizeWriteListSequence(TypeDesc listElementTypeDesc) {

            // check to see if we can write values of the attribute sequentially
            // currently we have only one data type (XmlQualifiedName) that we can not write "inline", 
            // because we need to output xmlns:qx="..." for each of the qnames

            return (listElementTypeDesc != null && listElementTypeDesc != QnameTypeDesc);
        }
 void WritePrimitiveValue(TypeDesc typeDesc, string source, bool isElement) {
     if (typeDesc == StringTypeDesc || typeDesc.FormatterName == "String") {
         Writer.Write(source);
     }
     else {
         if (!typeDesc.HasCustomFormatter) {
             Writer.Write(typeof(XmlConvert).FullName);
             Writer.Write(".ToString((");
             Writer.Write(typeDesc.CSharpName);
             Writer.Write(")");
             Writer.Write(source);
             Writer.Write(")");
         } 
         else {
             Writer.Write("From");
             Writer.Write(typeDesc.FormatterName);
             Writer.Write("(");
             Writer.Write(source);
             Writer.Write(")");
         }
     }
 }
 object GetDefaultValue(TypeDesc fieldTypeDesc, SoapAttributes a) {
     if (a.SoapDefaultValue == null || a.SoapDefaultValue == DBNull.Value) return null;
     if (!(fieldTypeDesc.Kind == TypeKind.Primitive || fieldTypeDesc.Kind == TypeKind.Enum))  {
         a.SoapDefaultValue = null;
         return a.SoapDefaultValue;
     }
     // for enums validate and return a string representation
     if (fieldTypeDesc.Kind == TypeKind.Enum) {
         if (fieldTypeDesc != typeScope.GetTypeDesc(a.SoapDefaultValue.GetType()))
             throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultEnumValue, a.SoapDefaultValue.GetType().FullName, fieldTypeDesc.FullName));
         string strValue = Enum.Format(a.SoapDefaultValue.GetType(), a.SoapDefaultValue, "G").Replace(",", " ");
         string numValue = Enum.Format(a.SoapDefaultValue.GetType(), a.SoapDefaultValue, "D");
         if (strValue == numValue) // means enum value wasn't recognized
             throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, strValue, a.SoapDefaultValue.GetType().FullName));
         return strValue;
     }
     return a.SoapDefaultValue;
 }
 NullableMapping CreateNullableMapping(TypeMapping baseMapping, TypeDesc typeDesc) {
     TypeMapping existingMapping = (TypeMapping)types[typeDesc.Name, baseMapping.Namespace];
     NullableMapping mapping;
     if (existingMapping != null) {
         if (existingMapping is NullableMapping) {
             mapping = (NullableMapping)existingMapping;
             if (mapping.BaseMapping is PrimitiveMapping && baseMapping is PrimitiveMapping)
                 return mapping;
             else if (mapping.BaseMapping == baseMapping) {
                 return mapping;
             }
             else {
                 throw new InvalidOperationException(Res.GetString(Res.XmlTypesDuplicate, typeDesc.FullName, existingMapping.TypeDesc.FullName, typeDesc.Name, existingMapping.Namespace));
             }
         }
         else if (!(baseMapping is PrimitiveMapping)){
             throw new InvalidOperationException(Res.GetString(Res.XmlTypesDuplicate, typeDesc.FullName, existingMapping.TypeDesc.FullName, typeDesc.Name, existingMapping.Namespace));
         }
     }
     mapping = new NullableMapping();
     mapping.BaseMapping = baseMapping;
     mapping.TypeDesc = typeDesc;
     mapping.TypeName = baseMapping.TypeName;
     mapping.Namespace = baseMapping.Namespace;
     mapping.IncludeInSchema = false; //baseMapping.IncludeInSchema;
     types.Add(typeDesc.Name, baseMapping.Namespace, mapping);
     typeScope.AddTypeMapping(mapping);
     return mapping;
 }
 TypeMapping GetTypeMapping(string typeName, string ns, TypeDesc typeDesc) {
     TypeMapping mapping = (TypeMapping)types[typeName, ns];
     if (mapping == null) return null;
     if (mapping.TypeDesc != typeDesc) 
         throw new InvalidOperationException(Res.GetString(Res.XmlTypesDuplicate, typeDesc.FullName, mapping.TypeDesc.FullName, typeName, ns));
     return mapping;
 }
示例#16
0
 protected TypeModel(Type type, TypeDesc typeDesc, ModelScope scope)
 {
     _scope    = scope;
     _type     = type;
     _typeDesc = typeDesc;
 }
示例#17
0
 internal ArrayModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope)
 {
 }
        internal string GetStringForMember(string obj, string memberName, TypeDesc typeDesc){
            if (!typeDesc.UseReflection)
                return obj+".@"+memberName;

            TypeDesc saveTypeDesc = typeDesc;
            while(typeDesc!=null){
                string typeFullName = typeDesc.CSharpName;
                string memberInfoName = GetReflectionVariable(typeFullName, memberName);
                if(memberInfoName != null)
                    return memberInfoName+"["+obj+"]";
                // member may be part of the basetype 
                typeDesc = typeDesc.BaseTypeDesc;
                if (typeDesc != null && !typeDesc.UseReflection)
                    return "(("+typeDesc.CSharpName+")"+obj+").@"+memberName;
            }
            //throw GetReflectionVariableException(saveTypeDesc.CSharpName,memberName); 
            // NOTE, Microsoft:Must never happen. If it does let the code
            // gen continue to help debugging what's gone wrong.
            // Eventually the compilation will fail.
            return "["+obj+"]";
        }
        string WriteTypeInfo(TypeScope scope, TypeDesc typeDesc, Type type){
            InitTheFirstTime();
            string typeFullName = typeDesc.CSharpName;
            string typeVariable = (string)reflectionVariables[typeFullName];
            if (typeVariable != null)
                return typeVariable;

            if (type.IsArray)
            {
                typeVariable = GenerateVariableName("array", typeDesc.CSharpName);
                TypeDesc elementTypeDesc = typeDesc.ArrayElementTypeDesc;
                if (elementTypeDesc.UseReflection)
                {
                    string elementTypeVariable = WriteTypeInfo(scope, elementTypeDesc, scope.GetTypeFromTypeDesc(elementTypeDesc));
                    writer.WriteLine("static "+typeof(Type).FullName+" "+typeVariable +" = " + elementTypeVariable + ".MakeArrayType();");
                }
                else
                {
                    string assemblyVariable = WriteAssemblyInfo(type);
                    writer.Write("static "+typeof(Type).FullName+" "+typeVariable +" = "+assemblyVariable+".GetType(");
                    WriteQuotedCSharpString(type.FullName);
                    writer.WriteLine(");");
                }
            }
            else
            {
                typeVariable = GenerateVariableName("type", typeDesc.CSharpName);

                Type parameterType = Nullable.GetUnderlyingType(type);
                if (parameterType != null)
                {
                    string parameterTypeVariable = WriteTypeInfo(scope, scope.GetTypeDesc(parameterType), parameterType);
                    writer.WriteLine("static "+typeof(Type).FullName+" "+typeVariable +" = typeof(System.Nullable<>).MakeGenericType(new " + typeof(Type).FullName + "[] {"+parameterTypeVariable+"});");
                }
                else
                {
                    string assemblyVariable = WriteAssemblyInfo(type);
                    writer.Write("static "+typeof(Type).FullName+" "+typeVariable +" = "+assemblyVariable+".GetType(");
                    WriteQuotedCSharpString(type.FullName);
                    writer.WriteLine(");");
                }
            }
            
            reflectionVariables.Add(typeFullName, typeVariable);

            TypeMapping mapping = scope.GetTypeMappingFromTypeDesc(typeDesc);
            if (mapping != null)
                WriteMappingInfo(mapping, typeVariable, type);
            if (typeDesc.IsCollection || typeDesc.IsEnumerable){// Arrays use the generic item_Array
                TypeDesc elementTypeDesc = typeDesc.ArrayElementTypeDesc;
                if (elementTypeDesc.UseReflection)
                    WriteTypeInfo(scope, elementTypeDesc, scope.GetTypeFromTypeDesc(elementTypeDesc));
                WriteCollectionInfo(typeVariable, typeDesc, type);
            }
            return typeVariable;
        }
        void WriteChoiceTypeCheck(string source, string fullTypeName, bool useReflection, ChoiceIdentifierAccessor choice, string enumName, TypeDesc typeDesc) {

            Writer.Write("if (((object)");
            Writer.Write(source);
            Writer.Write(") != null && !(");
            WriteInstanceOf(source, fullTypeName, useReflection);
            Writer.Write(")) throw CreateMismatchChoiceException(");
            WriteQuotedCSharpString(typeDesc.FullName);
            Writer.Write(", ");
            WriteQuotedCSharpString(choice.MemberName);
            Writer.Write(", ");
            WriteQuotedCSharpString(enumName);
            Writer.WriteLine(");");
        }
        void WriteArray(string source, string choiceSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc arrayTypeDesc) {
            if (elements.Length == 0 && text == null) return;
            Writer.WriteLine("{");
            Writer.Indent++;
            string arrayTypeName = arrayTypeDesc.CSharpName;
            WriteArrayLocalDecl(arrayTypeName, "a", source, arrayTypeDesc);
            if (arrayTypeDesc.IsNullable) {
                Writer.WriteLine("if (a != null) {");
                Writer.Indent++;
            }

            if (choice != null) {
                bool choiceUseReflection = choice.Mapping.TypeDesc.UseReflection;
                string choiceFullName = choice.Mapping.TypeDesc.CSharpName;
                WriteArrayLocalDecl(choiceFullName+"[]", "c", choiceSource, choice.Mapping.TypeDesc);
                // write check for the choice identifier array
                Writer.WriteLine("if (c == null || c.Length < a.Length) {");
                Writer.Indent++;
                Writer.Write("throw CreateInvalidChoiceIdentifierValueException(");
                WriteQuotedCSharpString(choice.Mapping.TypeDesc.FullName);
                Writer.Write(", ");
                WriteQuotedCSharpString(choice.MemberName);
                Writer.Write(");");
                Writer.Indent--;
                Writer.WriteLine("}");
            }

            WriteArrayItems(elements, text, choice, arrayTypeDesc, "a", "c");
            if (arrayTypeDesc.IsNullable) {
                Writer.Indent--;
                Writer.WriteLine("}");
            }
            Writer.Indent--;
            Writer.WriteLine("}");
        }
        void WriteMember(string source, AttributeAccessor attribute, TypeDesc memberTypeDesc, string parent) {
            if (memberTypeDesc.IsAbstract) return;
            if (memberTypeDesc.IsArrayLike) {
                Writer.WriteLine("{");
                Writer.Indent++;
                string fullTypeName = memberTypeDesc.CSharpName;
                WriteArrayLocalDecl(fullTypeName, "a", source, memberTypeDesc);
                if (memberTypeDesc.IsNullable) {
                    Writer.WriteLine("if (a != null) {");
                    Writer.Indent++;
                }
                if (attribute.IsList) {
                    if (CanOptimizeWriteListSequence(memberTypeDesc.ArrayElementTypeDesc)) {
                        Writer.Write("Writer.WriteStartAttribute(null, ");
                        WriteQuotedCSharpString(attribute.Name);
                        Writer.Write(", ");
                        string ns = attribute.Form == XmlSchemaForm.Qualified ? attribute.Namespace : String.Empty;
                        if (ns != null) {
                            WriteQuotedCSharpString(ns);
                        }
                        else {
                            Writer.Write("null");
                        }
                        Writer.WriteLine(");");
                    }
                    else {
                        Writer.Write(typeof(StringBuilder).FullName);
                        Writer.Write(" sb = new ");
                        Writer.Write(typeof(StringBuilder).FullName);
                        Writer.WriteLine("();");
                    }
                }
                TypeDesc arrayElementTypeDesc = memberTypeDesc.ArrayElementTypeDesc;

                if (memberTypeDesc.IsEnumerable) {
                    Writer.Write(" e = ");
                    Writer.Write(typeof(IEnumerator).FullName);
                    if (memberTypeDesc.IsPrivateImplementation) {
                        Writer.Write("((");
                        Writer.Write(typeof(IEnumerable).FullName);
                        Writer.WriteLine(").GetEnumerator();");
                    }
                    else if(memberTypeDesc.IsGenericInterface) {
                        if (memberTypeDesc.UseReflection) {
                            // we use wildcard method name for generic GetEnumerator method, so we cannot use GetStringForMethodInvoke call here
                            Writer.Write("(");
                            Writer.Write(typeof(IEnumerator).FullName);
                            Writer.Write(")");
                            Writer.Write(RaCodeGen.GetReflectionVariable(memberTypeDesc.CSharpName, "System.Collections.Generic.IEnumerable*"));
                            Writer.WriteLine(".Invoke(a, new object[0]);");
                        }
                        else {
                            Writer.Write("((System.Collections.Generic.IEnumerable<");
                            Writer.Write(arrayElementTypeDesc.CSharpName);
                            Writer.WriteLine(">)a).GetEnumerator();");
                        }
                    }
                    else {
                        if (memberTypeDesc.UseReflection) {
                            Writer.Write("(");
                            Writer.Write(typeof(IEnumerator).FullName);
                            Writer.Write(")");
                        }
                        Writer.Write(RaCodeGen.GetStringForMethodInvoke("a", memberTypeDesc.CSharpName, "GetEnumerator", memberTypeDesc.UseReflection));
                        Writer.WriteLine(";");
                    }
                    Writer.WriteLine("if (e != null)");
                    Writer.WriteLine("while (e.MoveNext()) {");
                    Writer.Indent++;

                    string arrayTypeFullName = arrayElementTypeDesc.CSharpName;
                    WriteLocalDecl(arrayTypeFullName, "ai", "e.Current", arrayElementTypeDesc.UseReflection);
                }
                else {
                    Writer.Write("for (int i = 0; i < ");
                    if (memberTypeDesc.IsArray) {
                        Writer.WriteLine("a.Length; i++) {");
                    }
                    else {
                        Writer.Write("((");
                        Writer.Write(typeof(ICollection).FullName);
                        Writer.WriteLine(")a).Count; i++) {");
                    }
                    Writer.Indent++;
                    string arrayTypeFullName = arrayElementTypeDesc.CSharpName;
                    WriteLocalDecl(arrayTypeFullName, "ai", RaCodeGen.GetStringForArrayMember("a", "i", memberTypeDesc), arrayElementTypeDesc.UseReflection);
                }
                if (attribute.IsList) {
                    // check to see if we can write values of the attribute sequentially
                    if (CanOptimizeWriteListSequence(memberTypeDesc.ArrayElementTypeDesc)) {
                        Writer.WriteLine("if (i != 0) Writer.WriteString(\" \");");
                        Writer.Write("WriteValue(");
                    }
                    else {
                        Writer.WriteLine("if (i != 0) sb.Append(\" \");");
                        Writer.Write("sb.Append(");
                    }
                    if (attribute.Mapping is EnumMapping)
                        WriteEnumValue((EnumMapping)attribute.Mapping, "ai");
                    else
                        WritePrimitiveValue(arrayElementTypeDesc, "ai", true);
                    Writer.WriteLine(");");
                }
                else {
                    WriteAttribute("ai", attribute, parent);
                }
                Writer.Indent--;
                Writer.WriteLine("}");
                if (attribute.IsList) {
                    // check to see if we can write values of the attribute sequentially
                    if (CanOptimizeWriteListSequence(memberTypeDesc.ArrayElementTypeDesc)) {
                        Writer.WriteLine("Writer.WriteEndAttribute();");
                    }
                    else {
                        Writer.WriteLine("if (sb.Length != 0) {");
                        Writer.Indent++;
                    
                        Writer.Write("WriteAttribute(");
                        WriteQuotedCSharpString(attribute.Name);
                        Writer.Write(", ");
                        string ns = attribute.Form == XmlSchemaForm.Qualified ? attribute.Namespace : String.Empty;
                        if (ns != null) {
                            WriteQuotedCSharpString(ns);
                            Writer.Write(", ");
                        }
                        Writer.WriteLine("sb.ToString());");
                        Writer.Indent--;
                        Writer.WriteLine("}");
                    }
                }

                if (memberTypeDesc.IsNullable) {
                    Writer.Indent--;
                    Writer.WriteLine("}");
                }
                Writer.Indent--;
                Writer.WriteLine("}");
            }
            else {
                WriteAttribute(source, attribute, parent);
            }
        }
示例#23
0
 internal PrimitiveModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope)
 {
 }
 void WriteMember(string source, string choiceSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc memberTypeDesc, bool writeAccessors) {
     if (memberTypeDesc.IsArrayLike && 
         !(elements.Length == 1 && elements[0].Mapping is ArrayMapping))
         WriteArray(source, choiceSource, elements, text, choice, memberTypeDesc);
     else
         WriteElements(source, choiceSource, elements, text, choice, "a", writeAccessors, memberTypeDesc.IsNullable);
 }
示例#25
0
文件: Types.cs 项目: johnhhm/corefx
 private static void AddPrimitive(Type type, string dataTypeName, string formatterName, TypeFlags flags)
 {
     XmlSchemaSimpleType dataType = new XmlSchemaSimpleType();
     dataType.Name = dataTypeName;
     TypeDesc typeDesc = new TypeDesc(type, true, dataType, formatterName, flags);
     if (s_primitiveTypes[type] == null)
         s_primitiveTypes.Add(type, typeDesc);
     s_primitiveDataTypes.Add(dataType, typeDesc);
     s_primitiveNames.Add(dataTypeName, XmlSchema.Namespace, typeDesc);
 }
        void WriteArrayItems(ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc arrayTypeDesc, string arrayName, string choiceName) {
            TypeDesc arrayElementTypeDesc = arrayTypeDesc.ArrayElementTypeDesc;

            if (arrayTypeDesc.IsEnumerable) {
                Writer.Write(typeof(IEnumerator).FullName);
                Writer.Write(" e = ");
                if (arrayTypeDesc.IsPrivateImplementation) {
                    Writer.Write("((");
                    Writer.Write(typeof(IEnumerable).FullName);
                    Writer.Write(")");
                    Writer.Write(arrayName);
                    Writer.WriteLine(").GetEnumerator();");
                }
                else if(arrayTypeDesc.IsGenericInterface) {
                    if (arrayTypeDesc.UseReflection) {
                        // we use wildcard method name for generic GetEnumerator method, so we cannot use GetStringForMethodInvoke call here
                        Writer.Write("(");
                        Writer.Write(typeof(IEnumerator).FullName);
                        Writer.Write(")");
                        Writer.Write(RaCodeGen.GetReflectionVariable(arrayTypeDesc.CSharpName, "System.Collections.Generic.IEnumerable*"));
                        Writer.Write(".Invoke(");
                        Writer.Write(arrayName);
                        Writer.WriteLine(", new object[0]);");
                    }
                    else {
                        Writer.Write("((System.Collections.Generic.IEnumerable<");
                        Writer.Write(arrayElementTypeDesc.CSharpName);
                        Writer.Write(">)");
                        Writer.Write(arrayName);
                        Writer.WriteLine(").GetEnumerator();");
                    }
                }
                else {
                    if (arrayTypeDesc.UseReflection) {
                        Writer.Write("(");
                        Writer.Write(typeof(IEnumerator).FullName);
                        Writer.Write(")");
                    }
                    Writer.Write(RaCodeGen.GetStringForMethodInvoke(arrayName, arrayTypeDesc.CSharpName, "GetEnumerator", arrayTypeDesc.UseReflection));
                    Writer.WriteLine(";");
                }
                Writer.WriteLine("if (e != null)");
                Writer.WriteLine("while (e.MoveNext()) {");
                Writer.Indent++;
                string arrayTypeFullName = arrayElementTypeDesc.CSharpName;
                WriteLocalDecl(arrayTypeFullName, arrayName+"i", "e.Current", arrayElementTypeDesc.UseReflection);
                WriteElements(arrayName + "i", choiceName + "i", elements, text, choice, arrayName + "a", true, true);
            }
            else {
                Writer.Write("for (int i");
                Writer.Write(arrayName);
                Writer.Write(" = 0; i");
                Writer.Write(arrayName);
                Writer.Write(" < ");
                if (arrayTypeDesc.IsArray) {
                    Writer.Write(arrayName);
                    Writer.Write(".Length");
                }
                else {
                    Writer.Write("((");
                    Writer.Write(typeof(ICollection).FullName);
                    Writer.Write(")");
                    Writer.Write(arrayName);
                    Writer.Write(").Count");
                }
                Writer.Write("; i");
                Writer.Write(arrayName);
                Writer.WriteLine("++) {");
                Writer.Indent++;
                int count = elements.Length + (text == null ? 0 : 1);
                if (count > 1) {
                    string arrayTypeFullName = arrayElementTypeDesc.CSharpName;
                    WriteLocalDecl(arrayTypeFullName, arrayName+"i", RaCodeGen.GetStringForArrayMember(arrayName, "i"+arrayName, arrayTypeDesc), arrayElementTypeDesc.UseReflection);
                    if (choice != null) {
                        string choiceFullName = choice.Mapping.TypeDesc.CSharpName;
                        WriteLocalDecl(choiceFullName, choiceName+"i", RaCodeGen.GetStringForArrayMember(choiceName, "i"+arrayName, choice.Mapping.TypeDesc), choice.Mapping.TypeDesc.UseReflection);
                    }
                    WriteElements(arrayName + "i", choiceName + "i", elements, text, choice, arrayName + "a", true, arrayElementTypeDesc.IsNullable);
                }
                else {
                    WriteElements(RaCodeGen.GetStringForArrayMember(arrayName , "i" + arrayName, arrayTypeDesc), elements, text, choice, arrayName + "a", true, arrayElementTypeDesc.IsNullable);
                }
            }
            Writer.Indent--;
            Writer.WriteLine("}");
        }
示例#27
0
文件: Types.cs 项目: johnhhm/corefx
 private static void AddNonXsdPrimitive(Type type, string dataTypeName, string ns, string formatterName, XmlQualifiedName baseTypeName, XmlSchemaFacet[] facets, TypeFlags flags)
 {
     XmlSchemaSimpleType dataType = new XmlSchemaSimpleType();
     dataType.Name = dataTypeName;
     TypeDesc typeDesc = new TypeDesc(type, false, dataType, formatterName, flags);
     if (s_primitiveTypes[type] == null)
         s_primitiveTypes.Add(type, typeDesc);
     s_primitiveDataTypes.Add(dataType, typeDesc);
     s_primitiveNames.Add(dataTypeName, ns, typeDesc);
 }
 void WriteArrayLocalDecl(string typeName, string variableName, string initValue, TypeDesc arrayTypeDesc) {
     RaCodeGen.WriteArrayLocalDecl(typeName,  variableName,  initValue,  arrayTypeDesc);
 }
示例#29
0
文件: Types.cs 项目: johnhhm/corefx
        private TypeDesc ImportTypeDesc(Type type, MemberInfo memberInfo, bool directReference)
        {
            TypeDesc typeDesc = null;
            TypeKind kind;
            Type arrayElementType = null;
            Type baseType = null;
            TypeFlags flags = 0;
            Exception exception = null;

            if (!type.GetTypeInfo().IsVisible)
            {
                flags |= TypeFlags.Unsupported;
                exception = new InvalidOperationException(SR.Format(SR.XmlTypeInaccessible, type.FullName));
            }
            else if (directReference && (type.GetTypeInfo().IsAbstract && type.GetTypeInfo().IsSealed))
            {
                flags |= TypeFlags.Unsupported;
                exception = new InvalidOperationException(SR.Format(SR.XmlTypeStatic, type.FullName));
            }

            if (!type.GetTypeInfo().IsValueType)
                flags |= TypeFlags.Reference;

            if (type == typeof(object))
            {
                kind = TypeKind.Root;
                flags |= TypeFlags.HasDefaultConstructor;
            }
            else if (type == typeof(ValueType))
            {
                kind = TypeKind.Enum;
                flags |= TypeFlags.Unsupported;
                if (exception == null)
                {
                    exception = new NotSupportedException(SR.Format(SR.XmlSerializerUnsupportedType, type.FullName));
                }
            }
            else if (type == typeof(void))
            {
                kind = TypeKind.Void;
            }
            else if (typeof(IXmlSerializable).IsAssignableFrom(type))
            {
                kind = TypeKind.Serializable;
                flags |= TypeFlags.Special | TypeFlags.CanBeElementValue;
                flags |= GetConstructorFlags(type, ref exception);
            }
            else if (type.IsArray)
            {
                kind = TypeKind.Array;
                if (type.GetArrayRank() > 1)
                {
                    flags |= TypeFlags.Unsupported;
                    if (exception == null)
                    {
                        exception = new NotSupportedException(SR.Format(SR.XmlUnsupportedRank, type.FullName));
                    }
                }
                arrayElementType = type.GetElementType();
                flags |= TypeFlags.HasDefaultConstructor;
            }
            else if (typeof(ICollection).IsAssignableFrom(type))
            {
                kind = TypeKind.Collection;
                arrayElementType = GetCollectionElementType(type, memberInfo == null ? null : memberInfo.DeclaringType.FullName + "." + memberInfo.Name);
                flags |= GetConstructorFlags(type, ref exception);
            }
            else if (type == typeof(XmlQualifiedName))
            {
                kind = TypeKind.Primitive;
            }
            else if (type.GetTypeInfo().IsPrimitive)
            {
                kind = TypeKind.Primitive;
                flags |= TypeFlags.Unsupported;
                if (exception == null)
                {
                    exception = new NotSupportedException(SR.Format(SR.XmlSerializerUnsupportedType, type.FullName));
                }
            }
            else if (type.GetTypeInfo().IsEnum)
            {
                kind = TypeKind.Enum;
            }
            else if (type.GetTypeInfo().IsValueType)
            {
                kind = TypeKind.Struct;
                if (IsOptionalValue(type))
                {
                    baseType = type.GetGenericArguments()[0];
                    flags |= TypeFlags.OptionalValue;
                }
                else
                {
                    baseType = type.GetTypeInfo().BaseType;
                }
                if (type.GetTypeInfo().IsAbstract) flags |= TypeFlags.Abstract;
            }
            else if (type.GetTypeInfo().IsClass)
            {
                if (type == typeof(XmlAttribute))
                {
                    kind = TypeKind.Attribute;
                    flags |= TypeFlags.Special | TypeFlags.CanBeAttributeValue;
                }
                else if (typeof(XmlNode).IsAssignableFrom(type))
                {
                    kind = TypeKind.Node;
                    baseType = type.GetTypeInfo().BaseType;
                    flags |= TypeFlags.Special | TypeFlags.CanBeElementValue | TypeFlags.CanBeTextValue;
                    if (typeof(XmlText).IsAssignableFrom(type))
                        flags &= ~TypeFlags.CanBeElementValue;
                    else if (typeof(XmlElement).IsAssignableFrom(type))
                        flags &= ~TypeFlags.CanBeTextValue;
                    else if (type.IsAssignableFrom(typeof(XmlAttribute)))
                        flags |= TypeFlags.CanBeAttributeValue;
                }
                else
                {
                    kind = TypeKind.Class;
                    baseType = type.GetTypeInfo().BaseType;
                    if (type.GetTypeInfo().IsAbstract)
                        flags |= TypeFlags.Abstract;
                }
            }
            else if (type.GetTypeInfo().IsInterface)
            {
                kind = TypeKind.Void;
                flags |= TypeFlags.Unsupported;
                if (exception == null)
                {
                    if (memberInfo == null)
                    {
                        exception = new NotSupportedException(SR.Format(SR.XmlUnsupportedInterface, type.FullName));
                    }
                    else
                    {
                        exception = new NotSupportedException(SR.Format(SR.XmlUnsupportedInterfaceDetails, memberInfo.DeclaringType.FullName + "." + memberInfo.Name, type.FullName));
                    }
                }
            }
            else
            {
                kind = TypeKind.Void;
                flags |= TypeFlags.Unsupported;
                if (exception == null)
                {
                    exception = new NotSupportedException(SR.Format(SR.XmlSerializerUnsupportedType, type.FullName));
                }
            }

            // check to see if the type has public default constructor for classes
            if (kind == TypeKind.Class && !type.GetTypeInfo().IsAbstract)
            {
                flags |= GetConstructorFlags(type, ref exception);
            }
            // check if a struct-like type is enumerable
            if (kind == TypeKind.Struct || kind == TypeKind.Class)
            {
                if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    arrayElementType = GetEnumeratorElementType(type, ref flags);
                    kind = TypeKind.Enumerable;

                    // GetEnumeratorElementType checks for the security attributes on the GetEnumerator(), Add() methods and Current property, 
                    // we need to check the MoveNext() and ctor methods for the security attribues
                    flags |= GetConstructorFlags(type, ref exception);
                }
            }
            typeDesc = new TypeDesc(type, CodeIdentifier.MakeValid(TypeName(type)), type.ToString(), kind, null, flags, null);
            typeDesc.Exception = exception;

            if (directReference && (typeDesc.IsClass || kind == TypeKind.Serializable))
                typeDesc.CheckNeedConstructor();

            if (typeDesc.IsUnsupported)
            {
                // return right away, do not check anything else
                return typeDesc;
            }
            _typeDescs.Add(type, typeDesc);

            if (arrayElementType != null)
            {
                TypeDesc td = GetTypeDesc(arrayElementType, memberInfo, true, false);
                // explicitly disallow read-only elements, even if they are collections
                if (directReference && (td.IsCollection || td.IsEnumerable) && !td.IsPrimitive)
                {
                    td.CheckNeedConstructor();
                }
                typeDesc.ArrayElementTypeDesc = td;
            }
            if (baseType != null && baseType != typeof(object) && baseType != typeof(ValueType))
            {
                typeDesc.BaseTypeDesc = GetTypeDesc(baseType, memberInfo, false, false);
            }
            return typeDesc;
        }
 void WriteCollectionInfo(string typeVariable, TypeDesc typeDesc, Type type){
     string typeFullName = CodeIdentifier.GetCSharpName(type);
     string elementTypeFullName = typeDesc.ArrayElementTypeDesc.CSharpName;
     bool elementUseReflection = typeDesc.ArrayElementTypeDesc.UseReflection;
     if (typeDesc.IsCollection) {
         WriteDefaultIndexerInit(type, typeFullName, typeDesc.UseReflection, elementUseReflection);
     }
     else if (typeDesc.IsEnumerable) {
         if (typeDesc.IsGenericInterface) {
             WriteMethodInfo(typeFullName, typeVariable, "System.Collections.Generic.IEnumerable*", true);
         }
         else if (!typeDesc.IsPrivateImplementation) {
             WriteMethodInfo(typeFullName, typeVariable, "GetEnumerator", true);
         }
     }
     WriteMethodInfo(typeFullName, typeVariable, "Add", false, GetStringForTypeof(elementTypeFullName, elementUseReflection));
 }
示例#31
0
文件: Types.cs 项目: johnhhm/corefx
 internal TypeDesc(string name, string fullName, XmlSchemaType dataType, TypeKind kind, TypeDesc baseTypeDesc, TypeFlags flags, string formatterName)
 {
     _name = name.Replace('+', '.');
     _fullName = fullName.Replace('+', '.');
     _kind = kind;
     _baseTypeDesc = baseTypeDesc;
     _flags = flags;
     _isXsdType = kind == TypeKind.Primitive;
     if (_isXsdType)
         _weight = 1;
     else if (kind == TypeKind.Enum)
         _weight = 2;
     else if (_kind == TypeKind.Root)
         _weight = -1;
     else
         _weight = baseTypeDesc == null ? 0 : baseTypeDesc.Weight + 1;
     _dataType = dataType;
     _formatterName = formatterName;
 }
 internal void WriteArrayLocalDecl( string typeName, string variableName, string initValue, TypeDesc arrayTypeDesc) {
     if (arrayTypeDesc.UseReflection){
         if(arrayTypeDesc.IsEnumerable)
             typeName = typeof(IEnumerable).FullName;
         else if (arrayTypeDesc.IsCollection)
             typeName = typeof(ICollection).FullName;
         else
             typeName = typeof(Array).FullName;
     }
     writer.Write(typeName);
     writer.Write(" ");
     writer.Write(variableName);
     if (initValue != null){
         writer.Write(" = ");
         if (initValue != "null")
             writer.Write("("+typeName+")");
         writer.Write(initValue);
     }
     writer.WriteLine(";");
 }
示例#33
0
文件: Types.cs 项目: johnhhm/corefx
 private static bool CanWriteProperty(PropertyInfo propertyInfo, TypeDesc typeDesc)
 {
     // If the property is a collection, we don't need a setter.
     if (typeDesc.Kind == TypeKind.Collection || typeDesc.Kind == TypeKind.Enumerable)
     {
         return true;
     }
     // Else the property needs a public setter.
     return propertyInfo.SetMethod != null && propertyInfo.SetMethod.IsPublic;
 }