2010-04-27 60 views
5

如何使用c#属性执行以下操作。以下是在构造函数中注释参数的Java代码片段。c#中的参数属性

public class Factory { 
    private final String name; 
    private final String value; 
    public Factory(@Inject("name") String name, @Inject("value") String value) { 
     this.name = name; 
     this.value = value; 
    } 
} 

从看看c#注释它看起来不像我可以注释参数。这可能吗?

回答

13

您可以绝对属性参数:

public Factory([Inject("name")] String name, [Inject("value")] String value) 

当然的属性必须被声明为许可证它的参数通过AttributeUsageAttribute(AttributeTargets.Parameter)指定。

参见OutAttributeDefaultParameterValueAttribute作为示例。

+0

我只是打字了这一点!你对我来说太快了= D – Tejs 2010-04-27 12:44:21

1

使用AttributeUsageAttribute和它们创建一个属性类,使用Reflection来检查参数。

[System.AttributeUsage(System.AttributeTargets.All)] 
class NewAttribute : System.Attribute { } 

Accessing Attributes with Reflection