我敢肯定你正在尋找:
var staticClass = new CodeTypeDeclaration("Extensions")
{
Attributes = MemberAttributes.Public|MemberAttributes.Static
};
然而,這似乎不工作。有趣的是:
provider.Supports(GeneratorSupport.StaticConstructors);
// True
provider.Supports(GeneratorSupport.PublicStaticMembers);
// True
但是,然而,當你去輸出它,即使Attributes屬性明顯不同於0x00005002
到0x00006003
變化沒有變化。
Per Microsoft Connect this is not possible:
感謝您報告這一點。不幸的是,它看起來不像我們可以支持CodeDom的靜態類。
原因是CodeDom的設計目標之一是獨立於語言,因此爲一種語言生成的任何代碼都可以很容易地爲其他語言生成。儘管C#中經常使用靜態類,但VB不支持它們。因此,添加對靜態類的支持意味着一些可以編譯爲C#的代碼將不能編譯爲VB,這違背了我們的目標。
儘管我們無法就此問題採取行動,但我們希望您在將來繼續提供反饋意見,以幫助我們改進。
一個骯髒的解決方法:
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
來編譯。
未來的讀者:我覺得固溶現在是[標籤:Roslyn],併爲這個類添加一個靜態修飾符。 – user7116 2013-04-12 16:20:54