简体   繁体   English

DefaultMemberAttribute - 它做什么?

[英]DefaultMemberAttribute - what does it do?

I've already read the MSDN article about it. 我已经阅读过有关它的MSDN文章。 It seems internally it is the way c# sets which is the function that is going to work as indexer(am I right?). 在内部看来,它是c#设置的方式,它将作为索引器工作(我是对的吗?)。 Now, I've seen the following example: 现在,我已经看到以下示例:

[DefaultMemberAttribute("Main")]
public class Program {

    public static void Main() {
        ...
    }
}

Now, I don't get it what it means. 现在,我不明白这意味着什么。


Thanks all. 谢谢大家。 But I still can't get its usefulness, apart from the indexer thing. 但除了索引器之外,我仍然无法获得它的用处。 When are we going to call InvokeMember? 我们什么时候打电话给InvokeMember?

No, the DefaultMemberAttribute is used by languages such as VB.NET to find out the member that is acted on by default if no member is referenced from an object, ie the member invoked by InvokeMember . 不,如果没有从对象引用成员,即InvokeMember调用的成员,则VB.NET等语言使用DefaultMemberAttribute查找默认情况下作用的成员。 This is often used in conjunction with indexers, as you noted, but it is not used by C# directly (unless you use InvokeMember explicitly). 正如您所指出的,这通常与索引器一起使用,但C#不直接使用它(除非您明确使用InvokeMember )。

However, for the benefit of other .NET languages, C# does emit the DefaultMemberAttribute for the indexer of a class (if it has one), as indicated by MSDN : 但是,为了其他.NET语言的优势,C#会为类的索引器(如果有的话)发出DefaultMemberAttribute ,如MSDN所示

The C# compiler emits the DefaultMemberAttribute on any type containing an indexer. C#编译器在包含索引器的任何类型上发出DefaultMemberAttribute。 In C# it is an error to manually attribute a type with the DefaultMemberAttribute if the type also declares an indexer. 在C#中,如果类型也声明了索引器,则使用DefaultMemberAttribute手动归属类型是错误的。

I think MSDN confuses things by referring to indexers a lot in the remarks but then giving an example that does not use an indexer. 我认为MSDN在评论中通过引用索引器来混淆事物,但后来给出了一个不使用索引器的示例。 To clarify, the default member can be anything, but C# gives special behavior for indexers by emitting the attribute for you (if an indexer exists) to the exception of all other use cases. 为了澄清,默认成员可以是任何内容,但C#通过向您发送属性(如果存在索引器)为所有其他用例例外,为索引器提供特殊行为。

I personally have never used it, but as far as I can tell you are defining the default method to be invoked when calling InvokeMember . 我个人从来没有使用它,但据我所知,你正在定义调用InvokeMember时要调用的默认方法。 So, using the code snippet you provided if I was to say: 所以,如果我要说的话,请使用您提供的代码段:

Program prog = new Program();
typeof(Program).InvokeMember("", null, null, prog, null);

Because I left the first argument empty of the InvokeMember call it would use the attribute to determine what the default member is of your class, in your case it is Main. 因为我将第一个参数留空了InvokeMember调用,所以它将使用该属性来确定您的类的默认成员,在您的情况下它是Main。

The DefaultMemberAttribute attribute defines the default member to be called on a when InvokeMember is called with an empty string as the first argument. DefaultMemberAttribute属性定义在调用InvokeMember时调用的默认成员,其中空字符串作为第一个参数。

If you read the MSDN docs for InvokeMember , it explicitly says: 如果您阅读InvokeMember的MSDN文档,它会明确说:

Parameters 参数
name 名称
Type: System.String 键入:System.String
The String containing the name of the constructor, method, property, or field member to invoke. String,包含要调用的构造函数,方法,属性或字段成员的名称。
-or- -要么-
An empty string ("") to invoke the default member. 用于调用默认成员的空字符串(“”)。

The default member will be the one declared by the DefaultMemberAttribute attribute. 默认成员将是DefaultMemberAttribute属性声明的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM