AArnott/ImmutableObjectGraph — pull request #27 by evilglee (2014-11-05 18:06:36+00:00)
Title: Adding an [External] Attribute

Immutable Objects can reference types defined in a previously-compiled
DLL via the <#@ assembly #> directive. However, there's no easy way to
reference types defined in the *same* assembly as your .tt file, because
that would create a circular dependency between the outputs of the T4
preprocessor and the C# compiler.

This change adds an [External] attribute that can be used within your
.tt file to enable references to an external type. Usage is fairly
simple:

In your .tt file:

[External]
enum FruitColor { } // <-- note empty braces

class Fruit { // an ordinary Immutable declaration
FruitColor color; // use of FruitColor like any other type
// other declarations
}

In any .cs file in the same project:

// The real declaration of FruitColor
enum FruitColor {
Golden,
Green,
Yellow
}

This works for any type: enums, structs, or classes. (Although if your
immutable object contains references to mutable classes, I take no blame
for your design!)

Under the hood, the [External] attribute just causes a type to be
compiled in as normal, but not included in the generated output. Thus
you'll end up with a .generated.cs file that uses the type but doesn't
define it. When that's run through the C# compiler alongside a normal
.cs file that defines the type, everything ends up working nicely.