BeginMethod() private method

private BeginMethod ( DynamicMethod dynamicMethod, Type delegateType, string methodName, Type argTypes, bool allowPrivateMemberAccess ) : void
dynamicMethod System.Reflection.Emit.DynamicMethod
delegateType System.Type
methodName string
argTypes System.Type
allowPrivateMemberAccess bool
return void
コード例 #1
0
            private CodeGenerator GenerateCollectionReaderHelper(CollectionDataContract collectionContract, bool isGetOnlyCollection)
            {
                _ilg = new CodeGenerator();
                bool memberAccessFlag = collectionContract.RequiresMemberAccessForRead(null);

                try
                {
                    if (isGetOnlyCollection)
                    {
                        _ilg.BeginMethod("Read" + collectionContract.StableName.Name + "FromXml" + "IsGetOnly", Globals.TypeOfXmlFormatGetOnlyCollectionReaderDelegate, memberAccessFlag);
                    }
                    else
                    {
                        _ilg.BeginMethod("Read" + collectionContract.StableName.Name + "FromXml" + string.Empty, Globals.TypeOfXmlFormatCollectionReaderDelegate, memberAccessFlag);
                    }
                }
                catch (SecurityException securityException)
                {
                    if (memberAccessFlag)
                    {
                        collectionContract.RequiresMemberAccessForRead(securityException);
                    }
                    else
                    {
                        throw;
                    }
                }
                InitArgs();
                _collectionContractArg = _ilg.GetArg(4);
                return(_ilg);
            }
コード例 #2
0
        internal System.Runtime.Serialization.CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
        {
            Type          underlyingType           = base.UnderlyingType;
            CodeGenerator generator                = new CodeGenerator();
            bool          allowPrivateMemberAccess = this.RequiresMemberAccessForCreate(null);

            try
            {
                generator.BeginMethod("Create" + DataContract.GetClrTypeFullName(underlyingType), typeof(System.Runtime.Serialization.CreateXmlSerializableDelegate), allowPrivateMemberAccess);
            }
            catch (SecurityException exception)
            {
                if (!allowPrivateMemberAccess || !exception.PermissionType.Equals(typeof(ReflectionPermission)))
                {
                    throw;
                }
                this.RequiresMemberAccessForCreate(exception);
            }
            if (underlyingType.IsValueType)
            {
                LocalBuilder localBuilder = generator.DeclareLocal(underlyingType, underlyingType.Name + "Value");
                generator.Ldloca(localBuilder);
                generator.InitObj(underlyingType);
                generator.Ldloc(localBuilder);
            }
            else
            {
                generator.New(this.GetConstructor());
            }
            generator.ConvertValue(base.UnderlyingType, Globals.TypeOfIXmlSerializable);
            generator.Ret();
            return((System.Runtime.Serialization.CreateXmlSerializableDelegate)generator.EndMethod());
        }
コード例 #3
0
 internal XmlFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
 {
     if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
     {
         return CreateReflectionXmlFormatClassWriterDelegate();
     }
     else
     {
         _ilg = new CodeGenerator();
         bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null);
         try
         {
             _ilg.BeginMethod("Write" + classContract.StableName.Name + "ToXml", Globals.TypeOfXmlFormatClassWriterDelegate, memberAccessFlag);
         }
         catch (SecurityException securityException)
         {
             if (memberAccessFlag)
             {
                 classContract.RequiresMemberAccessForWrite(securityException);
             }
             else
             {
                 throw;
             }
         }
         InitArgs(classContract.UnderlyingType);
         WriteClass(classContract);
         return (XmlFormatClassWriterDelegate)_ilg.EndMethod();
     }
 }
コード例 #4
0
            internal XmlFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract)
            {
                ilg = new CodeGenerator();
                bool memberAccessFlag = collectionContract.RequiresMemberAccessForWrite(null);

                try
                {
                    ilg.BeginMethod("Write" + collectionContract.StableName.Name + "ToXml", Globals.TypeOfXmlFormatCollectionWriterDelegate, memberAccessFlag);
                }
                catch (SecurityException securityException)
                {
                    if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
                    {
                        collectionContract.RequiresMemberAccessForWrite(securityException);
                    }
                    else
                    {
                        throw;
                    }
                }
                InitArgs(collectionContract.UnderlyingType);
                DemandMemberAccessPermission(memberAccessFlag);
                if (collectionContract.IsReadOnlyContract)
                {
                    ThrowIfCannotSerializeReadOnlyTypes(collectionContract);
                }
                WriteCollection(collectionContract);
                return((XmlFormatCollectionWriterDelegate)ilg.EndMethod());
            }
コード例 #5
0
            internal XmlFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract)
            {
                if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
                {
                    return(CreateReflectionXmlFormatCollectionWriterDelegate());
                }
                else
                {
#if USE_REFEMIT
                    throw new InvalidOperationException("Cannot generate class writer");
#else
                    _ilg = new CodeGenerator();
                    bool memberAccessFlag = collectionContract.RequiresMemberAccessForWrite(null);
                    try
                    {
                        _ilg.BeginMethod("Write" + collectionContract.StableName.Name + "ToXml", Globals.TypeOfXmlFormatCollectionWriterDelegate, memberAccessFlag);
                    }
                    catch (SecurityException securityException)
                    {
                        if (memberAccessFlag)
                        {
                            collectionContract.RequiresMemberAccessForWrite(securityException);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    InitArgs(collectionContract.UnderlyingType);
                    WriteCollection(collectionContract);
                    return((XmlFormatCollectionWriterDelegate)_ilg.EndMethod());
#endif
                }
            }
コード例 #6
0
 internal XmlFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract)
 {
     if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
     {
         return(CreateReflectionXmlFormatCollectionWriterDelegate());
     }
     else
     {
         _ilg = new CodeGenerator();
         bool memberAccessFlag = collectionContract.RequiresMemberAccessForWrite(null);
         try
         {
             _ilg.BeginMethod("Write" + collectionContract.XmlName.Name + "ToXml", Globals.TypeOfXmlFormatCollectionWriterDelegate, memberAccessFlag);
         }
         catch (SecurityException securityException)
         {
             if (memberAccessFlag)
             {
                 collectionContract.RequiresMemberAccessForWrite(securityException);
             }
             else
             {
                 throw;
             }
         }
         InitArgs(collectionContract.UnderlyingType);
         if (collectionContract.IsReadOnlyContract)
         {
             ThrowIfCannotSerializeReadOnlyTypes(collectionContract);
         }
         WriteCollection(collectionContract);
         return((XmlFormatCollectionWriterDelegate)_ilg.EndMethod());
     }
 }
コード例 #7
0
            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());
            }
コード例 #8
0
            internal XmlFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract)
            {
                _ilg = new CodeGenerator();
                bool memberAccessFlag = collectionContract.RequiresMemberAccessForWrite(null);

                try
                {
                    _ilg.BeginMethod("Write" + collectionContract.StableName.Name + "ToXml", Globals.TypeOfXmlFormatCollectionWriterDelegate, memberAccessFlag);
                }
                catch (SecurityException securityException)
                {
                    if (memberAccessFlag)
                    {
                        collectionContract.RequiresMemberAccessForWrite(securityException);
                    }
                    else
                    {
                        throw;
                    }
                }
                InitArgs(collectionContract.UnderlyingType);
                WriteCollection(collectionContract);
                return((XmlFormatCollectionWriterDelegate)_ilg.EndMethod());
            }
コード例 #9
0
        internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
        {
            Type          type             = this.UnderlyingType;
            CodeGenerator ilg              = new CodeGenerator();
            bool          memberAccessFlag = RequiresMemberAccessForCreate(null);

            try
            {
                ilg.BeginMethod("Create" + DataContract.GetClrTypeFullName(type), typeof(CreateXmlSerializableDelegate), memberAccessFlag);
            }
            catch (SecurityException securityException)
            {
                if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
                {
                    RequiresMemberAccessForCreate(securityException);
                }
                else
                {
                    throw;
                }
            }
            if (type.IsValueType)
            {
                System.Reflection.Emit.LocalBuilder local = ilg.DeclareLocal(type, type.Name + "Value");
                ilg.Ldloca(local);
                ilg.InitObj(type);
                ilg.Ldloc(local);
            }
            else
            {
                ilg.New(GetConstructor());
            }
            ilg.ConvertValue(this.UnderlyingType, Globals.TypeOfIXmlSerializable);
            ilg.Ret();
            return((CreateXmlSerializableDelegate)ilg.EndMethod());
        }
コード例 #10
0
        internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
        {
            Type          type             = this.UnderlyingType;
            CodeGenerator ilg              = new CodeGenerator();
            bool          memberAccessFlag = RequiresMemberAccessForCreate(null) && !(type.FullName == "System.Xml.Linq.XElement");

            try
            {
                ilg.BeginMethod("Create" + DataContract.GetClrTypeFullName(type), typeof(CreateXmlSerializableDelegate), memberAccessFlag);
            }
            catch (SecurityException securityException)
            {
                if (memberAccessFlag)
                {
                    RequiresMemberAccessForCreate(securityException);
                }
                else
                {
                    throw;
                }
            }
            if (type.IsValueType)
            {
                System.Reflection.Emit.LocalBuilder local = ilg.DeclareLocal(type, type.Name + "Value");
                ilg.Ldloca(local);
                ilg.InitObj(type);
                ilg.Ldloc(local);
            }
            else
            {
                // Special case XElement
                // codegen the same as 'internal XElement : this("default") { }'
                ConstructorInfo ctor = GetConstructor();
                if (!ctor.IsPublic && 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",
                            BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
                            new Type[] { typeof(string) }
                            );
                        ConstructorInfo XElement_ctor = type.GetConstructor(
                            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                            new Type[] { xName }
                            );
                        if (XName_op_Implicit != null && XElement_ctor != null)
                        {
                            ilg.Ldstr("default");
                            ilg.Call(XName_op_Implicit);
                            ctor = XElement_ctor;
                        }
                    }
                }
                ilg.New(ctor);
            }
            ilg.ConvertValue(this.UnderlyingType, Globals.TypeOfIXmlSerializable);
            ilg.Ret();
            return((CreateXmlSerializableDelegate)ilg.EndMethod());
        }
コード例 #11
0
            public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
            {
                if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
                {
                    return(new ReflectionXmlClassReader(classContract).ReflectionReadClass);
                }
#if uapaot
                else if (DataContractSerializer.Option == SerializationOption.ReflectionAsBackup)
                {
                    return(new ReflectionXmlClassReader(classContract).ReflectionReadClass);
                }
#endif
                else
                {
#if uapaot
                    throw new InvalidOperationException("Cannot generate class reader");
#else
                    _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;
                    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)
                    {
                        _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());
#endif
                }
            }
コード例 #12
0
            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());
            }