是否可以重载 C# 动态类型属性解析?
当类型转换为动态时是否可以重载类型解析属性的方式?
基本上我想做的是类似 ExpandoObject
的东西,您可以在其中动态访问新属性,但这些属性不是分配给它们的任何属性,我希望它们全部都是字符串的集合,当您访问一个词典时,它会调用一个词典 Web 服务,该服务会返回该单词的所有定义。我希望它是动态的,以便您可以指定任何单词。
我想用法会是这样的:
dynamic dictionary = new DictionaryWebservice();
var awesome = dictionary.awesome;
foreach(var definition in awesome)
{
// add definition to my output
}
这可能吗?
Is it possible to overload how a type resolves properties when cast to dynamic?
Basically what I am trying to do is something like ExpandoObject
where you can access new properties on the fly, but instead of those properties being whatever gets assigned to them, I want them all to be collections of strings, and when you access one, it calls a dictionary web service which returns all of the definitions of the word. I want it to be dynamic so that you can specify any word at all.
I'm thinking the usage would be something like this:
dynamic dictionary = new DictionaryWebservice();
var awesome = dictionary.awesome;
foreach(var definition in awesome)
{
// add definition to my output
}
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,从 System.Dynamic.DynamicObject。
但请记住,从动态方法返回的所有内容本身都是
动态
。因此,即使foreach
中的var 定义
也将是dynamic
类型。Yes, derive your type from System.Dynamic.DynamicObject.
Remember though that everything you return from a dynamic method is itself
dynamic
. Therefore, even thevar definition
in yourforeach
will be of typedynamic
.