A few weeks ago, Raymond IM’d me and
asked if I remembered the attribute that you can apply to your class to force
the explicit layout of members. I asked him why he would need or care about the
layout of his class members, and he asked me: “Do you use NUnit?” and I
immediately understood why he cared.
NUnit, in it’s GUI, presents
your test cases in alphabetical oder, not in the order
that you created them in your class. Other programs like Reflector
also present the methods in alpha order, in fact it looks like members
are laid out in IL in this order. This can be a little confusing
when you’re doing TDD,
and you’ve written a bunch of test cases, and you go to implement them. So say
you have this test fixture:
[TestFixture]
public class LiveLinkServiceTest
{
LiveLinkPrincipal [] liveLinkUsers;
[SetUp]
public void SetUp()
{
LiveLinkConfigInfoProvider llcip = new LiveLinkConfigInfoProvider();
liveLinkUsers = llcip.GetAllLiveLinkUsers();
Assert.IsNotNull(liveLinkUsers, "LiveLinkPrinipal Array is NULL");
Assert.IsTrue(liveLinkUsers.Length > 0, "LiveLinkPrinipal Array is EMPTY");
}
[Test]
public void TestGetLiveLinkTransactions()
{
LiveLinkService lls = new LiveLinkService();
ContainerTransactions cts = lls.GetNewTransactions(System.DateTime.Now.Subtract(TimeSpan.FromMinutes(10)),liveLinkUsers[0]);
Assert.IsTrue(cts.CONTAINER_TRANSACTIONS.Count > 0, "Container Transactions is empty");
Console.Out.WriteLine(cts.GetXml());
}
… // Rest of class removed
}
Here’s how it ends up looking in NUnit… Note that the order of my tests are
alphabetical, not in the order I arranged them in my class.

NUnit will load the DLL, and display your methods in alphabetical order,
unless it’s already loaded and cached the tests, in which case it adds new
methods that it finds to the bottom of the tree. This only happens when
the DLL is re-loaded.
It’d be nice to test them one at a time, from top-to-bottom, as you write the
code and go “green”, in the order that you arrange them in
code.
So, here’s the point of this post. Is there an attribute that you can use to
order these methods inside an assembly? I’ve researched StructLayout
Attribute Class. and while it will affect your assembly class’ fields,
methods are not affected.
If there’s not an existing attribute, would it make sense to have one that
programs like Reflector and NUnit, that use reflection to determine class
members, can use to determine method ordering for display purposes?
How about ClassMemberDisplayLayout Attribute Classs? Would this make sense?
-Brendan