Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is metaprogramming possible in C#?

In particular, would it be possible to have code similar to this c++ code executed at compile time in c#?

template <int N> struct Factorial  {     enum { value = N * Factorial<N - 1>::value }; };  template <> struct Factorial<0>  {     enum { value = 1 }; };  // Factorial<4>::value == 24 // Factorial<0>::value == 1 void foo() {     int x = Factorial<4>::value; // == 24     int y = Factorial<0>::value; // == 1 } 
like image 238
Brian R. Bondy Avatar asked Oct 26 '08 01:10

Brian R. Bondy


People also ask

Does C have metaprogramming?

Metaprogramming in C is the art of creating new language constructs, typically using macros.

Which language is best for metaprogramming?

Lisp is probably the quintessential language with metaprogramming facilities, both because of its historical precedence and because of the simplicity and power of its metaprogramming.

Are generics metaprogramming?

Again, in general, a metaprogram is a program that yields another program, whereas generic programming is about parametrized(usually with other types) types(including functions) .

What is metaprogramming system?

Design your own Domain Specific Language with full development environment. MPS is a language workbench that targets Domain-specific Languages. With MPS you can design your own extensible DSLs and start using them right away to build end-user applications.


1 Answers

No, metaprogramming of this complexity is not supported directly by the C# language. However, like @littlegeek said, the Text Template Transformation Toolkit included with Visual Studio will allow you to achieve code generation of any complexity.

like image 168
Jacob Krall Avatar answered Oct 29 '22 02:10

Jacob Krall