/// <summary> /// Create cil source-code representation of a <see cref="EnumDefinition"/>. /// </summary> /// <exception cref="Exceptions.InvalidAssemblyNameException"> /// Thrown when a invalid assembly name is given. /// </exception> /// <exception cref="Exceptions.InvalidNamespaceException"> /// Thrown when a invalid namespace identifier is given. /// </exception> /// <exception cref="Exceptions.OutOfBoundsValueException"> /// Thrown when enum value does not fit in given storage-type. /// </exception> /// <param name="enumDefinition">Enum to generate cil source-code for</param> /// <param name="assemblyName">Name of the assembly to generate</param> /// <param name="namespace">Optional namespace to add the enum to</param> /// <param name="headerMode">Mode to use when adding a header</param> /// <param name="indentMode">Mode to use for indenting</param> /// <param name="spaceIndentSize">When indenting with spaces this controls how many</param> /// <param name="newlineMode">Mode to use for ending lines</param> /// <param name="storageType">Underlying enum storage-type to use</param> /// <param name="curlyBracketMode">Mode to use when writing curly brackets</param> /// <returns>String containing the genenerated cil sourcecode</returns> public static string ExportCil( this EnumDefinition enumDefinition, string assemblyName, string @namespace = null, HeaderMode headerMode = HeaderMode.Default, CodeBuilder.IndentMode indentMode = CodeBuilder.IndentMode.Spaces, int spaceIndentSize = 4, CodeBuilder.NewlineMode newlineMode = CodeBuilder.NewlineMode.Unix, StorageType storageType = StorageType.Implicit, CurlyBracketMode curlyBracketMode = CurlyBracketMode.NewLine) { if (enumDefinition == null) { throw new ArgumentNullException(nameof(enumDefinition)); } if (string.IsNullOrEmpty(assemblyName) || !IdentifierValidator.Validate(assemblyName)) { throw new InvalidAssemblyNameException(assemblyName); } if (!string.IsNullOrEmpty(@namespace) && !IdentifierValidator.ValidateNamespace(@namespace)) { throw new InvalidNamespaceException(@namespace); } foreach (var oobEntry in enumDefinition.Entries.Where(e => !storageType.Validate(e.Value))) { throw new OutOfBoundsValueException(storageType, oobEntry.Value); } var builder = new CodeBuilder(indentMode, spaceIndentSize, newlineMode); if (headerMode != HeaderMode.None) { builder.AddHeader(); builder.WriteEndLine(); } // Add reference to mscorlib. builder.WriteLine(".assembly extern mscorlib { }"); builder.WriteEndLine(); // Add assembly info. builder.Write($".assembly {assemblyName}"); StartScope(builder, curlyBracketMode); builder.WriteLine(".ver 1:0:0:0"); EndScope(builder); builder.WriteEndLine(); // Add module info. builder.WriteLine($".module {assemblyName}.dll"); builder.WriteEndLine(); // Add enum class. builder.AddEnum(enumDefinition, storageType, curlyBracketMode, @namespace); return(builder.Build()); }
/// <summary> /// Create csharp source-code representation of a <see cref="EnumDefinition"/>. /// </summary> /// <exception cref="Exceptions.InvalidNamespaceException"> /// Thrown when a invalid namespace identifier is given. /// </exception> /// <exception cref="Exceptions.OutOfBoundsValueException"> /// Thrown when enum value does not fit in given storage-type. /// </exception> /// <param name="enumDefinition">Enum to generate csharp source-code for</param> /// <param name="namespace">Optional namespace to add the enum to</param> /// <param name="headerMode">Mode to use when adding a header</param> /// <param name="indentMode">Mode to use for indenting</param> /// <param name="spaceIndentSize">When indenting with spaces this controls how many</param> /// <param name="newlineMode">Mode to use for ending lines</param> /// <param name="storageType">Underlying enum storage-type to use</param> /// <param name="curlyBracketMode">Mode to use when writing curly brackets</param> /// <returns>String containing the genenerated csharp sourcecode</returns> public static string ExportCSharp( this EnumDefinition enumDefinition, string @namespace = null, HeaderMode headerMode = HeaderMode.Default, CodeBuilder.IndentMode indentMode = CodeBuilder.IndentMode.Spaces, int spaceIndentSize = 4, CodeBuilder.NewlineMode newlineMode = CodeBuilder.NewlineMode.Unix, StorageType storageType = StorageType.Implicit, CurlyBracketMode curlyBracketMode = CurlyBracketMode.NewLine) { if (enumDefinition == null) { throw new ArgumentNullException(nameof(enumDefinition)); } if (!string.IsNullOrEmpty(@namespace) && !IdentifierValidator.ValidateNamespace(@namespace)) { throw new InvalidNamespaceException(@namespace); } foreach (var oobEntry in enumDefinition.Entries.Where(e => !storageType.Validate(e.Value))) { throw new OutOfBoundsValueException(storageType, oobEntry.Value); } var builder = new CodeBuilder(indentMode, spaceIndentSize, newlineMode); if (headerMode != HeaderMode.None) { builder.AddHeader(); builder.WriteEndLine(); } builder.WriteLine("using System.CodeDom.Compiler;"); builder.WriteEndLine(); if (string.IsNullOrEmpty(@namespace)) { builder.AddEnum(enumDefinition, storageType, curlyBracketMode); } else { builder.AddNamespace( @namespace, b => b.AddEnum(enumDefinition, storageType, curlyBracketMode), curlyBracketMode); } return(builder.Build()); }