创建使用codeDOM扩展方法 [英] Creating extension method using CodeDOM

查看:121
本文介绍了创建使用codeDOM扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用codeDOM的扩展方法。似乎没有是不允许他们使用 ExtensionAttribute (其中C#内部使用标记扩展方法)的支持。

I'm trying to create an extension method using CodeDOM. There doesn't seem to be any support for them and using ExtensionAttribute (which C# uses internally to mark extension methods) is not allowed.

这是可能的使用一招指定改性剂,但如何让我的包含类静态,从而使code实际上编译?

It's possible to use a trick to specify the this modifier, but how do I make the containing class static, so that the code actually compiles?

由于静态是一个C#的概念,它不是通过codeDOM API公开。并设置 TypeAttributes TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.Public 不起作用,因为

Since static is a C# concept, it's not exposed through the CodeDOM API. And setting TypeAttributes to TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.Public doesn't work, because

抽象类不能被密封或静态

an abstract class cannot be sealed or static

如何让我的扩展方法来编译?

How do I make the extension method to compile?

推荐答案

我是pretty的确定你要找的:

I'm pretty sure you're looking for:

var staticClass = new CodeTypeDeclaration("Extensions")
    {
        Attributes = MemberAttributes.Public|MemberAttributes.Static
    };

然而,这好像不工作。有趣的是:

However, this appears not to work. Interestingly enough:

provider.Supports(GeneratorSupport.StaticConstructors);
// True

provider.Supports(GeneratorSupport.PublicStaticMembers);
// True

但是,但是,当你去把它输出,没有改变,即使Attributes属性清楚地 0x00005002 更改为 0x00006003

<一个href="http://connect.microsoft.com/VisualStudio/feedback/details/526505/microsoft-csharp-csharp$c$cgenerator-outputtypeattributes-does-not-write-static-class-when-$c$ctypedefinition-has-typeattributes-abstract-typeattributes-sealed"相对=nofollow>每微软连接这是不可能的:

感谢您报告这一点。不幸的是,它并不像我们可以支持静态类codeDOM。

Thanks for reporting this. Unfortunately, it doesn't look like we can support static classes for CodeDom.

的原因是,中的codeDOM的设计目标之一是要独立于语言的,这样就可以很容易地对不同的语言来生成一个语言生成的任何code。虽然静态类在C#中经常使用,VB不支持他们。因此,加入支持静态类将意味着约code,它可以编译为C#不会编译为VB,这违背了我们的目标。

The reason is that one of the design goals of CodeDom is to be language-independent, so that any code generated for one language can easily be generated for a different language. While static classes are used often in C#, VB does not support them. Therefore, adding support for static classes will mean that some code that can compile for C# won't be compilable for VB, which goes against our goals.

虽然我们不能在这个问题上采取行动,我们请您继续提供反馈,在未来帮助我们改进。

While we can't act on this issue, we ask that you please continue to provide feedback in the future to help us improve.

一个肮脏的解决方法:

var type = new CodeTypeDeclaration("Extensions");
type.Attributes = MemberAttributes.Public;
type.StartDirectives.Add(
    new CodeRegionDirective(CodeRegionMode.Start, "\nstatic"));
type.EndDirectives.Add(
    new CodeRegionDirective(CodeRegionMode.End, String.Empty));

产地:

#region
static
public class Extensions
{
}
#endregion

这编译。

这篇关于创建使用codeDOM扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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