Windows Vista Forums

Keeping the order of custom attributes

  1. #1


    Dmitry Nogin Guest

    Keeping the order of custom attributes

    Hi Nicholas,

    My framework supports extra attributes on the grid control datasource (typed DataTable or business object collection) to provide some extra information for my control which is smart enough to read it.

    For example, I have an attribute to generate extra buttons in the grid header to turn on/turn off multiple columns in one touch.

    The usage looks like this:

    [GridSwitch("Name", // string buttonText,

    "FirstName", "LastName")] // params string[] columns

    [GridSwitch("Contacts",

    "Address", "Phone", "Fax")]

    class EmployeeCollection : IBindingList, IList<Employee>

    {

    ...

    }



    class Employee : INotifyPropertyChanged

    {

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Address { get; set; }

    public string Phone { get; set; }

    public string Fax { get; set; }

    }

    So we are about to receive the following grid control on the screen:
    /------------------------------------------------------------------------------\| Employees [Name] [Contacts] | [Organize v] [Customize v] ||------------------------------------------------------------------------------|| First Name | Last Name | Address | Phone | Fax ||------------------------------------------------------------------------------|| Dmitry | Nogin | 130 rue de la...| 123-4567 | 222-2222 || Tim | Horton | 111 Somewhere...| 765-4321 | 333-3333 || | | | | |\------------------------------------------------------------------------------/
    where [Name] and [Contacts] buttons are generated automatically and can toggle visibility of associated columns; the problem is that users of my controls expect me to show the buttons according to the order of attributes.

    I have attributing to specify defaults for many grid settings, like column summaries, formatting, grouping, inplace editors, etc... It's not so common to depend on attribute order, but it happens from time to time. I used to use extra Ordinal parameter, but people do not understand what is the meaning of it; why it is not possible to deduce the desired position from the declaration order, so it would be greate to be able to check its value at debug time or something...


    -- dmitry

    Dmitry,

    You could generate the ordinals at runtime, but they would be generated in the order they are created, not in the manner you are expecting, so that's not really an option.

    What exactly are you trying to do?




    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@xxxxxx
    "Dmitry Nogin" <dmitry.nogin@xxxxxx> wrote in message news:E868EA76-654C-4FC4-95B5-E0022E139671@xxxxxx
    Dear Nicholas,
    Thank you a lot.

    Do I have any chances to generate these ordinals automatically? Or validate its order at runtime while debug build is executed?
    It looks like StackFrame.GetFileLineNumber method will not help here...


    -- dmitry

    "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxx> wrote in message news:96B435C6-69DC-44BD-819E-682C1AA079BF@xxxxxx
    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

  2. #2


    Nicholas Paldino [.NET/C# MVP] Guest

    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

  3. #3


    Dmitry Nogin Guest

    Re: Keeping the order of custom attributes

    Dear Nicholas,
    Thank you a lot.

    Do I have any chances to generate these ordinals automatically? Or validate its order at runtime while debug build is executed?
    It looks like StackFrame.GetFileLineNumber method will not help here...


    -- dmitry

    "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxx> wrote in message news:96B435C6-69DC-44BD-819E-682C1AA079BF@xxxxxx
    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

  4. #4


    Nicholas Paldino [.NET/C# MVP] Guest

    Re: Keeping the order of custom attributes

    Dmitry,

    You could generate the ordinals at runtime, but they would be generated in the order they are created, not in the manner you are expecting, so that's not really an option.

    What exactly are you trying to do?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@xxxxxx
    "Dmitry Nogin" <dmitry.nogin@xxxxxx> wrote in message news:E868EA76-654C-4FC4-95B5-E0022E139671@xxxxxx
    Dear Nicholas,
    Thank you a lot.

    Do I have any chances to generate these ordinals automatically? Or validate its order at runtime while debug build is executed?
    It looks like StackFrame.GetFileLineNumber method will not help here...


    -- dmitry

    "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxx> wrote in message news:96B435C6-69DC-44BD-819E-682C1AA079BF@xxxxxx
    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

  5. #5


    Dmitry Nogin Guest

    Re: Keeping the order of custom attributes

    Hi Nicholas,

    My framework supports extra attributes on the grid control datasource (typed DataTable or business object collection) to provide some extra information for my control which is smart enough to read it.

    For example, I have an attribute to generate extra buttons in the grid header to turn on/turn off multiple columns in one touch.

    The usage looks like this:

    [GridSwitch("Name", // string buttonText,

    "FirstName", "LastName")] // params string[] columns

    [GridSwitch("Contacts",

    "Address", "Phone", "Fax")]

    class EmployeeCollection : IBindingList, IList<Employee>

    {

    ...

    }



    class Employee : INotifyPropertyChanged

    {

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Address { get; set; }

    public string Phone { get; set; }

    public string Fax { get; set; }

    }

    So we are about to receive the following grid control on the screen:
    /------------------------------------------------------------------------------\| Employees [Name] [Contacts] | [Organize v] [Customize v] ||------------------------------------------------------------------------------|| First Name | Last Name | Address | Phone | Fax ||------------------------------------------------------------------------------|| Dmitry | Nogin | 130 rue de la...| 123-4567 | 222-2222 || Tim | Horton | 111 Somewhere...| 765-4321 | 333-3333 || | | | | |\------------------------------------------------------------------------------/
    where [Name] and [Contacts] buttons are generated automatically and can toggle visibility of associated columns; the problem is that users of my controls expect me to show the buttons according to the order of attributes.

    I have attributing to specify defaults for many grid settings, like column summaries, formatting, grouping, inplace editors, etc... It's not so common to depend on attribute order, but it happens from time to time. I used to use extra Ordinal parameter, but people do not understand what is the meaning of it; why it is not possible to deduce the desired position from the declaration order, so it would be greate to be able to check its value at debug time or something...


    -- dmitry

    Dmitry,

    You could generate the ordinals at runtime, but they would be generated in the order they are created, not in the manner you are expecting, so that's not really an option.

    What exactly are you trying to do?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@xxxxxx
    "Dmitry Nogin" <dmitry.nogin@xxxxxx> wrote in message news:E868EA76-654C-4FC4-95B5-E0022E139671@xxxxxx
    Dear Nicholas,
    Thank you a lot.

    Do I have any chances to generate these ordinals automatically? Or validate its order at runtime while debug build is executed?
    It looks like StackFrame.GetFileLineNumber method will not help here...


    -- dmitry

    "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxx> wrote in message news:96B435C6-69DC-44BD-819E-682C1AA079BF@xxxxxx
    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

Keeping the order of custom attributes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing custom types output from custom cmdlet's in C# GUI Dan Finucane PowerShell 0 18 Sep 2008
Custom order of 'All Programs' and 'Favorites' - Impossible? RockinAkin Vista General 5 28 Apr 2008
changing and finding custom dictionary in vista? Office is loaded but I will not be keeping it. Andre Da Costa[ActiveWin] Vista General 11 23 Oct 2007
Custom Control Source Property - Required Attributes Karl 140.6 Avalon 0 07 Aug 2007
Custom Dependency Property in custom class hierarchy not workingcorrectly?! MueMeister Avalon 0 02 Mar 2006