GetCSharpName() private static method

private static GetCSharpName ( Type t, Type parameters, int index, StringBuilder sb ) : int
t System.Type
parameters System.Type
index int
sb StringBuilder
return int
 private void GenerateGetSerializer(Hashtable serializers, XmlMapping[] xmlMappings)
 {
     this.writer.Write("public override ");
     this.writer.Write(typeof(XmlSerializer).FullName);
     this.writer.Write(" GetSerializer(");
     this.writer.Write(typeof(Type).FullName);
     this.writer.WriteLine(" type) {");
     this.writer.Indent++;
     for (int i = 0; i < xmlMappings.Length; i++)
     {
         if (xmlMappings[i] is XmlTypeMapping)
         {
             Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type;
             if (((type != null) && (type.IsPublic || type.IsNestedPublic)) && ((!DynamicAssemblies.IsTypeDynamic(type) && !type.IsGenericType) && (!type.ContainsGenericParameters || !DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))))
             {
                 this.writer.Write("if (type == typeof(");
                 this.writer.Write(CodeIdentifier.GetCSharpName(type));
                 this.writer.Write(")) return new ");
                 this.writer.Write((string)serializers[xmlMappings[i].Key]);
                 this.writer.WriteLine("();");
             }
         }
     }
     this.writer.WriteLine("return null;");
     this.writer.Indent--;
     this.writer.WriteLine("}");
 }
 internal string GenerateBaseSerializer(string baseSerializer, string readerClass, string writerClass, CodeIdentifiers classes)
 {
     baseSerializer = CodeIdentifier.MakeValid(baseSerializer);
     baseSerializer = classes.AddUnique(baseSerializer, baseSerializer);
     this.writer.WriteLine();
     this.writer.Write("public abstract class ");
     this.writer.Write(CodeIdentifier.GetCSharpName(baseSerializer));
     this.writer.Write(" : ");
     this.writer.Write(typeof(XmlSerializer).FullName);
     this.writer.WriteLine(" {");
     this.writer.Indent++;
     this.writer.Write("protected override ");
     this.writer.Write(typeof(XmlSerializationReader).FullName);
     this.writer.WriteLine(" CreateReader() {");
     this.writer.Indent++;
     this.writer.Write("return new ");
     this.writer.Write(readerClass);
     this.writer.WriteLine("();");
     this.writer.Indent--;
     this.writer.WriteLine("}");
     this.writer.Write("protected override ");
     this.writer.Write(typeof(XmlSerializationWriter).FullName);
     this.writer.WriteLine(" CreateWriter() {");
     this.writer.Indent++;
     this.writer.Write("return new ");
     this.writer.Write(writerClass);
     this.writer.WriteLine("();");
     this.writer.Indent--;
     this.writer.WriteLine("}");
     this.writer.Indent--;
     this.writer.WriteLine("}");
     return(baseSerializer);
 }
        internal void GenerateSupportedTypes(Type[] types)
        {
            this.writer.Write("public override ");
            this.writer.Write(typeof(bool).FullName);
            this.writer.Write(" CanSerialize(");
            this.writer.Write(typeof(Type).FullName);
            this.writer.WriteLine(" type) {");
            this.writer.Indent++;
            Hashtable hashtable = new Hashtable();

            for (int i = 0; i < types.Length; i++)
            {
                Type type = types[i];
                if (((type != null) && (type.IsPublic || type.IsNestedPublic)) && ((((hashtable[type] == null) && !DynamicAssemblies.IsTypeDynamic(type)) && !type.IsGenericType) && (!type.ContainsGenericParameters || !DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))))
                {
                    hashtable[type] = type;
                    this.writer.Write("if (type == typeof(");
                    this.writer.Write(CodeIdentifier.GetCSharpName(type));
                    this.writer.WriteLine(")) return true;");
                }
            }
            this.writer.WriteLine("return false;");
            this.writer.Indent--;
            this.writer.WriteLine("}");
        }
        internal string GenerateBaseSerializer(string baseSerializer, string readerClass, string writerClass, CodeIdentifiers classes)
        {
            baseSerializer = CodeIdentifier.MakeValid(baseSerializer);
            baseSerializer = classes.AddUnique(baseSerializer, baseSerializer);

            TypeBuilder baseSerializerTypeBuilder = CodeGenerator.CreateTypeBuilder(
                this.moduleBuilder,
                CodeIdentifier.GetCSharpName(baseSerializer),
                TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.BeforeFieldInit,
                typeof(XmlSerializer),
                CodeGenerator.EmptyTypeArray);

            ConstructorInfo readerCtor = CreatedTypes[readerClass].GetConstructor(
                CodeGenerator.InstanceBindingFlags,
                null,
                CodeGenerator.EmptyTypeArray,
                null
                );

            ilg = new CodeGenerator(baseSerializerTypeBuilder);
            ilg.BeginMethod(typeof(XmlSerializationReader),
                            "CreateReader",
                            CodeGenerator.EmptyTypeArray,
                            CodeGenerator.EmptyStringArray,
                            CodeGenerator.ProtectedOverrideMethodAttributes);
            ilg.New(readerCtor);
            ilg.EndMethod();

            ConstructorInfo writerCtor = CreatedTypes[writerClass].GetConstructor(
                CodeGenerator.InstanceBindingFlags,
                null,
                CodeGenerator.EmptyTypeArray,
                null
                );

            ilg.BeginMethod(typeof(XmlSerializationWriter),
                            "CreateWriter",
                            CodeGenerator.EmptyTypeArray,
                            CodeGenerator.EmptyStringArray,
                            CodeGenerator.ProtectedOverrideMethodAttributes);
            ilg.New(writerCtor);
            ilg.EndMethod();

            baseSerializerTypeBuilder.DefineDefaultConstructor(
                MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
            Type baseSerializerType = baseSerializerTypeBuilder.CreateType();

            CreatedTypes.Add(baseSerializerType.Name, baseSerializerType);

            return(baseSerializer);
        }
Beispiel #5
0
        internal void GenerateSupportedTypes(Type[] types)
        {
            _writer.Write("public override ");
            _writer.Write(typeof(bool).FullName);
            _writer.Write(" CanSerialize(");
            _writer.Write(typeof(Type).FullName);
            _writer.WriteLine(" type) {");
            _writer.Indent++;
            Hashtable uniqueTypes = new Hashtable();

            for (int i = 0; i < types.Length; i++)
            {
                Type type = types[i];

                if (type == null)
                {
                    continue;
                }
                if (!type.IsPublic && !type.IsNestedPublic)
                {
                    continue;
                }
                if (uniqueTypes[type] != null)
                {
                    continue;
                }
                if (DynamicAssemblies.IsTypeDynamic(type))
                {
                    continue;
                }
                if (type.IsGenericType || type.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))
                {
                    continue;
                }
                uniqueTypes[type] = type;
                _writer.Write("if (type == typeof(");
                _writer.Write(CodeIdentifier.GetCSharpName(type));
                _writer.WriteLine(")) return true;");
            }
            _writer.WriteLine("return false;");
            _writer.Indent--;
            _writer.WriteLine("}");
        }
Beispiel #6
0
        private string WriteDefaultIndexerInit(Type type, string escapedName, bool collectionUseReflection, bool elementUseReflection)
        {
            string       s = this.GenerateVariableName("item", escapedName);
            PropertyInfo defaultIndexer = TypeScope.GetDefaultIndexer(type, null);

            this.writer.Write("static XSArrayInfo ");
            this.writer.Write(s);
            this.writer.Write("= new XSArrayInfo(");
            this.writer.Write(this.GetStringForTypeof(CodeIdentifier.GetCSharpName(type), collectionUseReflection));
            this.writer.Write(".GetProperty(");
            this.WriteQuotedCSharpString(defaultIndexer.Name);
            this.writer.Write(",");
            this.writer.Write(this.GetStringForTypeof(CodeIdentifier.GetCSharpName(defaultIndexer.PropertyType), elementUseReflection));
            this.writer.Write(",new ");
            this.writer.Write(typeof(Type[]).FullName);
            this.writer.WriteLine("{typeof(int)}));");
            this.reflectionVariables.Add("0:" + escapedName, s);
            return(s);
        }
Beispiel #7
0
        private void WriteCollectionInfo(string typeVariable, TypeDesc typeDesc, Type type)
        {
            string cSharpName    = CodeIdentifier.GetCSharpName(type);
            string typeFullName  = typeDesc.ArrayElementTypeDesc.CSharpName;
            bool   useReflection = typeDesc.ArrayElementTypeDesc.UseReflection;

            if (typeDesc.IsCollection)
            {
                this.WriteDefaultIndexerInit(type, cSharpName, typeDesc.UseReflection, useReflection);
            }
            else if (typeDesc.IsEnumerable)
            {
                if (typeDesc.IsGenericInterface)
                {
                    this.WriteMethodInfo(cSharpName, typeVariable, "System.Collections.Generic.IEnumerable*", true, new string[0]);
                }
                else if (!typeDesc.IsPrivateImplementation)
                {
                    this.WriteMethodInfo(cSharpName, typeVariable, "GetEnumerator", true, new string[0]);
                }
            }
            this.WriteMethodInfo(cSharpName, typeVariable, "Add", false, new string[] { this.GetStringForTypeof(typeFullName, useReflection) });
        }
Beispiel #8
0
        private void InternalLoad(Type elementType, bool asAddress = false)
        {
            Match match = regex.Match(Arg);

            if (match.Success)
            {
                object varA    = ILG.GetVariable(match.Groups["a"].Value);
                Type   varType = ILG.GetVariableType(varA);
                object varIA   = ILG.GetVariable(match.Groups["ia"].Value);
                if (varType.IsArray)
                {
                    ILG.Load(varA);
                    ILG.Load(varIA);
                    Type eType = varType.GetElementType();
                    if (CodeGenerator.IsNullableGenericType(eType))
                    {
                        ILG.Ldelema(eType);
                        ConvertNullableValue(eType, elementType);
                    }
                    else
                    {
                        if (eType.IsValueType)
                        {
                            ILG.Ldelema(eType);
                            if (!asAddress)
                            {
                                ILG.Ldobj(eType);
                            }
                        }
                        else
                        {
                            ILG.Ldelem(eType);
                        }
                        if (elementType != null)
                        {
                            ILG.ConvertValue(eType, elementType);
                        }
                    }
                }
                else
                {
                    ILG.Load(varA);
                    ILG.Load(varIA);
                    MethodInfo get_Item = varType.GetMethod(
                        "get_Item",
                        CodeGenerator.InstanceBindingFlags,
                        null,
                        new Type[] { typeof(Int32) },
                        null
                        );

                    if (get_Item == null && typeof(IList).IsAssignableFrom(varType))
                    {
                        get_Item = iListGetItemMethod.Value;
                    }

                    Debug.Assert(get_Item != null);
                    ILG.Call(get_Item);
                    Type eType = get_Item.ReturnType;
                    if (CodeGenerator.IsNullableGenericType(eType))
                    {
                        LocalBuilder localTmp = ILG.GetTempLocal(eType);
                        ILG.Stloc(localTmp);
                        ILG.Ldloca(localTmp);
                        ConvertNullableValue(eType, elementType);
                    }
                    else if ((elementType != null) && !(eType.IsAssignableFrom(elementType) || elementType.IsAssignableFrom(eType)))
                    {
                        throw new CodeGeneratorConversionException(eType, elementType, asAddress, "IsNotAssignableFrom");
                    }
                    else
                    {
                        Convert(eType, elementType, asAddress);
                    }
                }
            }
            else if (Source == "null")
            {
                ILG.Load(null);
            }
            else
            {
                object var;
                Type   varType;
                if (Arg.StartsWith("o.@", StringComparison.Ordinal) || MemberInfo != null)
                {
                    var     = ILG.GetVariable(Arg.StartsWith("o.@", StringComparison.Ordinal) ? "o" : Arg);
                    varType = ILG.GetVariableType(var);
                    if (varType.IsValueType)
                    {
                        ILG.LoadAddress(var);
                    }
                    else
                    {
                        ILG.Load(var);
                    }
                }
                else
                {
                    var     = ILG.GetVariable(Arg);
                    varType = ILG.GetVariableType(var);

                    if (CodeGenerator.IsNullableGenericType(varType) &&
                        varType.GetGenericArguments()[0] == elementType)
                    {
                        ILG.LoadAddress(var);
                        ConvertNullableValue(varType, elementType);
                    }
                    else
                    {
                        if (asAddress)
                        {
                            ILG.LoadAddress(var);
                        }
                        else
                        {
                            ILG.Load(var);
                        }
                    }
                }

                if (MemberInfo != null)
                {
                    Type memberType = (MemberInfo is FieldInfo) ?
                                      ((FieldInfo)MemberInfo).FieldType : ((PropertyInfo)MemberInfo).PropertyType;
                    if (CodeGenerator.IsNullableGenericType(memberType))
                    {
                        ILG.LoadMemberAddress(MemberInfo);
                        ConvertNullableValue(memberType, elementType);
                    }
                    else
                    {
                        ILG.LoadMember(MemberInfo);
                        Convert(memberType, elementType, asAddress);
                    }
                }
                else
                {
                    match = regex2.Match(Source);
                    if (match.Success)
                    {
                        Debug.Assert(match.Groups["arg"].Value == Arg);
                        Debug.Assert(match.Groups["cast"].Value == CodeIdentifier.GetCSharpName(Type));
                        if (asAddress)
                        {
                            ILG.ConvertAddress(varType, Type);
                        }
                        else
                        {
                            ILG.ConvertValue(varType, Type);
                        }
                        varType = Type;
                    }
                    Convert(varType, elementType, asAddress);
                }
            }
        }
Beispiel #9
0
        internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass)
        {
            string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name));

            serializerName = classes.AddUnique(serializerName + "Serializer", mapping);

            TypeBuilder typedSerializerTypeBuilder = CodeGenerator.CreateTypeBuilder(
                _moduleBuilder,
                CodeIdentifier.GetCSharpName(serializerName),
                TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                CreatedTypes[baseSerializer],
                Array.Empty <Type>()
                );

            ilg = new CodeGenerator(typedSerializerTypeBuilder);
            ilg.BeginMethod(
                typeof(Boolean),
                "CanDeserialize",
                new Type[] { typeof(XmlReader) },
                new string[] { "xmlReader" },
                CodeGenerator.PublicOverrideMethodAttributes
                );

            if (mapping.Accessor.Any)
            {
                ilg.Ldc(true);
                ilg.Stloc(ilg.ReturnLocal);
                ilg.Br(ilg.ReturnLabel);
            }
            else
            {
                MethodInfo XmlReader_IsStartElement = typeof(XmlReader).GetMethod(
                    "IsStartElement",
                    CodeGenerator.InstanceBindingFlags,
                    new Type[] { typeof(String), typeof(String) }
                    );
                ilg.Ldarg(ilg.GetArg("xmlReader"));
                ilg.Ldstr(GetCSharpString(mapping.Accessor.Name));
                ilg.Ldstr(GetCSharpString(mapping.Accessor.Namespace));
                ilg.Call(XmlReader_IsStartElement);
                ilg.Stloc(ilg.ReturnLocal);
                ilg.Br(ilg.ReturnLabel);
            }
            ilg.MarkLabel(ilg.ReturnLabel);
            ilg.Ldloc(ilg.ReturnLocal);
            ilg.EndMethod();

            if (writeMethod != null)
            {
                ilg = new CodeGenerator(typedSerializerTypeBuilder);
                ilg.BeginMethod(
                    typeof(void),
                    "Serialize",
                    new Type[] { typeof(object), typeof(XmlSerializationWriter) },
                    new string[] { "objectToSerialize", "writer" },
                    CodeGenerator.ProtectedOverrideMethodAttributes);
                MethodInfo writerType_writeMethod = CreatedTypes[writerClass].GetMethod(
                    writeMethod,
                    CodeGenerator.InstanceBindingFlags,
                    new Type[] { (mapping is XmlMembersMapping) ? typeof(object[]) : typeof(object) }
                    );
                ilg.Ldarg("writer");
                ilg.Castclass(CreatedTypes[writerClass]);
                ilg.Ldarg("objectToSerialize");
                if (mapping is XmlMembersMapping)
                {
                    ilg.ConvertValue(typeof(object), typeof(object[]));
                }
                ilg.Call(writerType_writeMethod);
                ilg.EndMethod();
            }
            if (readMethod != null)
            {
                ilg = new CodeGenerator(typedSerializerTypeBuilder);
                ilg.BeginMethod(
                    typeof(object),
                    "Deserialize",
                    new Type[] { typeof(XmlSerializationReader) },
                    new string[] { "reader" },
                    CodeGenerator.ProtectedOverrideMethodAttributes);
                MethodInfo readerType_readMethod = CreatedTypes[readerClass].GetMethod(
                    readMethod,
                    CodeGenerator.InstanceBindingFlags,
                    Array.Empty <Type>()
                    );
                ilg.Ldarg("reader");
                ilg.Castclass(CreatedTypes[readerClass]);
                ilg.Call(readerType_readMethod);
                ilg.EndMethod();
            }
            typedSerializerTypeBuilder.DefineDefaultConstructor(CodeGenerator.PublicMethodAttributes);
            Type typedSerializerType = typedSerializerTypeBuilder.CreateTypeInfo().AsType();

            CreatedTypes.Add(typedSerializerType.Name, typedSerializerType);

            return(typedSerializerType.Name);
        }
Beispiel #10
0
        internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass)
        {
            string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name));

            serializerName = classes.AddUnique(serializerName + "Serializer", mapping);

            _writer.WriteLine();
            _writer.Write("public sealed class ");
            _writer.Write(CodeIdentifier.GetCSharpName(serializerName));
            _writer.Write(" : ");
            _writer.Write(baseSerializer);
            _writer.WriteLine(" {");
            _writer.Indent++;

            _writer.WriteLine();
            _writer.Write("public override ");
            _writer.Write(typeof(bool).FullName);
            _writer.Write(" CanDeserialize(");
            _writer.Write(typeof(XmlReader).FullName);
            _writer.WriteLine(" xmlReader) {");
            _writer.Indent++;

            if (mapping.Accessor.Any)
            {
                _writer.WriteLine("return true;");
            }
            else
            {
                _writer.Write("return xmlReader.IsStartElement(");
                WriteQuotedCSharpString(mapping.Accessor.Name);
                _writer.Write(", ");
                WriteQuotedCSharpString(mapping.Accessor.Namespace);
                _writer.WriteLine(");");
            }
            _writer.Indent--;
            _writer.WriteLine("}");

            if (writeMethod != null)
            {
                _writer.WriteLine();
                _writer.Write("protected override void Serialize(object objectToSerialize, ");
                _writer.Write(typeof(System.Xml.Serialization.XmlSerializationWriter).FullName);
                _writer.WriteLine(" writer) {");
                _writer.Indent++;
                _writer.Write("((");
                _writer.Write(writerClass);
                _writer.Write(")writer).");
                _writer.Write(writeMethod);
                _writer.Write("(");
                if (mapping is XmlMembersMapping)
                {
                    _writer.Write("(object[])");
                }
                _writer.WriteLine("objectToSerialize);");
                _writer.Indent--;
                _writer.WriteLine("}");
            }
            if (readMethod != null)
            {
                _writer.WriteLine();
                _writer.Write("protected override object Deserialize(");
                _writer.Write(typeof(System.Xml.Serialization.XmlSerializationReader).FullName);
                _writer.WriteLine(" reader) {");
                _writer.Indent++;
                _writer.Write("return ((");
                _writer.Write(readerClass);
                _writer.Write(")reader).");
                _writer.Write(readMethod);
                _writer.WriteLine("();");
                _writer.Indent--;
                _writer.WriteLine("}");
            }
            _writer.Indent--;
            _writer.WriteLine("}");

            return(serializerName);
        }