This is a multi-part message in MIME format.
------=_NextPart_000_0029_01C8E9F2.0D2C65B0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
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();
=20
foreach (TestAttribute a in =
GetType().GetCustomAttributes(typeof(TestAttribute), true))
MessageBox.Show(a.Ordinal.ToString());
}
}
=20
[AttributeUsage(AttributeTargets.Class, AllowMultiple=3Dtrue)]
public class TestAttribute : Attribute
{
private readonly int _ordinal;
=20
public TestAttribute(int ordinal)
{
_ordinal =3D ordinal;
}
=20
public int Ordinal=20
{
get { return _ordinal; }
}
}
Thanks,
-- dmitry
------=_NextPart_000_0029_01C8E9F2.0D2C65B0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.6001.18063" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Is it possible to make the C# compiler =
to keep the=20
order of Custom attributes as it is in the source file?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>For example, I've got the following =
output from the=20
code snippet below: 4, 3, 1, 2</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Can I preserve the original order at =
the moment of=20
compilation?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial =
size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>[<SPAN=20
style=3D"COLOR: #2b91af">Test</SPAN>(1)]<?xml:namespace prefix =3D o ns =
=3D=20
"urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>[<SPAN=20
style=3D"COLOR: #2b91af">Test</SPAN>(2)]<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>[<SPAN=20
style=3D"COLOR: #2b91af">Test</SPAN>(3)]<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>[<SPAN=20
style=3D"COLOR: #2b91af">Test</SPAN>(4)]<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN><SPAN=20
style=3D"COLOR: blue">public</SPAN> <SPAN style=3D"COLOR: =
blue">partial</SPAN> <SPAN=20
style=3D"COLOR: blue">class</SPAN> <SPAN style=3D"COLOR: =
#2b91af">Form1</SPAN> :=20
<SPAN style=3D"COLOR: #2b91af">Form<o:p></o:p></SPAN></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =
</SPAN>{<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN><SPAN style=3D"COLOR: blue">public</SPAN> =
Form1()<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN>{<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: =
yes"> =20
</SPAN>InitializeComponent();<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><o:p> </o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: =
yes"> =20
</SPAN><SPAN style=3D"COLOR: blue">foreach</SPAN> (<SPAN=20
style=3D"COLOR: #2b91af">TestAttribute</SPAN> a <SPAN=20
style=3D"COLOR: blue">in</SPAN> GetType().GetCustomAttributes(<SPAN=20
style=3D"COLOR: blue">typeof</SPAN>(<SPAN=20
style=3D"COLOR: #2b91af">TestAttribute</SPAN>), <SPAN=20
style=3D"COLOR: blue">true</SPAN>))<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: =
yes"> &n=
bsp; =20
</SPAN><SPAN=20
style=3D"COLOR: =
#2b91af">MessageBox</SPAN>.Show(a.Ordinal.ToString());<o:p></o:p></SPAN><=
/P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN>}<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =
</SPAN>}<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><o:p> </o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>[<SPAN=20
style=3D"COLOR: #2b91af">AttributeUsage</SPAN>(<SPAN=20
style=3D"COLOR: #2b91af">AttributeTargets</SPAN>.Class, =
AllowMultiple=3D<SPAN=20
style=3D"COLOR: blue">true</SPAN>)]<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN><SPAN=20
style=3D"COLOR: blue">public</SPAN> <SPAN style=3D"COLOR: =
blue">class</SPAN> <SPAN=20
style=3D"COLOR: #2b91af">TestAttribute</SPAN> : <SPAN=20
style=3D"COLOR: #2b91af">Attribute<o:p></o:p></SPAN></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =
</SPAN>{<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN><SPAN style=3D"COLOR: blue">private</SPAN> <SPAN=20
style=3D"COLOR: blue">readonly</SPAN> <SPAN style=3D"COLOR: =
blue">int</SPAN>=20
_ordinal;<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><o:p> </o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN><SPAN style=3D"COLOR: blue">public</SPAN> TestAttribute(<SPAN=20
style=3D"COLOR: blue">int</SPAN> ordinal)<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN>{<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: =
yes"> =20
</SPAN>_ordinal =3D ordinal;<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN>}<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><o:p> </o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN><SPAN style=3D"COLOR: blue">public</SPAN> <SPAN=20
style=3D"COLOR: blue">int</SPAN> Ordinal <o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN>{<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: =
yes"> =20
</SPAN><SPAN style=3D"COLOR: blue">get</SPAN> { <SPAN=20
style=3D"COLOR: blue">return</SPAN> _ordinal; }<o:p></o:p></SPAN></P>
<P class=3DMsoNormal=20
style=3D"MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; =
mso-layout-grid-align: none"><SPAN=20
style=3D"FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-language: =
HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> =20
</SPAN>}<o:p></o:p></SPAN></P>
<P class=3DMsoNormal style=3D"MARGIN: 0cm 0cm 10pt"><SPAN=20
style=3D"FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; =
mso-bidi-language: HE; mso-no-proof: yes"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>}</SPAN></P>
<P class=3DMsoNormal style=3D"MARGIN: 0cm 0cm 10pt"><SPAN=20
style=3D"FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; =
mso-bidi-language: HE; mso-no-proof: yes"></SPAN> </P>
<P class=3DMsoNormal style=3D"MARGIN: 0cm 0cm 10pt"><SPAN=20
style=3D"FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; =
mso-bidi-language: HE; mso-no-proof: yes">Thanks,</SPAN></P>
<P class=3DMsoNormal style=3D"MARGIN: 0cm 0cm 10pt"><SPAN=20
style=3D"FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; =
mso-bidi-language: HE; mso-no-proof: yes">--=20
dmitry</SPAN></P>
<P class=3DMsoNormal style=3D"MARGIN: 0cm 0cm 10pt"><SPAN=20
style=3D"FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; =
mso-bidi-language: HE; mso-no-proof: =
yes"></SPAN> </P></FONT></DIV></BODY></HTML>
------=_NextPart_000_0029_01C8E9F2.0D2C65B0--