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

Enum value in CustomAttributeArgument #950

Open
prestsauce opened this issue Jun 16, 2024 · 0 comments
Open

Enum value in CustomAttributeArgument #950

prestsauce opened this issue Jun 16, 2024 · 0 comments

Comments

@prestsauce
Copy link

I'm working within .net8.0-windows. I'm trying to write a class to a module (dll) using Mono.Cecil.

This class has 2 custom attributes on it. The first attribute has a constructor with a single string argument - no problems here. All works as expected. The second attribute has a constructor with a single enum argument.

The expected outcome should look like this:

  [CustomAttributeType1("someStringArgument")]
  [CustomAttributeType2(MyEnum.SomeValue)]
  public class MyCustomClass : MyCustomClassBase
  {
     ...
  }

The actual output looks like so:

  [CustomAttributeType1("someStringArgument")]
  [CustomAttributeType2]
  public class MyCustomClass : MyCustomClassBase
  {
     ...
  }

Essentially, the constructor is being posted inside the attribute with no arguments or even parentheses.

The code I'm using for CustomAttributeType2 is as follows:


...
assembly/module defined first
...


var enumType = module.ImportReference(typeof(MyEnum));
var attrType2Ctor = module.ImportReference(typeof(CustomAttributeType2).GetConstructor(new Type[] { typeof(MyEnum) }));
var customAttr2 = new CustomAttribute(attrType2Ctor);
customAttr2.ConstructorArguments.Add(new CustomAttributeArgument(enumType, MyEnum.SomeValue));
typeDefinition.CustomAttributes.Add(customAttr2);

I've also tried the following for the constructor argument:

customAttr2.ConstructorArguments.Add(new CustomAttributeArgument(enumType, (int)MyEnum.SomeValue));

I've tried this with other enums, and even got the following result, in which the NAME of the enum has been cut off:

  [CustomAttributeType1("someStringArgument")]
  [Custom]
  public class MyCustomClass : MyCustomClassBase
  {
     ...
  }

Any advice would be appreciated.

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