向 ExpandoObjects 添加方法
更新
问题不在于代码,问题在于您显然无法从立即窗口评估动态对象。
我正在尝试为 ExpandoObject 添加方法,但不确定如何让它工作。这是我的代码:
dynamic myObj = new ExpandoObject();
myObj.First = "Micah";
myObj.Last = "Martin";
myObj.AsString = new Func<string>(() => myObj.First + " " + myObj.Last);
//No matter what I do I get 'object' does not contain a definition for 'AsString'
myObj.AsString;
myObj.AsString();
myObj.AsString.Invoke();
有人知道该怎么做吗?
UPDATE
The problem is not the code, the problem is that you apparently can't evaluate dynamic objects from the immediate window.
I'm trying to tack on methods to an ExpandoObject but not sure how to get it to work. Here's my code:
dynamic myObj = new ExpandoObject();
myObj.First = "Micah";
myObj.Last = "Martin";
myObj.AsString = new Func<string>(() => myObj.First + " " + myObj.Last);
//No matter what I do I get 'object' does not contain a definition for 'AsString'
myObj.AsString;
myObj.AsString();
myObj.AsString.Invoke();
Anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定您包含了所有代码吗?
我刚刚测试并运行了以下命令并成功:
Are you sure you included all the code?
I just tested and ran the following and was successful:
编译器会抱怨
所以去掉它。当然,还要删除您所说的无法编译的代码行。但是,一旦修复了这些位,其余代码就应该可以工作。示例(加上添加另一个“方法”):
The compiler will complain about
So get rid of it. And of course get rid of the line of code that you say does not compile. However, the rest of your code should work once those bits are fixed. Example (plus adding another "method"):