////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////

        // BuildProvider implementation

        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            Hashtable       properties  = ProfileBase.GetPropertiesForCompilation();
            CodeCompileUnit compileUnit = new CodeCompileUnit();
            Hashtable       groups      = new Hashtable();
            Type            baseType    = Type.GetType(ProfileBase.InheritsFromTypeString, false);

            // namespace ASP {
            //
            CodeNamespace ns = new CodeNamespace();

            // ns.Name = BaseCodeDomTreeGenerator.defaultNamespace;

            //GEN: using System;
            ns.Imports.Add(new CodeNamespaceImport("System"));
            //GEN: using System.Web;
            ns.Imports.Add(new CodeNamespaceImport("System.Web"));
            //GEN: using System.Web.Profile;
            ns.Imports.Add(new CodeNamespaceImport("System.Web.Profile"));

            // class Profile :  System.Web.Security.ProfileBase {
            //
            CodeTypeDeclaration type = new CodeTypeDeclaration();

            type.Name = ProfileTypeName;
            if (baseType != null)
            {
                type.BaseTypes.Add(new CodeTypeReference(baseType));
                assemblyBuilder.AddAssemblyReference(baseType.Assembly, compileUnit);
            }
            else
            {
                type.BaseTypes.Add(new CodeTypeReference(ProfileBase.InheritsFromTypeString));
                ProfileSection config = MTConfigUtil.GetProfileAppConfig();
                if (config != null)
                {
                    PropertyInformation prop = config.ElementInformation.Properties["inherits"];
                    if (prop != null && prop.Source != null && prop.LineNumber > 0)
                    {
                        type.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(prop.Source), prop.LineNumber);
                    }
                }
            }
            // tell the assemblyBuilder to generate a fast factory for this type
            assemblyBuilder.GenerateTypeFactory(/*ns.Name + "." + */ ProfileTypeName);

            foreach (DictionaryEntry de in properties)
            {
                ProfileNameTypeStruct property = (ProfileNameTypeStruct)de.Value;
                if (property.PropertyType != null)
                {
                    assemblyBuilder.AddAssemblyReference(property.PropertyType.Assembly, compileUnit);
                }
                int pos = property.Name.IndexOf('.');
                if (pos < 0)
                {
                    // public string Color { get { return (string) GetProperty("Color"); } set { SetProperty("Color", value); } }
                    CreateCodeForProperty(assemblyBuilder, type, property);
                }
                else
                {
                    string grpName = property.Name.Substring(0, pos);
                    if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(grpName))
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Profile_bad_group, grpName), property.FileName, property.LineNumber);
                    }
                    if (groups[grpName] == null)
                    {
                        groups.Add(grpName, property.Name);
                    }
                    else
                    {
                        groups[grpName] = ((string)groups[grpName]) + ";" + property.Name;
                    }
                }
            }

            foreach (DictionaryEntry de in groups)
            {
                // public ProfileGroupFooClass Foo { get { return ProfileGroupSomething; }}
                //
                // public class ProfileGroupFoo : ProfileGroup {
                //      Properties
                // }
                AddPropertyGroup(assemblyBuilder, (string)de.Key, (string)de.Value, properties, type, ns);
            }


            // public ASP.Profile GetProfileForUser(string username) {
            //      return (ASP.Profile) this.GetUserProfile(username);
            // }
            AddCodeForGetProfileForUser(type);

            // }
            //
            ns.Types.Add(type);
            compileUnit.Namespaces.Add(ns);

            assemblyBuilder.AddCodeCompileUnit(this, compileUnit);
        }
        private void CreateCodeForProperty(AssemblyBuilder assemblyBuilder, CodeTypeDeclaration type, ProfileNameTypeStruct property)
        {
            string name  = property.Name;
            int    index = name.IndexOf('.');

            if (index > 0)
            {
                name = name.Substring(index + 1);
            }
            if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(name))
            {
                throw new ConfigurationErrorsException(System.Web.SR.GetString("Profile_bad_name"), property.FileName, property.LineNumber);
            }
            CodeMemberProperty property2 = new CodeMemberProperty {
                Name       = name,
                Attributes = MemberAttributes.Public,
                HasGet     = true,
                Type       = property.PropertyCodeRefType
            };
            CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression {
                Method = { TargetObject = new CodeThisReferenceExpression(), MethodName = "GetPropertyValue" }
            };

            expression.Parameters.Add(new CodePrimitiveExpression(name));
            CodeMethodReturnStatement statement = new CodeMethodReturnStatement(new CodeCastExpression(property2.Type, expression));

            property2.GetStatements.Add(statement);
            if (!property.IsReadOnly)
            {
                CodeMethodInvokeExpression expression2 = new CodeMethodInvokeExpression {
                    Method = { TargetObject = new CodeThisReferenceExpression(), MethodName = "SetPropertyValue" }
                };
                expression2.Parameters.Add(new CodePrimitiveExpression(name));
                expression2.Parameters.Add(new CodePropertySetValueReferenceExpression());
                property2.HasSet = true;
                property2.SetStatements.Add(expression2);
            }
            type.Members.Add(property2);
        }
        // End of BuildProvider implementation

        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////
        private void CreateCodeForProperty(AssemblyBuilder assemblyBuilder, CodeTypeDeclaration type, ProfileNameTypeStruct property)
        {
            string name = property.Name;
            int    pos  = name.IndexOf('.');

            if (pos > 0)
            {
                name = name.Substring(pos + 1);
            }
            if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(name))
            {
                throw new ConfigurationErrorsException(SR.GetString(SR.Profile_bad_name), property.FileName, property.LineNumber);
            }
            // e.g.: public string Color {
            //                       get { return (string) GetProperty("Color"); }
            //                       set { SetProperty("Color", value); } }


            // public  property.Type property.name {
            CodeMemberProperty prop = new CodeMemberProperty();

            prop.Name       = name;
            prop.Attributes = MemberAttributes.Public;
            prop.HasGet     = true;
            prop.Type       = property.PropertyCodeRefType;

            ////////////////////////////////////////////////////////////
            // Get statements
            // get { return (property.type) GetProperty(property.name); }
            CodeMethodInvokeExpression cmie;
            CodeMethodReturnStatement  getLine;

            cmie = new CodeMethodInvokeExpression();
            cmie.Method.TargetObject = new CodeThisReferenceExpression();
            cmie.Method.MethodName   = "GetPropertyValue";
            cmie.Parameters.Add(new CodePrimitiveExpression(name));
            getLine = new CodeMethodReturnStatement(new CodeCastExpression(prop.Type, cmie));

            prop.GetStatements.Add(getLine);

            if (!property.IsReadOnly)
            {
                ////////////////////////////////////////////////////////////
                // Set statements
                // set { SetProperty(property.name, value); }
                CodeMethodInvokeExpression setLine;

                setLine = new CodeMethodInvokeExpression();
                setLine.Method.TargetObject = new CodeThisReferenceExpression();
                setLine.Method.MethodName   = "SetPropertyValue";
                setLine.Parameters.Add(new CodePrimitiveExpression(name));
                setLine.Parameters.Add(new CodePropertySetValueReferenceExpression());
                prop.HasSet = true;
                prop.SetStatements.Add(setLine);
            }
            //prop.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(property.FileName), property.LineNumber);
            type.Members.Add(prop);
        }
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            Hashtable       propertiesForCompilation = ProfileBase.GetPropertiesForCompilation();
            CodeCompileUnit ccu        = new CodeCompileUnit();
            Hashtable       hashtable2 = new Hashtable();
            Type            type       = Type.GetType(ProfileBase.InheritsFromTypeString, false);
            CodeNamespace   ns         = new CodeNamespace();

            ns.Imports.Add(new CodeNamespaceImport("System"));
            ns.Imports.Add(new CodeNamespaceImport("System.Web"));
            ns.Imports.Add(new CodeNamespaceImport("System.Web.Profile"));
            CodeTypeDeclaration declaration = new CodeTypeDeclaration {
                Name = "ProfileCommon"
            };

            if (type != null)
            {
                declaration.BaseTypes.Add(new CodeTypeReference(type));
                assemblyBuilder.AddAssemblyReference(type.Assembly, ccu);
            }
            else
            {
                declaration.BaseTypes.Add(new CodeTypeReference(ProfileBase.InheritsFromTypeString));
                ProfileSection profileAppConfig = MTConfigUtil.GetProfileAppConfig();
                if (profileAppConfig != null)
                {
                    PropertyInformation information = profileAppConfig.ElementInformation.Properties["inherits"];
                    if (((information != null) && (information.Source != null)) && (information.LineNumber > 0))
                    {
                        declaration.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(information.Source), information.LineNumber);
                    }
                }
            }
            assemblyBuilder.GenerateTypeFactory("ProfileCommon");
            foreach (DictionaryEntry entry in propertiesForCompilation)
            {
                ProfileNameTypeStruct property = (ProfileNameTypeStruct)entry.Value;
                if (property.PropertyType != null)
                {
                    assemblyBuilder.AddAssemblyReference(property.PropertyType.Assembly, ccu);
                }
                int index = property.Name.IndexOf('.');
                if (index < 0)
                {
                    this.CreateCodeForProperty(assemblyBuilder, declaration, property);
                }
                else
                {
                    string str = property.Name.Substring(0, index);
                    if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(str))
                    {
                        throw new ConfigurationErrorsException(System.Web.SR.GetString("Profile_bad_group", new object[] { str }), property.FileName, property.LineNumber);
                    }
                    if (hashtable2[str] == null)
                    {
                        hashtable2.Add(str, property.Name);
                    }
                    else
                    {
                        hashtable2[str] = ((string)hashtable2[str]) + ";" + property.Name;
                    }
                }
            }
            foreach (DictionaryEntry entry2 in hashtable2)
            {
                this.AddPropertyGroup(assemblyBuilder, (string)entry2.Key, (string)entry2.Value, propertiesForCompilation, declaration, ns);
            }
            this.AddCodeForGetProfileForUser(declaration);
            ns.Types.Add(declaration);
            ccu.Namespaces.Add(ns);
            assemblyBuilder.AddCodeCompileUnit(this, ccu);
        }