DefineLiteral Method

EnumBuilder.DefineLiteral Method (String, Object)


Defines the named static field in an enumeration type with the specified constant value.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public FieldBuilder DefineLiteral(
	string literalName,
	object literalValue


Type: System.String

The name of the static field.

Type: System.Object

The constant value of the literal.

Return Value

Type: System.Reflection.Emit.FieldBuilder

The defined field.

The defined field will have the field attributes Public, Static, and Literal set.


In the .NET Framework versions 1.0 and 1.1, it is necessary to define enumerations using TypeBuilder because EnumBuilder emits enumerations whose elements are of type Int32 instead of the enumeration type. In the .NET Framework version 2.0, EnumBuilder emits enumerations whose elements have the correct type.

The following code example demonstrates the construction of an enumeration within a dynamic assembly, using EnumBuilder. The example defines an enumeration named Elevation, with an underlying type of Int32, and creates two elements: Low, with a value of 0, and High, with a value of 1. After the type has been created, the assembly is saved with the name TempAssembly.dll. You can use the Ildasm.exe (IL Disassembler) to examine the contents of this assembly.


Prior to the .NET Framework version 2.0, this code example does not produce a correct enumeration.

using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
    public static void Main()
        // Get the current application domain for the current thread.
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // Create a dynamic assembly in the current application domain, 
        // and allow it to be executed and saved to disk.
        AssemblyName aName = new AssemblyName("TempAssembly");
        AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
            aName, AssemblyBuilderAccess.RunAndSave);

        // Define a dynamic module in "TempAssembly" assembly. For a single-
        // module assembly, the module has the same name as the assembly.
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

        // Define a public enumeration with the name "Elevation" and an 
        // underlying type of Integer.
        EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));

        // Define two members, "High" and "Low".
        eb.DefineLiteral("Low", 0);
        eb.DefineLiteral("High", 1);

        // Create the type and save the assembly.
        Type finished = eb.CreateType();
        ab.Save(aName.Name + ".dll");

        foreach( object o in Enum.GetValues(finished) )
            Console.WriteLine("{0}.{1} = {2}", finished, o, ((int) o));

/* This code example produces the following output:

Elevation.Low = 0
Elevation.High = 1 

.NET Framework
Available since 1.1
Available since 2.0
Return to top
© 2015 Microsoft