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(); }
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(Int32); } 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); } ClrFacade.CreateEnumType(eb); }
private static void CompileEnum(EnumMetadataEnum enumMetadata) { try { string enumFullName = EnumWriter.GetEnumFullName(enumMetadata); lock (_enumCompilationLock) { Type type; if (!LanguagePrimitives.TryConvertTo<Type>(enumFullName, CultureInfo.InvariantCulture, out type)) { EnumWriter.Compile(enumMetadata); } } } catch (Exception exception) { throw new XmlException(string.Format(CultureInfo.InvariantCulture, CmdletizationCoreResources.ScriptWriter_InvalidEnum, new object[] { enumMetadata.EnumName, exception.Message }), exception); } }
internal static string GetEnumFullName(EnumMetadataEnum enumMetadata) { return ("Microsoft.PowerShell.Cmdletization.GeneratedTypes." + enumMetadata.EnumName); }
private EnumMetadataEnum Read47_EnumMetadataEnum(bool isNullable, bool checkType) { XmlQualifiedName type = checkType ? base.GetXsiType() : null; bool flag = false; if (isNullable) { flag = base.ReadNull(); } if ((checkType && (type != null)) && ((type.Name != this.id38_EnumMetadataEnum) || (type.Namespace != this.id2_Item))) { throw base.CreateUnknownTypeException(type); } if (flag) { return null; } EnumMetadataEnum o = new EnumMetadataEnum(); EnumMetadataEnumValue[] a = null; int length = 0; bool[] flagArray = new bool[4]; while (base.Reader.MoveToNextAttribute()) { if ((!flagArray[1] && (base.Reader.LocalName == this.id42_EnumName)) && (base.Reader.NamespaceURI == this.id4_Item)) { o.EnumName = base.Reader.Value; flagArray[1] = true; } else { if ((!flagArray[2] && (base.Reader.LocalName == this.id43_UnderlyingType)) && (base.Reader.NamespaceURI == this.id4_Item)) { o.UnderlyingType = base.Reader.Value; flagArray[2] = true; continue; } if ((!flagArray[3] && (base.Reader.LocalName == this.id44_BitwiseFlags)) && (base.Reader.NamespaceURI == this.id4_Item)) { o.BitwiseFlags = XmlConvert.ToBoolean(base.Reader.Value); o.BitwiseFlagsSpecified = true; flagArray[3] = true; continue; } if (!base.IsXmlnsAttribute(base.Reader.Name)) { base.UnknownNode(o, ":EnumName, :UnderlyingType, :BitwiseFlags"); } } } base.Reader.MoveToElement(); if (base.Reader.IsEmptyElement) { base.Reader.Skip(); o.Value = (EnumMetadataEnumValue[]) base.ShrinkArray(a, length, typeof(EnumMetadataEnumValue), true); return o; } base.Reader.ReadStartElement(); base.Reader.MoveToContent(); int whileIterations = 0; int readerCount = base.ReaderCount; while ((base.Reader.NodeType != XmlNodeType.EndElement) && (base.Reader.NodeType != XmlNodeType.None)) { if (base.Reader.NodeType == XmlNodeType.Element) { if ((base.Reader.LocalName == this.id41_Value) && (base.Reader.NamespaceURI == this.id2_Item)) { a = (EnumMetadataEnumValue[]) base.EnsureArrayIndex(a, length, typeof(EnumMetadataEnumValue)); a[length++] = this.Read36_EnumMetadataEnumValue(false, true); } else { base.UnknownNode(o, "http://schemas.microsoft.com/cmdlets-over-objects/2009/11:Value"); } } else { base.UnknownNode(o, "http://schemas.microsoft.com/cmdlets-over-objects/2009/11:Value"); } base.Reader.MoveToContent(); base.CheckReaderCount(ref whileIterations, ref readerCount); } o.Value = (EnumMetadataEnumValue[]) base.ShrinkArray(a, length, typeof(EnumMetadataEnumValue), true); base.ReadEndElement(); return o; }
private void Write47_EnumMetadataEnum(string n, string ns, EnumMetadataEnum o, bool isNullable, bool needType) { if (o == null) { if (isNullable) { base.WriteNullTagLiteral(n, ns); } } else { if (!needType && !(o.GetType() == typeof(EnumMetadataEnum))) { throw base.CreateUnknownTypeException(o); } base.WriteStartElement(n, ns, o, false, null); if (needType) { base.WriteXsiType("EnumMetadataEnum", "http://schemas.microsoft.com/cmdlets-over-objects/2009/11"); } base.WriteAttribute("EnumName", "", o.EnumName); base.WriteAttribute("UnderlyingType", "", o.UnderlyingType); if (o.BitwiseFlagsSpecified) { base.WriteAttribute("BitwiseFlags", "", XmlConvert.ToString(o.BitwiseFlags)); } EnumMetadataEnumValue[] valueArray = o.Value; if (valueArray != null) { for (int i = 0; i < valueArray.Length; i++) { this.Write36_EnumMetadataEnumValue("Value", "http://schemas.microsoft.com/cmdlets-over-objects/2009/11", valueArray[i], false, false); } } bool bitwiseFlagsSpecified = o.BitwiseFlagsSpecified; base.WriteEndElement(o); } }
private static void CompileEnum(EnumMetadataEnum enumMetadata) { try { string enumFullName = EnumWriter.GetEnumFullName(enumMetadata); lock (s_enumCompilationLock) { Type alreadyExistingType; if (!LanguagePrimitives.TryConvertTo(enumFullName, CultureInfo.InvariantCulture, out alreadyExistingType)) { EnumWriter.Compile(enumMetadata); } } } catch (Exception e) { var errorMessage = string.Format( CultureInfo.InvariantCulture, CmdletizationCoreResources.ScriptWriter_InvalidEnum, enumMetadata.EnumName, e.Message); throw new XmlException(errorMessage, e); } }
internal static string GetEnumFullName(EnumMetadataEnum enumMetadata) { return namespacePrefix + "." + enumMetadata.EnumName; }
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; }
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(); }