View Single Post
Old 07-19-2008   #2 (permalink)
Nicholas Paldino [.NET/C# MVP]


 
 

Re: Keeping the order of custom attributes

Dmitry,

No, it isn't. However, if you are using LINQ, you can do so by exposing a property through the attrbitute which exposes the 1, 2, 3, 4, and then ordering on that property.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxx
"Dmitry Nogin" <dmitry.nogin@xxxxxx> wrote in message news:C8A856F5-D61B-484E-918C-F12A77265B60@xxxxxx
Hi,
Is it possible to make the C# compiler to keep the order of Custom attributes as it is in the source file?

For example, I've got the following output from the code snippet below: 4, 3, 1, 2
Can I preserve the original order at the moment of compilation?


[Test(1)]

[Test(2)]

[Test(3)]

[Test(4)]

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();



foreach (TestAttribute a in GetType().GetCustomAttributes(typeof(TestAttribute), true))

MessageBox.Show(a.Ordinal.ToString());

}

}



[AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]

public class TestAttribute : Attribute

{

private readonly int _ordinal;



public TestAttribute(int ordinal)

{

_ordinal = ordinal;

}



public int Ordinal

{

get { return _ordinal; }

}

}



Thanks,

-- dmitry



My System SpecsSystem Spec