string GenerateHelperClass(string className)
        {
            string indent = "    ";

            string srcFile = className + "." + provider.FileExtension;

            using (StreamWriter sw = new StreamWriter(srcFile, false))
            {
                CodeCompileUnit ccu = new CodeCompileUnit();
                CodeNamespace   ns  = EmitNamespaceAndUsings();
                ccu.Namespaces.Add(ns);
                CodeTypeDeclaration ctd = null;
                if (className.Equals(defaultValuesClassName))
                {
                    ctd = CodeGenHelperMethods.EmitDefaultValuesClass(propertiesGenerated, className);
                }
                else if (className.Equals(constantsClassName))
                {
                    ctd = CodeGenHelperMethods.EmitConstantStringsClass(propertiesGenerated, className);
                }
                else
                {
                    Console.WriteLine("Don't know how to generate this helper class, ignoring: " + className);
                }
                ns.Types.Add(ctd);

                using (IndentedTextWriter itw = new IndentedTextWriter(sw, indent))
                {
                    provider.GenerateCodeFromCompileUnit(ccu, itw, new CodeGeneratorOptions());
                }
            }
            return(srcFile);
        }
コード例 #2
0
        internal static void EmitReflectedProperties(ref CodeTypeDeclaration customConfigCodeClass, Type typeToReflect, out PropertyNameType[] propertiesGenerated, bool excludeBaseClassProperties, string constantsClassName, string defaultValuesClassName)
        {
            // by default, include base classes
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

            // if asked to exclude, modify BindingFlags
            if (excludeBaseClassProperties)
            {
                bindingFlags = bindingFlags | BindingFlags.DeclaredOnly;
            }

            PropertyInfo[]     allProperties = typeToReflect.GetProperties(bindingFlags);
            PropertyNameType[] allPropNames  = new PropertyNameType[allProperties.Length];
            int index = 0;

            foreach (PropertyInfo propertyInfo in allProperties)
            {
                // Only expose properties in config that can be written
                if (propertyInfo.CanWrite)
                {
                    // Including types present in base classes as well
                    PropertyNameType nameType;
                    nameType.propertyName = propertyInfo.Name;
                    nameType.propertyType = propertyInfo.PropertyType;

                    customConfigCodeClass.Members.Add(CodeGenHelperMethods.CreateProperty(nameType, constantsClassName, defaultValuesClassName));
                    allPropNames.SetValue(nameType, index++);
                }
            }
            propertiesGenerated = new PropertyNameType[index];
            Array.Copy(allPropNames, propertiesGenerated, index);
        }
        internal CodeCompileUnit BuildCodeGraph(out ArrayList otherGeneratedFiles)
        {
            compileUnit         = new CodeCompileUnit();
            otherGeneratedFiles = new ArrayList();

            CodeNamespace nameSpace = EmitNamespaceAndUsings();

            compileUnit.Namespaces.Add(nameSpace);
            CodeTypeDeclaration customBESectionClass = EmitClass();

            nameSpace.Types.Add(customBESectionClass);

            EmitConstructors(ref customBESectionClass);
            EmitBindingElementTypeProperty(ref customBESectionClass);
            EmitCreateBindingElementMethod(ref customBESectionClass);

            CodeGenHelperMethods.EmitReflectedProperties(ref customBESectionClass, bindingElementType, out propertiesGenerated, constantsClassName, defaultValuesClassName);

            EmitApplyConfigurationMethod(ref customBESectionClass);
            EmitInitializeFromMethod(ref customBESectionClass);
            EmitCopyFromMethod(ref customBESectionClass);
            CodeGenHelperMethods.EmitPropertiesProperty(ref customBESectionClass, propertiesGenerated, constantsClassName, defaultValuesClassName);

            // also emit the constants used in these properties that we just emitted
            otherGeneratedFiles.Add(GenerateHelperClass(constantsClassName));

            // also emit a class that can hold default values of all the properties that the user would fill in
            otherGeneratedFiles.Add(GenerateHelperClass(defaultValuesClassName));
            return(compileUnit);
        }