2016-07-14 29 views
1

私は参照アセンブリを介してビットを検索し、奇妙なコメント/スニペットに気づいた:モジュールをどのようにターゲットに設定できますか?

// Attribute class used by the compiler to mark modules. 
// If present, then debugging information for everything in the 
// assembly was generated by the compiler, and will be preserved 
// by the Runtime so that the debugger can provide full functionality 
// in the case of JIT attach. If not present, then the compiler may 
// or may not have included debugging information, and the Runtime 
// won't preserve the debugging info, which will make debugging after 
// a JIT attach difficult. 
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Module, AllowMultiple = false)] 
[ComVisible(true)] 
public sealed class DebuggableAttribute : Attribute 
{ 

は今、私はAttributeTargets上に飛び乗ったとモジュールは、C#の/であると思いましNET。

[Flags] 
    [ComVisible(true)] 
    [__DynamicallyInvokable] 
    [Serializable] 
    public enum AttributeTargets 
    { 
    [__DynamicallyInvokable] Assembly = 1, 
    [__DynamicallyInvokable] Module = 2, 
    [__DynamicallyInvokable] Class = 4, 
    [__DynamicallyInvokable] Struct = 8, 
    [__DynamicallyInvokable] Enum = 16, 
    [__DynamicallyInvokable] Constructor = 32, 
    [__DynamicallyInvokable] Method = 64, 
    [__DynamicallyInvokable] Property = 128, 
    [__DynamicallyInvokable] Field = 256, 
    [__DynamicallyInvokable] Event = 512, 
    [__DynamicallyInvokable] Interface = 1024, 
    [__DynamicallyInvokable] Parameter = 2048, 
    [__DynamicallyInvokable] Delegate = 4096, 
    [__DynamicallyInvokable] ReturnValue = 8192, 
    [__DynamicallyInvokable] GenericParameter = 16384, 
    [__DynamicallyInvokable] All = GenericParameter | ReturnValue | Delegate | Parameter | Interface | Event | Field | Property | Method | Constructor | Enum | Struct | Class | Module | Assembly, 
    } 

私の質問は、どのように属性を持つC#コードからモジュールをターゲットできますか?

+0

この属性は、コンパイラによって既に生成されています。それを間違って書いてはいけません。*非常に有害な副作用があります。 –

+0

@HansPassant大丈夫 – Mafii

答えて

1

は例えば、module修飾子を使用してください:

[module: Debuggable(DebuggableAttribute.DebuggingModes.Default)] 

は、任意の名前空間/クラスの宣言の上の任意の.csファイルに配置することができます。

モジュールはタイプのコンテナです。ほとんどのコンパイラ(C#を含む)はアセンブリごとに1つのモジュールを発行します。 ILSpyなどの逆アセンブラツールで、モジュールレベルの属性を表示できます。

これは、VB.NETモジュールと同じではないことに注意してください。これは、C#静的クラスに似ています。

+0

どこに置くのですか?クラスの上、名前空間の上に? – Mafii

+0

AssemblyInfo.csが最適な場所です。これは、ブロック内にあっても、usingステートメント以外でも、他のアセンブリ/モジュールレベルの属性の後に置くこともできません。 –

+0

@ felix-b大丈夫、ありがとう! – Mafii

関連する問題