4 c#
C#文档说您可以为参数分配自定义属性.确切的句子是:"目标元素可以是程序集,类,构造函数,委托,枚举,事件,字段,接口,方法,可移植可执行文件模块,参数,属性,返回值,结构或其他属性." 鉴于此,做这样的事情的正确语法是什么:
private void SomeMethod
([CustomAttribute(Blah = "blah1")] string actualParam,
[CustomAttribute(Blah = "blah2")] DateTime anotherParam
)
{
// method's body
}
Run Code Online (Sandbox Code Playgroud)
或者我完全错过了什么?
这就像你说的那样.对于参数,您将执行以下操作:
void SomeMethod([MarshalAs(UnmanagedType.FunctionPtr)] Action del);
Run Code Online (Sandbox Code Playgroud)
对于返回值,您可以:
[return: MyAttribute(Param1 = "Value1")]
int Method()
Run Code Online (Sandbox Code Playgroud)
同样,也有[module: MyAttribute],[assembly: MyAttribute]模块和组件.