CodeGenerator.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / CodeGenerator.cs / 3 / CodeGenerator.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
// ***NOTE*** If this code is changed, make corresponding changes in System.ServiceModel.CodeGenerator also
namespace System.Runtime.Serialization 
{
    using System; 
    using System.Collections; 
    using System.Globalization;
    using System.Xml; 
    using System.Reflection;
    using System.Reflection.Emit;
    using System.IO;
    using System.Security; 
    using System.Security.Permissions;
    using System.Diagnostics; 
 
    internal class CodeGenerator
    { 
        /// 
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain.
        ///  
        [SecurityCritical]
        static MethodInfo getTypeFromHandle; 
        static MethodInfo GetTypeFromHandle 
        {
            ///  
            /// Critical - fetches the critical getTypeFromHandle field
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null.
            /// 
            [SecurityCritical, SecurityTreatAsSafe] 
            get
            { 
                if (getTypeFromHandle == null) 
                    getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");
                return getTypeFromHandle; 
            }
        }
        /// 
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent 
        ///            data from being modified or leaked to other components in appdomain.
        ///  
        [SecurityCritical] 
        static MethodInfo stringFormat;
        static MethodInfo StringFormat 
        {
            /// 
            /// Critical - fetches the critical stringFormat field
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null. 
            /// 
            [SecurityCritical, SecurityTreatAsSafe] 
            get 
            {
                if (stringFormat == null) 
                    stringFormat = typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(object[]) });
                return stringFormat;
            }
        } 
        /// 
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent 
        ///            data from being modified or leaked to other components in appdomain. 
        /// 
        [SecurityCritical] 
        static MethodInfo stringConcat2;
        static MethodInfo StringConcat2
        {
            ///  
            /// Critical - fetches the critical stringConcat2 field
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null. 
            ///  
            [SecurityCritical, SecurityTreatAsSafe]
            get 
            {
                if (stringConcat2 == null)
                    stringConcat2 = typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) });
                return stringConcat2; 
            }
        } 
        ///  
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain. 
        /// 
        [SecurityCritical]
        static MethodInfo stringConcat3;
        static MethodInfo StringConcat3 
        {
            ///  
            /// Critical - fetches the critical stringConcat3 field 
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null.
            ///  
            [SecurityCritical, SecurityTreatAsSafe]
            get
            {
                if (stringConcat3 == null) 
                    stringConcat3 = typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string), typeof(string) });
                return stringConcat3; 
            } 
        }
        ///  
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain.
        /// 
        [SecurityCritical] 
        static MethodInfo objectToString;
        static MethodInfo ObjectToString 
        { 
            /// 
            /// Critical - fetches the critical objectToString field 
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null.
            /// 
            [SecurityCritical, SecurityTreatAsSafe]
            get 
            {
                if (objectToString == null) 
                    objectToString = typeof(object).GetMethod("ToString", new Type[0]); 
                return objectToString;
            } 
        }
        /// 
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain. 
        /// 
        [SecurityCritical] 
        static MethodInfo objectEquals; 
        static MethodInfo ObjectEquals
        { 
            /// 
            /// Critical - fetches the critical objectEquals field
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null.
            ///  
            [SecurityCritical, SecurityTreatAsSafe]
            get 
            { 
                if (objectEquals == null)
                    objectEquals = Globals.TypeOfObject.GetMethod("Equals", BindingFlags.Public | BindingFlags.Static); 
                return objectEquals;
            }
        }
        ///  
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain. 
        ///  
        [SecurityCritical]
        static MethodInfo arraySetValue; 
        static MethodInfo ArraySetValue
        {
            /// 
            /// Critical - fetches the critical arraySetValue field 
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null.
            ///  
            [SecurityCritical, SecurityTreatAsSafe] 
            get
            { 
                if (arraySetValue == null)
                    arraySetValue = typeof(Array).GetMethod("SetValue", new Type[] { typeof(object), typeof(int) });
                return arraySetValue;
            } 
        }
 
        Type delegateType; 

        #if USE_REFEMIT 
        AssemblyBuilder assemblyBuilder;
        ModuleBuilder moduleBuilder;
        TypeBuilder typeBuilder;
        static int typeCounter; 
        MethodBuilder methodBuilder;
        #else 
        ///  
        /// Critical - Static fields are marked SecurityCritical or readonly to prevent
        ///            data from being modified or leaked to other components in appdomain. 
        /// 
        [SecurityCritical]
        static Module serializationModule;
        static Module SerializationModule 
        {
            ///  
            /// Critical - fetches the critical serializationModule field 
            /// Safe - get-only properties only needs to be protected for write; initialized in getter if null.
            ///  
            [SecurityCritical, SecurityTreatAsSafe]
            get
            {
                if (serializationModule == null) 
                {
                    serializationModule = typeof(CodeGenerator).Module;   // could to be replaced by different dll that has SkipVerification set to false 
                } 
                return serializationModule;
            } 
        }
        DynamicMethod dynamicMethod;
        #endif
 
        ILGenerator ilGen;
        ArrayList argList; 
        Stack blockStack; 
        Label methodEndLabel;
 
        LocalBuilder stringFormatArray;

        Hashtable localNames;
        int lineNo = 1; 
        enum CodeGenTrace {None, Save, Tron};
        CodeGenTrace codeGenTrace; 
 
        internal CodeGenerator()
        { 
            SourceSwitch codeGenSwitch = SerializationTrace.CodeGenerationSwitch;
            if ((codeGenSwitch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
                codeGenTrace = CodeGenTrace.Tron;
            else if ((codeGenSwitch.Level & SourceLevels.Information) == SourceLevels.Information) 
                codeGenTrace = CodeGenTrace.Save;
            else 
                codeGenTrace = CodeGenTrace.None; 
        }
 
#if !USE_REFEMIT
        internal void BeginMethod(DynamicMethod dynamicMethod, Type delegateType, string methodName, Type[] argTypes, bool allowPrivateMemberAccess)
        {
            this.dynamicMethod = dynamicMethod; 
            this.ilGen = this.dynamicMethod.GetILGenerator();
            this.delegateType = delegateType; 
 
            InitILGeneration(methodName, argTypes);
        } 
#endif

        internal void BeginMethod(string methodName, Type delegateType, bool allowPrivateMemberAccess)
        { 
            MethodInfo signature = delegateType.GetMethod("Invoke");
            ParameterInfo[] parameters = signature.GetParameters(); 
            Type[] paramTypes = new Type[parameters.Length]; 
            for (int i = 0; i < parameters.Length; i++)
                paramTypes[i] = parameters[i].ParameterType; 
            BeginMethod(signature.ReturnType, methodName, paramTypes, allowPrivateMemberAccess);
            this.delegateType = delegateType;
        }
 
        void BeginMethod(Type returnType, string methodName, Type[] argTypes, bool allowPrivateMemberAccess)
        { 
            #if USE_REFEMIT 
            string typeName = "Type" + (typeCounter++);
            InitAssemblyBuilder(typeName + "." + methodName); 
            this.typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public);
            this.methodBuilder = typeBuilder.DefineMethod(methodName, MethodAttributes.Public|MethodAttributes.Static, returnType, argTypes);
            this.ilGen = this.methodBuilder.GetILGenerator();
            #else 
            this.dynamicMethod = new DynamicMethod(methodName, returnType, argTypes, SerializationModule, allowPrivateMemberAccess);
            this.ilGen = this.dynamicMethod.GetILGenerator(); 
            #endif 

            InitILGeneration(methodName, argTypes); 
        }

        void InitILGeneration(string methodName, Type[] argTypes)
        { 
            this.methodEndLabel = ilGen.DefineLabel();
            this.blockStack = new Stack(); 
            this.argList = new ArrayList(); 
            for (int i=0;i");
                Else();
                if (type.IsArray)
                { 
                    LocalBuilder arrayVar = DeclareLocal(type, "arrayVar");
                    Store(arrayVar); 
                    Load("{ "); 
                    LocalBuilder arrayValueString = DeclareLocal(typeof(string), "arrayValueString");
                    Store(arrayValueString); 
                    LocalBuilder i = DeclareLocal(typeof(int), "i");
                    For(i, 0, arrayVar);
                    Load(arrayValueString);
                    LoadArrayElement(arrayVar, i); 
                    ToString(arrayVar.LocalType.GetElementType());
                    Load(", "); 
                    Concat3(); 
                    Store(arrayValueString);
                    EndFor(); 
                    Load(arrayValueString);
                    Load("}");
                    Concat2();
                } 
                else
                    Call(ObjectToString); 
                EndIf(); 
            }
        } 

        internal void Concat2()
        {
            Call(StringConcat2); 
        }
 
        internal void Concat3() 
        {
            Call(StringConcat3); 
        }

        internal Label[] Switch(int labelCount)
        { 
            SwitchState switchState = new SwitchState(DefineLabel(), DefineLabel());
            Label[] caseLabels = new Label[labelCount]; 
            for (int i=0;i 0; 
            return !hasAptca;
        } 

        void DemandFullTrust()
        {
            fullTrustDemanded = true; 
/*
            if (codeGenTrace != CodeGenTrace.None) 
                EmitSourceComment("DemandFullTrust() {"); 

            Ldc(PermissionState.Unrestricted); 
            New(permissionSetCtor);
            Call(permissionSetDemand);

            if (codeGenTrace != CodeGenTrace.None) 
                EmitSourceComment("}");
*/ 
        } 

        static bool IsProtectedWithSecurity(MethodBase method) 
        {
            return false;
            //return (method.Attributes & MethodAttributes.HasSecurity) != 0;
        } 

#endif 
 
    }
 

    internal class ArgBuilder
    {
        internal int Index; 
        internal Type ArgType;
        internal ArgBuilder(int index, Type argType) 
        { 
            this.Index = index;
            this.ArgType = argType; 
        }
    }

    internal class ForState 
    {
        LocalBuilder indexVar; 
        Label beginLabel; 
        Label testLabel;
        Label endLabel; 
        bool requiresEndLabel;
        object end;

        internal ForState(LocalBuilder indexVar, Label beginLabel, Label testLabel, object end) 
        {
            this.indexVar = indexVar; 
            this.beginLabel = beginLabel; 
            this.testLabel = testLabel;
            this.end = end; 
        }

        internal LocalBuilder Index
        { 
            get
            { 
                return indexVar; 
            }
        } 

        internal Label BeginLabel
        {
            get 
            {
                return beginLabel; 
            } 
        }
 
        internal Label TestLabel
        {
            get
            { 
                return testLabel;
            } 
        } 

        internal Label EndLabel 
        {
            get
            {
                return endLabel; 
            }
            set 
            { 
                endLabel = value;
            } 
        }

        internal bool RequiresEndLabel
        { 
            get
            { 
                return requiresEndLabel; 
            }
            set 
            {
                requiresEndLabel = value;
            }
        } 

        internal object End 
        { 
            get
            { 
                return end;
            }
        }
    } 

    internal enum Cmp 
    { 
        LessThan,
        EqualTo, 
        LessThanOrEqualTo,
        GreaterThan,
        NotEqualTo,
        GreaterThanOrEqualTo 
    }
 
    internal class IfState 
    {
        Label elseBegin; 
        Label endIf;

        internal Label EndIf
        { 
            get
            { 
                return this.endIf; 
            }
            set 
            {
                this.endIf= value;
            }
        } 

        internal Label ElseBegin 
        { 
            get
            { 
                return this.elseBegin;
            }
            set
            { 
                this.elseBegin= value;
            } 
        } 

    } 

#if NotUsed
    internal class BitFlagsGenerator
    { 
        LocalBuilder[] locals;
        CodeGenerator ilg; 
        int bitCount; 
        internal BitFlagsGenerator(int bitCount, CodeGenerator ilg, string localName)
        { 
            this.ilg = ilg;
            this.bitCount = bitCount;
            int localCount = (bitCount+7)/8;
            locals = new LocalBuilder[localCount]; 
            for (int i=0;i> 3; 
        }

        static byte GetBitValue(int bitIndex)
        { 
            return (byte)(1 << (bitIndex & 7));
        } 
    } 
#endif
 
    internal class SwitchState
    {
        Label defaultLabel;
        Label endOfSwitchLabel; 
        bool defaultDefined;
        internal SwitchState(Label defaultLabel, Label endOfSwitchLabel) 
        { 
            this.defaultLabel = defaultLabel;
            this.endOfSwitchLabel = endOfSwitchLabel; 
            this.defaultDefined = false;
        }
        internal Label DefaultLabel
        { 
            get
            { 
                return defaultLabel; 
            }
        } 

        internal Label EndOfSwitchLabel
        {
            get 
            {
                return endOfSwitchLabel; 
            } 
        }
        internal bool DefaultDefined 
        {
            get
            {
                return defaultDefined; 
            }
            set 
            { 
                defaultDefined = value;
            } 
        }
    }

} 

 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK