tags:

views:

184

answers:

7
+5  Q: 

Templates in C#

I know generics are in C# to fulfill a role similar to C++ templates but I really need a way to generate some code at compile time - in this particular situation it would be very easy to solve the problem with C++ templates.

Does anyone know of any alternatives? Perhaps a VS plug-in that preprocesses the code or something like that? It doesn't need to be very sophisticated, I just need to generate some methods at compile time.

Here's a very simplified example in C++ (note that this method would be called inside a tight loop with various conditions instead of just "Advanced" and those conditions would change only once per frame - using if's would be too slow and writing all the alternative methods by hand would be impossible to maintain). Also note that performance is very important and that's why I need this to be generated at compile time.

template <bool Advanced>
int TraceRay( Ray r )
{
    do
    {
        if ( WalkAndTestCollision( r ) )
        {
            if ( Advanced )
                return AdvancedShade( collision );
            else
                return SimpleShade( collision );
        }
    }
    while ( InsideScene( r ) );
}
+1  A: 

Not really, as far as I know. You can do this type of thing at runtime, of course; a few meta-programming options, none of them trivial:

  • reflection (the simplest option if you don't need "fastest possible")
  • CSharpCodeProvider and some code-generation
  • the same with CodeDom
  • ILGenerator if you want hardcore
Marc Gravell
Also PostSharp (www.sharpcrafters.com) is in the same category and could be used.
cfeduke
Unfortunately performance is essential so it has to be generated at compile time.
John Doe
+9  A: 

You can use T4.

EDIT: In your example, you can use a simple bool parameter.

SLaks
+1: This is probably the most feature-rich option presented so far. (Though some explanation would help too ;) )
Reed Copsey
T4 seems to do what I want, thanks! It`s not nearly as clean as C++ templates but it`s just one bottleneck class so I`m probably going with it.
John Doe
A: 

Generics does work as templates, if that the case. There is a way to create code in runtime -

Check is CodeProject Example:

CodeProject

Dani
He wants to do this at compile time.
SLaks
A: 

In addition to Marc's excellent suggestions, you may want to have a look at PostSharp.

Lucero
A: 

I've done some Meta-Programming - style tricks using static generics that use reflection (and now I'm using dynamic code generation using System.Linq.Expressions; as well having used ILGenerator for some more insane stuff). http://www.lordzoltan.org/blog/post/Pseudo-Template-Meta-Programming-in-C-Sharp-Part-2.aspx for an example I put together (sorry about the lack of code formatting - it's a very old post!) that might be of use.

I've also used T4 (link goes to a series of tutorials by my favourite authority on T4 - Oleg Sych), as suggested by SLaks - which is a really nice way to generate code, especially if you're also comfortable with Asp.Net-style syntax. If you generate partial classes using the T4 output, then the developer can then embellish and add to the class however they see fit.

If it absolutely has to be compile-time - then I'd go for T4 (or write your own custom tool, but that's a bit heavy). If not, then a static generic could help, probably in partnership with the kind of solutions mentioned by Marc.

Andras Zoltan
A: 

If you want true code generation, you could use CodeSmith http://www.codesmithtools.com which isn't free/included like T4, but has some more features, and can function as a VS.NET plug-in.

goofballLogic
A: 

Here's an older article that uses genetic programming to generate and compile code on the fly:

http://msdn.microsoft.com/en-us/magazine/cc163934.aspx

"The Generator class is the kernel of the genetic programming application. It discovers available base class terminals and functions. It generates, compiles, and executes C# code to search for a good solution to the problem it is given. The constructor is passed a System.Type which is the root class for .NET reflection operations."

Might be overkill for your situation, but does show what C# can do. (Note this article is from the 1.0 days)

Shane Cusson