Skip to content

Oversight in MemberGetEmitter when dealing with enums #17

Description

@cbp123

The relevant code is in MemberGetEmitter (and possibly elsewhere in the codebase):

        if (fieldInfo.DeclaringType.IsEnum)
          this.Generator.ldc_i4((int) fieldInfo.GetValue((object) fieldInfo.DeclaringType)).boxIfValueType(fieldInfo.FieldType);
        else
          this.Generator.ldfld(fieldInfo.IsStatic, fieldInfo).boxIfValueType(fieldInfo.FieldType);

A little known fact of C# is that enums can inherit from shorts, bytes and longs, e.g.

public enum MyEnum : short
{
  Foo = 1,
  Bar = 2
}

Atempting to get a field value for enum that inherits from something other than int32 causes the above code to crash with an InvalidCastException

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions