Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I set constant value to an enum field when creating a new enum #922

Open
SwingCosmic opened this issue Aug 10, 2023 · 0 comments
Open

Comments

@SwingCosmic
Copy link

I can't set value to FieldDefinition.Constant and FieldDefinition.HasConstant because it depends on the constant value inside of the assembly module.

private TypeDefinition CloneEnum(TypeDefinition type,  ModuleDefinition target)
{
    var baseType = target.ImportReference(typeof(Enum));
    var newEnum = new TypeDefinition(type.Namespace, type.Name, type.Attributes, baseType);

    Console.WriteLine(type.Name);
    foreach (var f in type.Fields)
    {
        FieldDefinition field;
        if (!f.HasConstant) // value__
        {
            field = new FieldDefinition(f.Name, f.Attributes, target.TypeSystem.Int32);
        } else
        {
            field = new FieldDefinition(f.Name, f.Attributes, newEnum);
            // next two lines doesn't work
            field.HasConstant = true; //still false
            field.Constant = f.Constant; // still null
        }
        newEnum.Fields.Add(field);
    }
    return newEnum;
}

The result assembly contains the enums without any values.

image

And I'am really confused about this two properties:

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant