Disable AuthoringTests.txt
Creating a new test project in Visual Studio 2005 and 2008, Microsoft automatically generates a file in the test project called “AuthoringTests.txt”. After creating a dozen or so test projects, this file really starts getting in the way. Fortunately there is an easy way to disable Visual Studio from including this file in any new test project.
It is fairly simple. Click on Tools>Options>Test Tools>Test Project and uncheck the checkbox that reads “About Test Projects” introduction file and voila, no more AuthoringTests.txt!
Below is a screen shot of the setting:
This setting is found in the same location for Visual Studio 2005 and 2008.

Tech-ed 2008

Tech-ed South Africa 2008!
This was my first Tech-ed ever and what a fantastic experience! The event kicked off on 3 August 2008 at the ICC in Durban for three days.
Arriving at the airport we noticed this little oddity in the spelling of experience. What a laugh; I hope Microsoft gets it right next time! What they did get right however was a very well organised conference. No transport problems and they surely did look after our stomachs very well. So, maybe they can’t spell but they can surely organise an event!

The Opening Keynote was spectacular. Lasers, dancers and a whole lot of action. There were approximately 2200 people attending this event, of which 14% was female.
A great party was sponsored by Dimension Data called the Speed of Sound, which carried on way into the night. A superior live band called Prime Circle was there to get the event started on the right note. Good choice Microsoft!

A beach party was sponsored by the local municipality of Durban. What an awesome time. Karen Zoid was there live in action to entertain a bunch of IT people. I’m sure it must have been a strange sight for her to see that IT people can actually rock!
For the Closing Keynote, Stephen Attenborough, the commercial director of Virgin Galactic was the presenter and talked about their latest spaceship SpaceshipTwo and Mothership Eve. All-in-all a great conference. In the next upcoming posts I’ll share some information from the sessions that I attended.

Dynamic type factory
Wow, it has been crazy for a while, but I finally got it together :). So, on with our little aspect oriented framework.
The first building block in our aspect proxy framework is the Dynamic Type Factory. As you might have guessed, this is the component that we will use to define a dynamic assembly that will hold our in-memory assembly with the dynamically created types. In other words, we have an assembly that is created in-memory as a container for the proxy classes that we will create and instantiate.
A couple of static variables and constants are needed for the DynamicTypeFactory class which are:
private const string ASSEMBLY_NAME = "AspectProxies";
private static ModuleBuilder _ModuleBuilder;
private static AssemblyBuilder _AssemblyBuilder;
private static string _FileName =
string.Format("{0}.dll", ASSEMBLY_NAME);
private static AssemblyName _AssemblyName =
new AssemblyName(ASSEMBLY_NAME);
First of all, we need to give the assembly some sort of name and I thought “AspectProxies” to be a good fit. We then declare an AssemblyBuilder which we will use only to define a dynamic module. We then declare a ModuleBuilder which we will use to define our dynamic types with. We set a FileName variable which we will expose via a property so that others can know what the assembly file is called when it saved to disk. Lastly we declare an AssemlyName variable that we will provide to the AssemblyBuilder.
For simplicity we will only have one dynamic assembly in the current AppDomain. Hence, the DynamicTypeFactory class will have a static constructor that creates a new dynamic assembly and module, like so:
static DynamicTypeFactory()
{
_AssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(
_AssemblyName, AssemblyBuilderAccess.RunAndSave);
_ModuleBuilder = _AssemblyBuilder.DefineDynamicModule(
ASSEMBLY_NAME, _FileName);
}
We then create a property to expose our filename, like so:
public static string FileName
{
get { return _FileName; }
}
Next, we define the most commonly used method in this class, that we will call to define our dynamic types with:
public static TypeBuilder Create<T>(T instance, string identifier)
{
if (!typeof(T).IsInterface)
throw new ArgumentException("Type T must be an interface.");
if (instance == null)
throw new ArgumentNullException("instance"));
if (string.IsNullOrEmpty(identifier))
identifier = Guid.NewGuid().ToString("N");
return _ModuleBuilder.DefineType(
string.Format("{0}_Proxy", identifier),
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed,
typeof(object),
Resolver.GetInterfaces(instance.GetType()));
}
In this method we firstly check whether the type of T is indeed an interface, since we only want to create proxy types for interfaces. We also check that the instance that we were provided with is not null. The reason we need to check this is that we are going to find all of the interfaces that the instance implements, since the interface might not necessarily inherit from the same interfaces.
Obviously, every type that we define must have a unique name and this is where the identifier comes in. Sometimes we would want a name that we can recognize such as the type the proxy is for and maybe some of the interfaces. Using reflector to examine our types can become rather painful to examine if we have a thousand types and no way to identify them. Just for safety we check if we an identifier, if not then we will just assign it a GUID value. This identifier is later on post-pended with “_Proxy”.
The next bit actually defines the type in the dynamic module. We supply a couple of attributes that says that our type must be a public sealed class which derives from object. The next parameter resolves or gets all of the interfaces on our instance and supplies them for the type definition. Now we defined a type in the meta data but still need to give this type a body.
When debugging, it is useful to have the ability to save the proxy type to the disk. For that we need to have a method like so:
public static void Save()
{
_AssemblyBuilder.Save(_FileName);
}
Now that we are able to define types dynamically we will next look at how to create the body of for this proxy type.


leave a comment