Exemplo n.º 1
0
        internal static void Compile(EnumMetadataEnum enumMetadata)
        {
            Type        type;
            EnumBuilder builder2;
            string      enumFullName = GetEnumFullName(enumMetadata);

            if (enumMetadata.UnderlyingType != null)
            {
                type = (Type)LanguagePrimitives.ConvertTo(enumMetadata.UnderlyingType, typeof(Type), CultureInfo.InvariantCulture);
            }
            else
            {
                type = typeof(int);
            }
            ModuleBuilder builder = _moduleBuilder.Value;

            lock (_moduleBuilderUsageLock)
            {
                builder2 = builder.DefineEnum(enumFullName, TypeAttributes.Public, type);
            }
            if (enumMetadata.BitwiseFlagsSpecified && enumMetadata.BitwiseFlags)
            {
                CustomAttributeBuilder customBuilder = new CustomAttributeBuilder(typeof(FlagsAttribute).GetConstructor(Type.EmptyTypes), new object[0]);
                builder2.SetCustomAttribute(customBuilder);
            }
            foreach (EnumMetadataEnumValue value2 in enumMetadata.Value)
            {
                string name         = value2.Name;
                object literalValue = LanguagePrimitives.ConvertTo(value2.Value, type, CultureInfo.InvariantCulture);
                builder2.DefineLiteral(name, literalValue);
            }
            builder2.CreateType();
        }
Exemplo n.º 2
0
        internal static string GetCSharpCode(EnumMetadataEnum enumMetadata)
        {
            var codeCompileUnit = CreateCodeCompileUnit(enumMetadata);

            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            CodeDomProvider.CreateProvider("C#").GenerateCodeFromCompileUnit(
                codeCompileUnit,
                stringWriter,
                new CodeGeneratorOptions());
            return(stringWriter.ToString());
        }
Exemplo n.º 3
0
        internal static void Compile(EnumMetadataEnum enumMetadata)
        {
            string fullEnumName = GetEnumFullName(enumMetadata);

            Type underlyingType;

            if (enumMetadata.UnderlyingType != null)
            {
                underlyingType = (Type)LanguagePrimitives.ConvertTo(enumMetadata.UnderlyingType, typeof(Type), CultureInfo.InvariantCulture);
            }
            else
            {
                underlyingType = typeof(int);
            }

            ModuleBuilder mb = s_moduleBuilder.Value;
            EnumBuilder   eb;

            lock (s_moduleBuilderUsageLock)
            {
                eb = mb.DefineEnum(fullEnumName, TypeAttributes.Public, underlyingType);
            }

            if (enumMetadata.BitwiseFlagsSpecified && enumMetadata.BitwiseFlags)
            {
                var cab = new CustomAttributeBuilder(typeof(FlagsAttribute).GetConstructor(PSTypeExtensions.EmptyTypes), new object[0]);
                eb.SetCustomAttribute(cab);
            }

            foreach (var value in enumMetadata.Value)
            {
                string name         = value.Name;
                object integerValue = LanguagePrimitives.ConvertTo(value.Value, underlyingType, CultureInfo.InvariantCulture);
                eb.DefineLiteral(name, integerValue);
            }

            eb.CreateTypeInfo();
        }
Exemplo n.º 4
0
 internal static string GetEnumFullName(EnumMetadataEnum enumMetadata)
 {
     return(namespacePrefix + "." + enumMetadata.EnumName);
 }
Exemplo n.º 5
0
        private static CodeCompileUnit CreateCodeCompileUnit(EnumMetadataEnum enumMetadata)
        {
            var codeDomProvider = CodeDomProvider.CreateProvider("C#");

            string subnamespaceText = string.Empty;
            string enumNameText;
            int    indexOfLastDot = enumMetadata.EnumName.LastIndexOf('.');

            if (indexOfLastDot < 0)
            {
                enumNameText = enumMetadata.EnumName;
            }
            else
            {
                subnamespaceText = "." + enumMetadata.EnumName.Substring(0, indexOfLastDot);
                enumNameText     = enumMetadata.EnumName.Substring(
                    indexOfLastDot + 1, enumMetadata.EnumName.Length - indexOfLastDot - 1);
            }

            // defense in depth (in case xsd is allowing some invalid identifiers)
            // + xsd allows reserved keywords (i.e. "namespace" passes the regex test, but is not a valid identifier)
            if (!codeDomProvider.IsValidIdentifier(enumNameText))
            {
                var errorMessage = string.Format(
                    CultureInfo.InvariantCulture,
                    ActivityResources.EnumWriter_InvalidEnumName,
                    enumMetadata.EnumName);
                throw new XmlException(errorMessage);
            }
            var newEnum = new CodeTypeDeclaration(codeDomProvider.CreateValidIdentifier(enumNameText))
            {
                IsEnum = true, Attributes = MemberAttributes.Public
            };

            if (enumMetadata.BitwiseFlagsSpecified && enumMetadata.BitwiseFlags)
            {
                newEnum.CustomAttributes.Add(
                    new CodeAttributeDeclaration(new CodeTypeReference(typeof(FlagsAttribute))));
            }

            Type underlyingType = null;

            if (enumMetadata.UnderlyingType != null)
            {
                underlyingType = Type.GetType(enumMetadata.UnderlyingType, false, true);

                if (underlyingType != null)
                {
                    newEnum.BaseTypes.Add(underlyingType);
                }
                else
                {
                    underlyingType = typeof(Int32);
                }
            }
            else
            {
                underlyingType = typeof(Int32);
            }

            foreach (var value in enumMetadata.Value)
            {
                // defense in depth (in case xsd is allowing some invalid identifiers)
                // + xsd allows reserved keywords (i.e. "namespace" passes the regex test, but is not a valid identifier)
                if (!codeDomProvider.IsValidIdentifier(value.Name)) // defense in depth (in case xsd is allowing some invalid identifiers)
                {
                    var errorMessage = string.Format(
                        CultureInfo.InvariantCulture,
                        ActivityResources.EnumWriter_InvalidValueName,
                        value.Name);
                    throw new XmlException(errorMessage);
                }

                var nameValuePair = new CodeMemberField(underlyingType, codeDomProvider.CreateValidIdentifier(value.Name));

                object integerValue = LanguagePrimitives.ConvertTo(
                    value.Value, underlyingType, CultureInfo.InvariantCulture);
                nameValuePair.InitExpression = new CodePrimitiveExpression(integerValue);

                newEnum.Members.Add(nameValuePair);
            }

            var topLevelNamespace = new CodeNamespace(namespacePrefix + subnamespaceText);

            topLevelNamespace.Types.Add(newEnum);

            var codeCompileUnit = new CodeCompileUnit();

            codeCompileUnit.Namespaces.Add(topLevelNamespace);
            codeCompileUnit.ReferencedAssemblies.Add("System.dll");

            return(codeCompileUnit);
        }
Exemplo n.º 6
0
 internal static string GetEnumFullName(EnumMetadataEnum enumMetadata)
 {
     return("Microsoft.PowerShell.Cmdletization.GeneratedTypes." + enumMetadata.EnumName);
 }