December 2010 - Posts
In other words, how to use T4 templates without ANY runtime dependencies? Yes, it is possible, and quite simple and elegant actually.
In a desktop project, just open the Add New Item dialog, and search for "text template":

From the two available templates, the one that gives you a zero-dependency runtime-usable template is the first one: Preprocessed Text Template.
Once unfolded, you get the .tt file, but also a dependent .cs file automatically generated. Note the Custom Tool associated with the file:

If you open up the .cs file, you will see that it doesn't contain the rendered "Hello World!!!" I added in the .tt, but rather a full class named after the template file itself:
namespace ConsoleApplication1
{
using System;
#line 1 "C:\Temp\ConsoleApplication1\ConsoleApplication1\PreTextTemplate1.tt"
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "10.0.0.0")]
public partial class PreTextTemplate1 : PreTextTemplate1Base
{
public virtual string TransformText()
{
this.GenerationEnvironment = null;
this.Write("Hello World!!!");
return this.GenerationEnvironment.ToString();
}
}
#region Base class
...
#endregion
}
...
Read full article
Sometimes, you just have a reusable helper that you bring in to every project (i.e. an argument validation thingy, or the static reflection stuff, useful extension methods, and so on). You don't always need them all, they are generally single-file utilities, and having a Common.dll looks overkill or dumb.
Loose source file sharing has always been problematic, though: no packaging and versioning mechanism (unlike an assembly), hard to integrate with source control systems (SVN has external repository mappings, but nothing like that exists in TFS or Mercurial, AFAIK), and so on. I tried source files reuse before, with somewhat poor results.
In a recent project I wanted to reuse (again) that darn useful Guard.cs file. But I set to do it differently this time, and see if my "dream" for lightweight loose source file reuse could still be achieved.
Two key technologies enable some pretty cool and robust source file reuse nowadays:
- NuGet: unless you've been living under a rock, you probably heard all the ...

Read full article
You might think this is a trivial thing, with Type.BaseType and Type.GetInterfaces already there. But there's catch: the GetInterfaces method will give you all the implemented interfaces by the concrete type, as well as all its base types, as well as all the interfaces inherited by other interfaces it implements. What a mess! To make it more clear, say you have the following types:
public interface IBase
public class Base : IBase, ICloneable
public class Derived : Base
If you do
typeof(Derived).GetInterfaces().ToList().ForEach(t => Console.WriteLine(t.FullName));
Here's what you get:
Test.IBase
System.ICloneable
If you look back to Derived definition, it doesn't implement any interfaces itself, but rather it's the base class. So, in order to have a more precise information about the type, we should be able to get this instead:
Derived
Base
Object
IBase
ICloneable
Now, this could be very useful in a few scenarios. My particular one involves finding an adapter that is registered for the closest type to the actual instance (i.e. if I have adapters registered for Derived, IBase and ICloneable, I want to be able to give them priority automatically based on where they appear in the precise type hierarchy)....
Read full article
More Posts