当前位置: 首页>>代码示例>>C#>>正文


C# CodeGenerator.Load方法代码示例

本文整理汇总了C#中CodeGenerator.Load方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.Load方法的具体用法?C# CodeGenerator.Load怎么用?C# CodeGenerator.Load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CodeGenerator的用法示例。


在下文中一共展示了CodeGenerator.Load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateHashtableGetBegin

        internal FieldBuilder GenerateHashtableGetBegin(string privateName, string publicName, TypeBuilder serializerContractTypeBuilder)
        {
            FieldBuilder fieldBuilder = serializerContractTypeBuilder.DefineField(
                privateName,
                typeof(Hashtable),
                FieldAttributes.Private
                );
            ilg = new CodeGenerator(serializerContractTypeBuilder);
            PropertyBuilder propertyBuilder = serializerContractTypeBuilder.DefineProperty(
                publicName,
                PropertyAttributes.None,
                CallingConventions.HasThis,
                typeof(Hashtable),
                null, null, null, null, null);

            ilg.BeginMethod(
                typeof(Hashtable),
                "get_" + publicName,
                Array.Empty<Type>(),
                Array.Empty<string>(),
                CodeGenerator.PublicOverrideMethodAttributes | MethodAttributes.SpecialName);
            propertyBuilder.SetGetMethod(ilg.MethodBuilder);

            ilg.Ldarg(0);
            ilg.LoadMember(fieldBuilder);
            ilg.Load(null);
            // this 'if' ends in GenerateHashtableGetEnd
            ilg.If(Cmp.EqualTo);

            ConstructorInfo Hashtable_ctor = typeof(Hashtable).GetConstructor(
                CodeGenerator.InstanceBindingFlags,
                Array.Empty<Type>()
                );
            LocalBuilder _tmpLoc = ilg.DeclareLocal(typeof(Hashtable), "_tmp");
            ilg.New(Hashtable_ctor);
            ilg.Stloc(_tmpLoc);

            return fieldBuilder;
        }
开发者ID:omariom,项目名称:corefx,代码行数:39,代码来源:XmlSerializationILGen.cs

示例2: GenerateInvokeEndDelegate

            internal InvokeEndDelegate GenerateInvokeEndDelegate(MethodInfo method, out int outputParameterCount)
            {
                bool requiresMemberAccess = MethodRequiresMemberAccess(method);

                this.ilg = new CodeGenerator();
                try
                {
                    this.ilg.BeginMethod("AsyncInvokeEnd" + method.Name, typeof(InvokeEndDelegate), requiresMemberAccess);
                }
                catch (SecurityException securityException)
                {
                    if (requiresMemberAccess && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
                    {
                        DiagnosticUtility.TraceHandledException(securityException, TraceEventType.Warning);
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            new SecurityException(SR.GetString(
                                    SR.PartialTrustServiceMethodNotVisible,
                                    method.DeclaringType.FullName,
                                    method.Name)));
                    }
                    else
                    {
                        throw;
                    }
                }

                ArgBuilder targetArg = ilg.GetArg(0);
                ArgBuilder outputParametersArg = ilg.GetArg(1);
                ArgBuilder resultArg = ilg.GetArg(2);

                ParameterInfo[] parameters = method.GetParameters();
                LocalBuilder returnLocal = ilg.DeclareLocal(ilg.CurrentMethod.ReturnType, "returnParam");
                LocalBuilder[] parameterLocals = new LocalBuilder[parameters.Length - 1];
                DeclareParameterLocals(parameters, parameterLocals);

                LoadZeroValueInputParametersIntoLocals(parameters, parameterLocals);
                LoadTarget(targetArg, method.ReflectedType);
                LoadParameters(parameters, parameterLocals);
                ilg.Load(resultArg);
                InvokeMethod(method, returnLocal);
                LoadOutputParametersIntoArray(parameters, parameterLocals, outputParametersArg, out outputParameterCount);

                ilg.Load(returnLocal);
                return (InvokeEndDelegate)this.ilg.EndMethod();
            }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:45,代码来源:InvokerUtil.cs

示例3: GenerateCreateInstanceDelegate

            internal CreateInstanceDelegate GenerateCreateInstanceDelegate(Type type, ConstructorInfo constructor)
            {
                bool requiresMemberAccess = !IsTypeVisible(type) || ConstructorRequiresMemberAccess(constructor);

                this.ilg = new CodeGenerator();
                try
                {
                    ilg.BeginMethod("Create" + type.FullName, typeof(CreateInstanceDelegate), requiresMemberAccess);
                }
                catch (SecurityException securityException)
                {
                    if (requiresMemberAccess && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
                    {
                        DiagnosticUtility.TraceHandledException(securityException, TraceEventType.Warning);
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            new SecurityException(SR.GetString(
                                    SR.PartialTrustServiceCtorNotVisible,
                                    type.FullName)));
                    }
                    else
                    {
                        throw;
                    }
                }

                if (type.IsValueType)
                {
                    LocalBuilder instanceLocal = ilg.DeclareLocal(type, type.Name + "Instance");
                    ilg.LoadZeroValueIntoLocal(type, instanceLocal);
                    ilg.Load(instanceLocal);
                }
                else
                {
                    ilg.New(constructor);
                }
                ilg.ConvertValue(type, ilg.CurrentMethod.ReturnType);
                return (CreateInstanceDelegate)ilg.EndMethod();
            }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:38,代码来源:InvokerUtil.cs

示例4: WriteHashtable

        string WriteHashtable(EnumMapping mapping, string typeName, out MethodBuilder get_TableName) {
            get_TableName = null;

            CodeIdentifier.CheckValidIdentifier(typeName);
            string propName = MakeUnique(mapping, typeName + "Values");
            if (propName == null) return CodeIdentifier.GetCSharpName(typeName);
            string memberName = MakeUnique(mapping, "_" + propName);
            propName = CodeIdentifier.GetCSharpName(propName);

            FieldBuilder fieldBuilder = this.typeBuilder.DefineField(
                memberName,
                typeof(Hashtable),
                FieldAttributes.Private
                );

            PropertyBuilder propertyBuilder = this.typeBuilder.DefineProperty(
                propName,
                PropertyAttributes.None,
                CallingConventions.HasThis,
                typeof(Hashtable),
                null, null, null, null, null);

            ilg = new CodeGenerator(this.typeBuilder);
            ilg.BeginMethod(
                typeof(Hashtable),
                "get_" + propName,
                CodeGenerator.EmptyTypeArray,
                CodeGenerator.EmptyStringArray,
                MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.SpecialName);

            ilg.Ldarg(0);
            ilg.LoadMember(fieldBuilder);
            ilg.Load(null);
            ilg.If(Cmp.EqualTo);

            ConstructorInfo Hashtable_ctor = typeof(Hashtable).GetConstructor(
                CodeGenerator.InstanceBindingFlags,
                null,
                CodeGenerator.EmptyTypeArray,
                null
                );
            LocalBuilder hLoc = ilg.DeclareLocal(typeof(Hashtable), "h");
            ilg.New(Hashtable_ctor);
            ilg.Stloc(hLoc);

            ConstantMapping[] constants = mapping.Constants;
            MethodInfo Hashtable_Add = typeof(Hashtable).GetMethod(
                "Add",
                CodeGenerator.InstanceBindingFlags,
                null,
                new Type[] { typeof(Object), typeof(Object) },
                null
                );

            for (int i = 0; i < constants.Length; i++) {
                ilg.Ldloc(hLoc);
                ilg.Ldstr(constants[i].XmlName);
                ilg.Ldc(Enum.ToObject(mapping.TypeDesc.Type, constants[i].Value));
                ilg.ConvertValue(mapping.TypeDesc.Type, typeof(long));
                ilg.ConvertValue(typeof(long), typeof(Object));

                ilg.Call(Hashtable_Add);
            }

            ilg.Ldarg(0);
            ilg.Ldloc(hLoc);
            ilg.StoreMember(fieldBuilder);

            ilg.EndIf();

            ilg.Ldarg(0);
            ilg.LoadMember(fieldBuilder);

            get_TableName = ilg.EndMethod();
            propertyBuilder.SetGetMethod(get_TableName);

            return propName;
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:78,代码来源:XmlSerializationReaderILGen.cs

示例5: GenerateClassReader

            public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
            {
                _ilg = new CodeGenerator();
                bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
                try
                {
                    _ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag);
                }
                catch (SecurityException securityException)
                {
                    if (memberAccessFlag)
                    {
                        classContract.RequiresMemberAccessForRead(securityException);
                    }
                    else
                    {
                        throw;
                    }
                }

                InitArgs();
                CreateObject(classContract);
                _ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal);
                InvokeOnDeserializing(classContract);
                LocalBuilder objectId = null;
                ReadClass(classContract);

                InvokeOnDeserialized(classContract);
                if (objectId == null)
                {
                    _ilg.Load(_objectLocal);

                    // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
                    // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
                    // on DateTimeOffset; which does not work in partial trust.

                    if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
                    {
                        _ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
                        _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
                        _ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType);
                    }
                    //Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>. 
                    else if (classContract.IsKeyValuePairAdapter)
                    {
                        _ilg.Call(classContract.GetKeyValuePairMethodInfo);
                        _ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType);
                    }
                    else
                    {
                        _ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType);
                    }
                }
                return (XmlFormatClassReaderDelegate)_ilg.EndMethod();
            }
开发者ID:AndreGleichner,项目名称:corefx,代码行数:55,代码来源:XmlFormatReaderGenerator.cs

示例6: GenerateClassReader

            public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
            {
                ilg = new CodeGenerator();
                bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
                try
                {
                    ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag);
                }
                catch (SecurityException securityException)
                {
                    if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
                    {
                        classContract.RequiresMemberAccessForRead(securityException);
                    }
                    else
                    {
                        throw;
                    }
                }

                InitArgs();
                DemandSerializationFormatterPermission(classContract);
                DemandMemberAccessPermission(memberAccessFlag);
                CreateObject(classContract);
                ilg.Call(contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, objectLocal);
                InvokeOnDeserializing(classContract);
                LocalBuilder objectId = null;
                if (HasFactoryMethod(classContract))
                {
                    objectId = ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead");
                    ilg.Call(contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod);
                    ilg.Stloc(objectId);
                }
                if (classContract.IsISerializable)
                    ReadISerializable(classContract);
                else
                    ReadClass(classContract);
                bool isFactoryType = InvokeFactoryMethod(classContract, objectId);
                if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType))
                    ilg.Call(objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null);
                InvokeOnDeserialized(classContract);
                if (objectId == null || !isFactoryType)
                {
                    ilg.Load(objectLocal);

                    // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
                    // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
                    // on DateTimeOffset; which does not work in partial trust.

                    if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
                    {
                        ilg.ConvertValue(objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
                        ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
                        ilg.ConvertValue(Globals.TypeOfDateTimeOffset, ilg.CurrentMethod.ReturnType);
                    }
                    else
                    {
                        ilg.ConvertValue(objectLocal.LocalType, ilg.CurrentMethod.ReturnType);
                    }
                }
                return (XmlFormatClassReaderDelegate)ilg.EndMethod();
            }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:62,代码来源:XmlFormatReaderGenerator.cs

示例7: ILGenForCreateInstance

        internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bool nonPublic) {
            // Special case DBNull
            if (type == typeof(DBNull)) {
                FieldInfo DBNull_Value = typeof(DBNull).GetField("Value", CodeGenerator.StaticBindingFlags);
                ilg.LoadMember(DBNull_Value);
                return;
            }

            // Special case XElement
            // codegen the same as 'internal XElement : this("default") { }'
            if (type.FullName == "System.Xml.Linq.XElement") {
                Type xName = type.Assembly.GetType("System.Xml.Linq.XName");
                if (xName != null) {
                    MethodInfo XName_op_Implicit = xName.GetMethod(
                        "op_Implicit",
                        CodeGenerator.StaticBindingFlags,
                        null,
                        new Type[] { typeof(String) },
                        null
                        );
                    ConstructorInfo XElement_ctor = type.GetConstructor(
                        CodeGenerator.InstanceBindingFlags,
                        null,
                        new Type[] { xName },
                        null
                        );
                    if (XName_op_Implicit != null && XElement_ctor != null) {
                        ilg.Ldstr("default");
                        ilg.Call(XName_op_Implicit);
                        ilg.New(XElement_ctor);
                        return;
                    }
                }
            }

            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
            if (nonPublic) {
                bindingFlags |= BindingFlags.NonPublic;
            }

            MethodInfo Activator_CreateInstance = typeof(Activator).GetMethod(
                  "CreateInstance",
                  CodeGenerator.StaticBindingFlags,
                  null,
                  new Type[] { typeof(Type), typeof(BindingFlags), typeof(Binder), typeof(Object[]), typeof(CultureInfo) },
                  null
                  );
            ilg.Ldc(type);
            ilg.Load((int)bindingFlags);
            ilg.Load(null);
            ilg.NewArray(typeof(Object), 0);
            ilg.Load(null);
            ilg.Call(Activator_CreateInstance);
            if (cast != null)
                ilg.ConvertValue(Activator_CreateInstance.ReturnType, cast);
        }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:56,代码来源:XmlSerializationWriterILGen.cs

示例8: WriteLiteralStructMethod

        void WriteLiteralStructMethod(StructMapping structMapping) {
            string methodName = (string)MethodNames[structMapping];
            string typeName = structMapping.TypeDesc.CSharpName;
            ilg = new CodeGenerator(this.typeBuilder);
            List<Type> argTypes = new List<Type>();
            List<string> argNames = new List<string>();
            if (structMapping.TypeDesc.IsNullable) {
                argTypes.Add(typeof(Boolean));
                argNames.Add("isNullable");
            }
            argTypes.Add(typeof(Boolean));
            argNames.Add("checkType");
            ilg.BeginMethod(
                structMapping.TypeDesc.Type,
                GetMethodBuilder(methodName),
                argTypes.ToArray(),
                argNames.ToArray(),
                CodeGenerator.PrivateMethodAttributes);

            LocalBuilder locXsiType = ilg.DeclareLocal(typeof(XmlQualifiedName), "xsiType");
            LocalBuilder locIsNull = ilg.DeclareLocal(typeof(Boolean), "isNull");
            MethodInfo XmlSerializationReader_GetXsiType = typeof(XmlSerializationReader).GetMethod(
                "GetXsiType",
                CodeGenerator.InstanceBindingFlags,
                null,
                CodeGenerator.EmptyTypeArray,
                null
                );
            MethodInfo XmlSerializationReader_ReadNull = typeof(XmlSerializationReader).GetMethod(
                 "ReadNull",
                 CodeGenerator.InstanceBindingFlags,
                 null,
                 CodeGenerator.EmptyTypeArray,
                 null
                 );
            Label labelTrue = ilg.DefineLabel();
            Label labelEnd = ilg.DefineLabel();
            ilg.Ldarg("checkType");
            ilg.Brtrue(labelTrue);
            ilg.Load(null);
            ilg.Br_S(labelEnd);
            ilg.MarkLabel(labelTrue);
            ilg.Ldarg(0);
            ilg.Call(XmlSerializationReader_GetXsiType);
            ilg.MarkLabel(labelEnd);
            ilg.Stloc(locXsiType);
            ilg.Ldc(false);
            ilg.Stloc(locIsNull);
            if (structMapping.TypeDesc.IsNullable) {
                ilg.Ldarg("isNullable");
                ilg.If();
                {
                    ilg.Ldarg(0);
                    ilg.Call(XmlSerializationReader_ReadNull);
                    ilg.Stloc(locIsNull);
                }
                ilg.EndIf();
            }

            ilg.Ldarg("checkType");
            ilg.If(); // if (checkType)
            if (structMapping.TypeDesc.IsRoot) {
                ilg.Ldloc(locIsNull);
                ilg.If();
                ilg.Ldloc(locXsiType);
                ilg.Load(null);
                ilg.If(Cmp.NotEqualTo);
                MethodInfo XmlSerializationReader_ReadTypedNull = typeof(XmlSerializationReader).GetMethod(
                       "ReadTypedNull",
                       CodeGenerator.InstanceBindingFlags,
                       null,
                       new Type[] { locXsiType.LocalType },
                       null
                       );
                ilg.Ldarg(0);
                ilg.Ldloc(locXsiType);
                ilg.Call(XmlSerializationReader_ReadTypedNull);
                ilg.Stloc(ilg.ReturnLocal);
                ilg.Br(ilg.ReturnLabel);
                ilg.Else();
                if (structMapping.TypeDesc.IsValueType) {
                    throw CodeGenerator.NotSupported("Arg_NeverValueType");
                }
                else {
                    ilg.Load(null);
                    ilg.Stloc(ilg.ReturnLocal);
                    ilg.Br(ilg.ReturnLabel);
                }
                ilg.EndIf(); // if (xsiType != null)

                ilg.EndIf(); // if (isNull)
            }
            ilg.Ldloc(typeof(XmlQualifiedName), "xsiType");
            ilg.Load(null);
            ilg.Ceq();
            if (!structMapping.TypeDesc.IsRoot) {
                labelTrue = ilg.DefineLabel();
                labelEnd = ilg.DefineLabel();
                // xsiType == null
                ilg.Brtrue(labelTrue);
//.........这里部分代码省略.........
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:101,代码来源:XmlSerializationReaderILGen.cs

示例9: GenerateInitCallbacksMethod

        void GenerateInitCallbacksMethod() {
            ilg = new CodeGenerator(this.typeBuilder);
            ilg.BeginMethod(typeof(void), "InitCallbacks", CodeGenerator.EmptyTypeArray, CodeGenerator.EmptyStringArray,
                CodeGenerator.ProtectedOverrideMethodAttributes);

            string dummyArrayMethodName = NextMethodName("Array");
            bool needDummyArrayMethod = false;
            ilg.EndMethod();

            if (needDummyArrayMethod) {
                ilg.BeginMethod(
                    typeof(object),
                    GetMethodBuilder(dummyArrayMethodName),
                    CodeGenerator.EmptyTypeArray,
                    CodeGenerator.EmptyStringArray,
                    CodeGenerator.PrivateMethodAttributes);
                MethodInfo XmlSerializationReader_UnknownNode1 = typeof(XmlSerializationReader).GetMethod(
                      "UnknownNode",
                      CodeGenerator.InstanceBindingFlags,
                      null,
                      new Type[] { typeof(object) },
                      null
                      );
                ilg.Ldarg(0);
                ilg.Load(null);
                ilg.Call(XmlSerializationReader_UnknownNode1);
                ilg.Load(null);
                ilg.EndMethod();
            }
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:30,代码来源:XmlSerializationReaderILGen.cs

示例10: WriteEnumMethod

        private void WriteEnumMethod(EnumMapping mapping)
        {
            string methodName = (string)MethodNames[mapping];
            List<Type> argTypes = new List<Type>();
            List<string> argNames = new List<string>();
            argTypes.Add(mapping.TypeDesc.Type);
            argNames.Add("v");
            ilg = new CodeGenerator(this.typeBuilder);
            ilg.BeginMethod(
                typeof(string),
                GetMethodBuilder(methodName),
                argTypes.ToArray(),
                argNames.ToArray(),
                CodeGenerator.PrivateMethodAttributes);
            LocalBuilder sLoc = ilg.DeclareLocal(typeof(string), "s");
            ilg.Load(null);
            ilg.Stloc(sLoc);
            ConstantMapping[] constants = mapping.Constants;

            if (constants.Length > 0)
            {
                InternalHashtable values = new InternalHashtable();
                List<Label> caseLabels = new List<Label>();
                List<string> retValues = new List<string>();
                Label defaultLabel = ilg.DefineLabel();
                Label endSwitchLabel = ilg.DefineLabel();
                // This local is necessary; otherwise, it becomes if/else
                LocalBuilder localTmp = ilg.DeclareLocal(mapping.TypeDesc.Type, "localTmp");
                ilg.Ldarg("v");
                ilg.Stloc(localTmp);
                for (int i = 0; i < constants.Length; i++)
                {
                    ConstantMapping c = constants[i];
                    if (values[c.Value] == null)
                    {
                        Label caseLabel = ilg.DefineLabel();
                        ilg.Ldloc(localTmp);
                        ilg.Ldc(Enum.ToObject(mapping.TypeDesc.Type, c.Value));
                        ilg.Beq(caseLabel);
                        caseLabels.Add(caseLabel);
                        retValues.Add(GetCSharpString(c.XmlName));
                        values.Add(c.Value, c.Value);
                    }
                }


                if (mapping.IsFlags)
                {
                    ilg.Br(defaultLabel);
                    for (int i = 0; i < caseLabels.Count; i++)
                    {
                        ilg.MarkLabel(caseLabels[i]);
                        ilg.Ldc(retValues[i]);
                        ilg.Stloc(sLoc);
                        ilg.Br(endSwitchLabel);
                    }
                    ilg.MarkLabel(defaultLabel);
                    RaCodeGen.ILGenForEnumLongValue(ilg, "v");
                    LocalBuilder strArray = ilg.DeclareLocal(typeof(String[]), "strArray");
                    ilg.NewArray(typeof(String), constants.Length);
                    ilg.Stloc(strArray);
                    for (int i = 0; i < constants.Length; i++)
                    {
                        ConstantMapping c = constants[i];
                        ilg.Ldloc(strArray);
                        ilg.Ldc(i);
                        ilg.Ldstr(GetCSharpString(c.XmlName));
                        ilg.Stelem(typeof(String));
                    }
                    ilg.Ldloc(strArray);
                    LocalBuilder longArray = ilg.DeclareLocal(typeof(long[]), "longArray");
                    ilg.NewArray(typeof(long), constants.Length);
                    ilg.Stloc(longArray);

                    for (int i = 0; i < constants.Length; i++)
                    {
                        ConstantMapping c = constants[i];
                        ilg.Ldloc(longArray);
                        ilg.Ldc(i);
                        ilg.Ldc(c.Value);
                        ilg.Stelem(typeof(long));
                    }
                    ilg.Ldloc(longArray);
                    ilg.Ldstr(GetCSharpString(mapping.TypeDesc.FullName));
                    MethodInfo XmlSerializationWriter_FromEnum = typeof(XmlSerializationWriter).GetMethod(
                        "FromEnum",
                        CodeGenerator.StaticBindingFlags,
                        new Type[] { typeof(Int64), typeof(String[]), typeof(Int64[]), typeof(String) }
                        );
                    ilg.Call(XmlSerializationWriter_FromEnum);
                    ilg.Stloc(sLoc);
                    ilg.Br(endSwitchLabel);
                }
                else
                {
                    ilg.Br(defaultLabel);
                    // Case bodies
                    for (int i = 0; i < caseLabels.Count; i++)
                    {
                        ilg.MarkLabel(caseLabels[i]);
//.........这里部分代码省略.........
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:101,代码来源:XmlSerializationWriterILGen.cs

示例11: WriteStructMethod


//.........这里部分代码省略.........

            if (!mapping.TypeDesc.IsAbstract)
            {
                if (mapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(mapping.TypeDesc.Type))
                {
                    MethodInfo XmlSerializationWriter_set_EscapeName = typeof(XmlSerializationWriter).GetMethod(
                        "set_EscapeName",
                        CodeGenerator.InstanceBindingFlags,
                        new Type[] { typeof(Boolean) }
                        );
                    ilg.Ldarg(0);
                    ilg.Ldc(false);
                    ilg.Call(XmlSerializationWriter_set_EscapeName);
                }

                string xmlnsSource = null;
                MemberMapping[] members = TypeScope.GetAllMembers(mapping, memberInfos);
                int xmlnsMember = FindXmlnsIndex(members);
                if (xmlnsMember >= 0)
                {
                    MemberMapping member = members[xmlnsMember];
                    CodeIdentifier.CheckValidIdentifier(member.Name);
                    xmlnsSource = RaCodeGen.GetStringForMember("o", member.Name, mapping.TypeDesc);
                }

                ilg.Ldarg(0);
                ilg.Ldarg("n");
                ilg.Ldarg("ns");
                ArgBuilder argO = ilg.GetArg("o");
                ilg.Ldarg(argO);
                ilg.ConvertValue(argO.ArgType, typeof(Object));
                ilg.Ldc(false);
                if (xmlnsSource == null)
                    ilg.Load(null);
                else
                {
                    System.Diagnostics.Debug.Assert(xmlnsSource.StartsWith("[email protected]", StringComparison.Ordinal));
                    ILGenLoad(xmlnsSource);
                }

                MethodInfo XmlSerializationWriter_WriteStartElement = typeof(XmlSerializationWriter).GetMethod(
                        "WriteStartElement",
                        CodeGenerator.InstanceBindingFlags,
                        new Type[] { typeof(String), typeof(String), typeof(Object), typeof(Boolean), typeof(XmlSerializerNamespaces) }
                        );
                ilg.Call(XmlSerializationWriter_WriteStartElement);
                if (!mapping.TypeDesc.IsRoot)
                {
                    ilg.If(ilg.GetArg("needType"), Cmp.EqualTo, true);
                    {
                        MethodInfo XmlSerializationWriter_WriteXsiType = typeof(XmlSerializationWriter).GetMethod(
                                "WriteXsiType",
                                CodeGenerator.InstanceBindingFlags,
                                new Type[] { typeof(String), typeof(String) }
                                );
                        ilg.Ldarg(0);
                        ilg.Ldstr(GetCSharpString(mapping.TypeName));
                        ilg.Ldstr(GetCSharpString(mapping.Namespace));
                        ilg.Call(XmlSerializationWriter_WriteXsiType);
                    }
                    ilg.EndIf();
                }
                for (int i = 0; i < members.Length; i++)
                {
                    MemberMapping m = members[i];
                    if (m.Attribute != null)
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:67,代码来源:XmlSerializationWriterILGen.cs

示例12: WriteInstanceOf

 internal void WriteInstanceOf(SourceInfo source, Type type, CodeGenerator ilg)
 {
     {
         source.Load(typeof(object));
         ilg.IsInst(type);
         ilg.Load(null);
         ilg.Cne();
         return;
     }
 }
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:10,代码来源:XmlSerializationWriterILGen.cs

示例13: ILGenForCreateInstance

        internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bool nonPublic)
        {
            // Special case DBNull
            if (type == Globals.TypeOfDBNull)
            {
                FieldInfo DBNull_Value = Globals.TypeOfDBNull.GetField("Value", CodeGenerator.StaticBindingFlags);
                ilg.LoadMember(DBNull_Value);
                return;
            }

            // Special case XElement
            // codegen the same as 'internal XElement : this("default") { }'
            if (type.FullName == "System.Xml.Linq.XElement")
            {
                Type xName = type.GetTypeInfo().Assembly.GetType("System.Xml.Linq.XName");
                if (xName != null)
                {
                    MethodInfo XName_op_Implicit = xName.GetMethod(
                        "op_Implicit",
                        CodeGenerator.StaticBindingFlags,
                        new Type[] { typeof(String) }
                        );
                    ConstructorInfo XElement_ctor = type.GetConstructor(
                        CodeGenerator.InstanceBindingFlags,
                        new Type[] { xName }
                        );
                    if (XName_op_Implicit != null && XElement_ctor != null)
                    {
                        ilg.Ldstr("default");
                        ilg.Call(XName_op_Implicit);
                        ilg.New(XElement_ctor);
                        return;
                    }
                }
            }

            Label labelReturn = ilg.DefineLabel();
            Label labelEndIf = ilg.DefineLabel();

            // TypeInfo typeInfo = type.GetTypeInfo();
            // typeInfo not declared explicitly
            ilg.Ldc(type);
            MethodInfo getTypeInfoMehod = typeof(IntrospectionExtensions).GetMethod(
                  "GetTypeInfo",
                  CodeGenerator.StaticBindingFlags,
                  new[] { typeof(Type) }
                  );
            ilg.Call(getTypeInfoMehod);

            // IEnumerator<ConstructorInfo> e = typeInfo.DeclaredConstructors.GetEnumerator();
            LocalBuilder enumerator = ilg.DeclareLocal(typeof(IEnumerator<>).MakeGenericType(typeof(ConstructorInfo)), "e");
            MethodInfo getDeclaredConstructors = typeof(TypeInfo).GetMethod("get_DeclaredConstructors");
            MethodInfo getEnumerator = typeof(IEnumerable<>).MakeGenericType(typeof(ConstructorInfo)).GetMethod("GetEnumerator");
            ilg.Call(getDeclaredConstructors);
            ilg.Call(getEnumerator);
            ilg.Stloc(enumerator);

            ilg.WhileBegin();
            // ConstructorInfo constructorInfo = e.Current();
            MethodInfo enumeratorCurrent = typeof(IEnumerator).GetMethod("get_Current");
            ilg.Ldloc(enumerator);
            ilg.Call(enumeratorCurrent);
            LocalBuilder constructorInfo = ilg.DeclareLocal(typeof(ConstructorInfo), "constructorInfo");
            ilg.Stloc(constructorInfo);

            // if (!constructorInfo.IsStatic && constructorInfo.GetParameters.Length() == 0)
            ilg.Ldloc(constructorInfo);
            MethodInfo constructorIsStatic = typeof(ConstructorInfo).GetMethod("get_IsStatic");
            ilg.Call(constructorIsStatic);
            ilg.Brtrue(labelEndIf);
            ilg.Ldloc(constructorInfo);
            MethodInfo constructorGetParameters = typeof(ConstructorInfo).GetMethod("GetParameters");
            ilg.Call(constructorGetParameters);
            ilg.Ldlen();
            ilg.Ldc(0);
            ilg.Cne();
            ilg.Brtrue(labelEndIf);

            // constructorInfo.Invoke(null);
            MethodInfo constructorInvoke = typeof(ConstructorInfo).GetMethod("Invoke", new Type[] { typeof(object[]) });
            ilg.Ldloc(constructorInfo);
            ilg.Load(null);
            ilg.Call(constructorInvoke);
            ilg.Br(labelReturn);

            ilg.MarkLabel(labelEndIf);
            ilg.WhileBeginCondition(); // while (e.MoveNext())
            MethodInfo IEnumeratorMoveNext = typeof(IEnumerator).GetMethod(
                "MoveNext",
                CodeGenerator.InstanceBindingFlags,
                Array.Empty<Type>());
            ilg.Ldloc(enumerator);
            ilg.Call(IEnumeratorMoveNext);
            ilg.WhileEndCondition();
            ilg.WhileEnd();

            MethodInfo Activator_CreateInstance = typeof(Activator).GetMethod(
                  "CreateInstance",
                  CodeGenerator.StaticBindingFlags,
                  new Type[] { typeof(Type) }
//.........这里部分代码省略.........
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:101,代码来源:XmlSerializationWriterILGen.cs

示例14: GenerateCollectionReader

            public XmlFormatCollectionReaderDelegate GenerateCollectionReader(CollectionDataContract collectionContract)
            {
                if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
                {
                    return new ReflectionXmlCollectionReader().ReflectionReadCollection;
                }
#if NET_NATIVE
                else if (DataContractSerializer.Option == SerializationOption.ReflectionAsBackup)
                {
                    return new ReflectionXmlCollectionReader().ReflectionReadCollection;
                }
#endif
                else
                {
#if NET_NATIVE
                    throw new InvalidOperationException("Cannot generate class reader");
#else
                    _ilg = GenerateCollectionReaderHelper(collectionContract, false /*isGetOnlyCollection*/);
                    ReadCollection(collectionContract);
                    _ilg.Load(_objectLocal);
                    _ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType);
                    return (XmlFormatCollectionReaderDelegate)_ilg.EndMethod();
#endif
                }
            }
开发者ID:rajansingh10,项目名称:corefx,代码行数:25,代码来源:XmlFormatReaderGenerator.cs

示例15: GenerateGetSerializer

        //GenerateGetSerializer(serializers, xmlMappings);
        private void GenerateGetSerializer(Dictionary<string, string> serializers, XmlMapping[] xmlMappings, TypeBuilder serializerContractTypeBuilder)
        {
            ilg = new CodeGenerator(serializerContractTypeBuilder);
            ilg.BeginMethod(
                typeof(XmlSerializer),
                "GetSerializer",
                new Type[] { typeof(Type) },
                new string[] { "type" },
                CodeGenerator.PublicOverrideMethodAttributes);

            for (int i = 0; i < xmlMappings.Length; i++)
            {
                if (xmlMappings[i] is XmlTypeMapping)
                {
                    Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type;
                    if (type == null)
                        continue;
                    if (!type.GetTypeInfo().IsPublic && !type.GetTypeInfo().IsNestedPublic)
                        continue;
                    // DDB172141: Wrong generated CS for serializer of List<string> type
                    if (type.GetTypeInfo().IsGenericType || type.GetTypeInfo().ContainsGenericParameters)
                        continue;
                    ilg.Ldarg("type");
                    ilg.Ldc(type);
                    ilg.If(Cmp.EqualTo);
                    {
                        ConstructorInfo ctor = CreatedTypes[(string)serializers[xmlMappings[i].Key]].GetConstructor(
                            CodeGenerator.InstanceBindingFlags,
                            Array.Empty<Type>()
                            );
                        ilg.New(ctor);
                        ilg.Stloc(ilg.ReturnLocal);
                        ilg.Br(ilg.ReturnLabel);
                    }
                    ilg.EndIf();
                }
            }
            ilg.Load(null);
            ilg.Stloc(ilg.ReturnLocal);
            ilg.Br(ilg.ReturnLabel);
            ilg.MarkLabel(ilg.ReturnLabel);
            ilg.Ldloc(ilg.ReturnLocal);
            ilg.EndMethod();
        }
开发者ID:omariom,项目名称:corefx,代码行数:45,代码来源:XmlSerializationILGen.cs


注:本文中的CodeGenerator.Load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。