开发者

Named parameters confusion

开发者 https://www.devze.com 2023-03-25 01:04 出处:网络
In the following cod开发者_StackOverflow中文版e: (WebMethod attribute in web service) [WebMethod(CacheDuration = 300)]

In the following cod开发者_StackOverflow中文版e: (WebMethod attribute in web service)

[WebMethod(CacheDuration = 300)]
public string GetData(string Id)
{

}

The intellisense for the WebMethod attribute class constructor shows the second overload as named parameters. My doubt is that aren't named parameter values specified using a colon (:) after the parameter name. How come an equal to operator is used here ?

Thanks.


You're using a third syntax particular to attributes, in which you can specify fields to initialize on the attribute instance using a Name = Value syntax in the constructor call.

[WebMethod(cacheDuration: 300)]

would work just as well, but it's using standard named-parameter syntax instead of attribute field-initialization syntax. Remember that attributes were around before named parameters existed.


I believe it has to do with WebMethod's properties.

Look at this VALID code:

class TestAttribute : Attribute
{
    public int MyProperty { get; set; }
}
class Program
{
    [Test(MyProperty=300)]
    public void method1()
    {
    }
}

If you look at the Metadata for WebMethod, there is a property called "CacheDuration".

0

精彩评论

暂无评论...
验证码 换一张
取 消