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

Better support for dropping attributes #345

Closed
MWstudios opened this issue Jan 12, 2024 · 5 comments
Closed

Better support for dropping attributes #345

MWstudios opened this issue Jan 12, 2024 · 5 comments

Comments

@MWstudios
Copy link

I need to drop multiple types with different attributes and I've noticed that due to RepackImporter.ShouldDrop<TMerge>(), ILMerge supports only one attribute. Why? I suggest supporting multiple attributes separated by a semicolon (this won't collide with any naming conventions and complement MSBuild item collections).

Also, I noticed that the code checks only for fields, methods, types, events and properties. Hence I suggest including module and assembly attributes as well.

@KirillOsenkov
Copy link
Collaborator

To clarify, are you looking to drop the members marked with an attribute or the attribute itself? Ideally if you could provide a tiny C# console app that demonstrates what you want and what your desired behavior is.

@MWstudios
Copy link
Author

I'm thinking of this:

[Attribute1] //drop these
class A { }
[Attribute2] //also drop these
class B { }

If I want to drop both of these attributes, I need to run ILRepack twice with each configuration set to drop one of these two attributes. The bottleneck is in ILRepack/RepackOptions.cs at line 118:

public string RepackDropAttribute { get; set; }

And in ILRepack/RepackImporter.cs at line 248:

// skip members marked with a custom attribute named as /repackdrop:RepackDropAttribute
var shouldDrop = member.HasCustomAttributes
    && member.CustomAttributes.FirstOrDefault(attr => attr.AttributeType.Name == _options.RepackDropAttribute) != null;

If there was, say, string[] instead of string, ILRepack could effectively drop multiple attributes in one go. Since the argument is passed into the program by /repackdrop, I suggested separating multiple attributes via a semicolon. So, in ILRepack/RepackOptions.cs at line 286, this:

RepackDropAttribute = cmd.Option("repackdrop");

would be changed to this:

RepackDropAttribute = cmd.Option("repackdrop").Split(';');

Since semicolons are prohibited in C# namings, there would be no name conflicts in the process. As far as I'm aware there is nothing else stopping this from being implemented.

@KirillOsenkov
Copy link
Collaborator

Thanks, your answer doesn't clearly answer my question:

To clarify, are you looking to drop the members marked with an attribute or the attribute itself?

In other words, for

[Attribute1] //drop these
class A { }

do you expect just the attribute to be deleted, or class A as well?

@MWstudios
Copy link
Author

I want to drop the members marked with the attribute, which is exactly what ILRepack does. But it only drops one attribute per run, so I need to create multiple configurations and them run them all in succession before building. I'm proposing a way to drop all members marked with at least one of the attributes provided. So, to drop both classes A and B, I would be able to write:

<ILRepack Internalize="false" InputAssemblies="$(OutputPath)DLL.dll" TargetKind="Dll" DebugInfo="true" OutputFile="$(OutputPath)DLL.dll" RepackDropAttribute="Attribute1;Attribute2"/>

instead of running ILRepack twice:

<ILRepack Internalize="false" InputAssemblies="$(OutputPath)DLL.dll" TargetKind="Dll" DebugInfo="true" OutputFile="$(OutputPath)DLL.dll" RepackDropAttribute="Attribute1"/>
<ILRepack Internalize="false" InputAssemblies="$(OutputPath)DLL.dll" TargetKind="Dll" DebugInfo="true" OutputFile="$(OutputPath)DLL.dll" RepackDropAttribute="Attribute2"/>

@KirillOsenkov
Copy link
Collaborator

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

2 participants