如何编写一个无需命名即可访问属性的类
我有一个关于 VB/C# 的(转储)问题,
我经常使用第三方类,在其中我只需指定 id 或键即可访问子对象。
示例:
而不是写:
DataRow row = GetAPopulatedDataRowSomeWhere();
Object result = row.Items[1]; // DataRow has no Items property
Object result = row.Items["colName"]; // Also not possible
我使用此代码来访问成员:
DataRow row = GetAPopulatedDataRowSomeWhere();
Object result = row[1];
Object result = row["colName"];
有人可以告诉我一个类必须是什么样子才能支持此语法吗? 我自己的班级有一个字典,我想通过这种方式访问。
MyClass["key"]; // <- that's what I want
MyClass.SubItems["key"]; // <- that's how I use it now
I have a (dump) question regarding VB/C#
I often use third party classes where I can access a child object with only specifying the id or key.
Example:
Instead of writing:
DataRow row = GetAPopulatedDataRowSomeWhere();
Object result = row.Items[1]; // DataRow has no Items property
Object result = row.Items["colName"]; // Also not possible
I use this code to access the members:
DataRow row = GetAPopulatedDataRowSomeWhere();
Object result = row[1];
Object result = row["colName"];
Can someone tell me how a class has to look like to support this syntax?
My own class has a Dictionary that I want to access this way.
MyClass["key"]; // <- that's what I want
MyClass.SubItems["key"]; // <- that's how I use it now
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要有一个索引属性。
You need to have an indexed property.