属性使用 MSDN 指南说明

发布于 2024-10-20 04:16:39 字数 862 浏览 3 评论 0原文

我很难从 属性用法中找出此建议的含义指南

不要定义同时具有命名参数和位置参数的参数。下面的代码示例说明了这种模式。

代码是:

public class NameAttribute: Attribute 
{
   string userName;
   int age;

   // This is a positional argument.
   public NameAttribute (string userName) 
   { 
       this.userName = userName;
   }
   public string UserName 
   { 
      get 
      {
         return userName; 
      }
   }
   // This is a named argument.
   public int Age 
   { 
      get 
      {
         return age;
      }
      set 
      {
         age = value;
      }
   } 
}

现在,如果它真的很简单,我很抱歉,我在浪费您的时间。但我只是不明白其含义和示例说明了什么。英语不是我的母语可能是一个因素,但我之前阅读 MSDN 并没有遇到任何困难。我也尝试阅读这篇文章的翻译版本,但它对我来说更没有意义。

因此,如果有人愿意重新措辞并向我解释它,那将非常有帮助。如果您还可以解释为什么 MSDN 建议这样做,我洗耳恭听。

I'm having a hard time trying to figure out the meaning of this advice from Attribute Usage Guildelines:

Do not define a parameter with both named and positional arguments. The following code example illustrates this pattern.

And the code is:

public class NameAttribute: Attribute 
{
   string userName;
   int age;

   // This is a positional argument.
   public NameAttribute (string userName) 
   { 
       this.userName = userName;
   }
   public string UserName 
   { 
      get 
      {
         return userName; 
      }
   }
   // This is a named argument.
   public int Age 
   { 
      get 
      {
         return age;
      }
      set 
      {
         age = value;
      }
   } 
}

Now, I'm sorry if it's really simple and I am wasting your time. But I just don't understand the meaning and what example demonstrates. The fact that English is not my native language might be a factor, but I didn't experience any difficulties reading MSDN before. I also tried to read the translated version of this article, but it makes even less sense to me.

So, if someone would be kind enough to rephrase and explain it to me, that would be really helpful. If you can also explain why MSDN advises to do so, I'm all ears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白衬杉格子梦 2024-10-27 04:16:39

考虑一下如何使用这样的属性:

[Name("Jack", Age = 25)]
public class ClassToDecorate { }

现在,这看起来不太清楚,因为两种样式混合在一起:UserName 是通过属性的构造函数(“位置”)设置的,但是 Age 被显式设置为属性(“named”)。

该指南建议最好重新设计该属性,以便完全以下装饰之一可以工作:(

[Name("Jack", 25)] // Positional only

或)

[Name(UserName = "Jack", Age = 25)] // Named only

Think about how you would have to use such an attribute:

[Name("Jack", Age = 25)]
public class ClassToDecorate { }

Now, this doesn't look all that clear because two styles are being mixed: UserName is being set via the attribute's constructor ("positional"), but Age is being explicitly set as a property ("named").

The guideline is suggesting that it would be better to redesign the attribute such that exactly one of the following decorations would work:

[Name("Jack", 25)] // Positional only

(or)

[Name(UserName = "Jack", Age = 25)] // Named only
煞人兵器 2024-10-27 04:16:39

我认为你是一个糟糕例子的受害者,它实际上并没有说明问题。错误的一个更好的例子是添加这个构造函数:

public NameAttribute (string userName, int age) 
{ 
    this.userName = userName;
    this.Age = age;
}

现在客户端代码可以这样使用属性:

[Name("foo", 42)]

这样:

[Name("foo", Age = 42)]

两者同样有效,客户端程序员将无法弥补他的想法是选择哪一个。一个更糟糕的例子是这个构造函数:

public NameAttribute(int age) {
    this.userName = "unspecified";
    this.Age = age;
}

它允许:

[Name(42, Age = 43)]

I think you are the victim of a crummy example, it doesn't actually demonstrate the problem. A better example of doing it wrong is adding this constructor:

public NameAttribute (string userName, int age) 
{ 
    this.userName = userName;
    this.Age = age;
}

Now the client code could use the attribute this way:

[Name("foo", 42)]

and this way:

[Name("foo", Age = 42)]

Both are equally valid, the client programmer won't be able to make up his mind which one to choose. A much nastier example would be this constructor:

public NameAttribute(int age) {
    this.userName = "unspecified";
    this.Age = age;
}

which allows:

[Name(42, Age = 43)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文