簡體   English   中英

C#:我可以或如何為方法參數指定屬性嗎?

[英]C#: Can I or how to assign attributes to method parameters?

C#文檔說您可以為參數分配自定義屬性。 確切的句子是:“目標元素可以是程序集,類,構造函數,委托,枚舉,事件,字段,接口,方法,可移植可執行文件模塊,參數,屬性,返回值,結構或其他屬性。” 鑒於此,做這樣的事情的正確語法是什么:

private void SomeMethod
([CustomAttribute(Blah = "blah1")] string actualParam,
 [CustomAttribute(Blah = "blah2")] DateTime anotherParam
)

{
// method's body
}

或者我完全錯過了什么?

這就像你說的那樣。 對於參數,您將執行以下操作:

void SomeMethod([MarshalAs(UnmanagedType.FunctionPtr)] Action del);

對於返回值,您可以:

[return: MyAttribute(Param1 = "Value1")]
int Method()

類似地,模塊和程序集有[module: MyAttribute][assembly: MyAttribute]

沒關系,伙計們。 它實際上可能與我輸入的語法完全相同。 屬性類只需要具有AttributeTargets.Parameter命名參數。 對不起,它發生:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM