在C#中可以进行元编程吗? [英] Is metaprogramming possible in C#?

查看:249
本文介绍了在C#中可以进行元编程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尤其是,是否有可能在编译时在c#中使类似于此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
}

推荐答案

否,C#语言不直接支持这种复杂性的元编程.但是,就像 @littlegeek 所说,

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.

这篇关于在C#中可以进行元编程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆