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

Attribute ordering is not preserved when rewriting #558

Closed
SteveGilham opened this issue Jan 12, 2019 · 3 comments
Closed

Attribute ordering is not preserved when rewriting #558

SteveGilham opened this issue Jan 12, 2019 · 3 comments

Comments

@SteveGilham
Copy link
Contributor

SteveGilham commented Jan 12, 2019

Attribute ordering can be significant in some cases e.g. Swagger based applications; rewritten assemblies change this ordering.

Example assembly

using System;

namespace CecilAttr
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public sealed class AAttribute : Attribute
    {
        public string Value { get; set; }
    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public sealed class ZAttribute : Attribute
    {
        public string Value { get; set; }
    }

    public class Class1
    {
        [Z(Value = "First"), A(Value = "Begin"), Z, A(Value = "End")]
        public static int Example2() { return 23; }
    }
}

Rewrite by simply

            var def = AssemblyDefinition.ReadAssembly([path to above assembly]);
            def.Write([somewhere else]);

Decompile the rewritten assembly e.g. by ILSpy to see the reordering

public class Class1
{
	[Z]
	[A(Value = "End")]
	[Z(Value = "First")]
	[A(Value = "Begin")]
	public static int Example2()
	{
		return 23;
	}
}

It doesn't appear to be a simple reversal, nor any simple sort-by-typename -- different inputs give different re-orderings

@SteveGilham
Copy link
Contributor Author

Root cause is that Array.Sort is not a stable sort, and is explicitly documented as such.

@SteveGilham
Copy link
Contributor Author

SteveGilham commented Jan 14, 2019

Stripped-down example using Swagger helpfully provided here -- https://github.com/jedigo/SwaggerAttributes

@jbevain
Copy link
Owner

jbevain commented Jan 14, 2019

Good catch Steve, thanks for reporting this.

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