我已经阅读了有关它的 MSDN 文章。它在内部似乎是 c# 设置的方式,它是将作为索引器工作的函数(我说的对吗?)。现在,我看到了以下示例:
[DefaultMemberAttribute("Main")]
public class Program {
public static void Main() {
...
}
}
现在,我不明白这是什么意思。
谢谢大家。但是除了索引器之外,我仍然无法获得它的用处。我们什么时候调用 InvokeMember?
最佳答案
不,DefaultMemberAttribute
由 VB.NET 等语言使用,如果没有从对象引用成员,则找出默认作用的成员,即由 InvokeMember
调用的成员.正如您所指出的,这通常与索引器结合使用,但 C# 不直接使用它(除非您明确使用 InvokeMember
)。
但是,为了其他 .NET 语言的利益,C# 确实会发出 DefaultMemberAttribute
对于一个类的索引器(如果它有一个),如 MSDN 所示:
The C# compiler emits the DefaultMemberAttribute on any type containing an indexer. In C# it is an error to manually attribute a type with the DefaultMemberAttribute if the type also declares an indexer.
我认为 MSDN 在评论中大量提及索引器,但随后给出了一个不使用索引器的示例,从而混淆了事情。澄清一下,默认成员可以是任何东西,但 C# 通过为您发出属性(如果存在索引器)为索引器提供特殊行为,但所有其他用例除外。
关于c# - DefaultMemberAttribute - 它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1119814/