向 ExpandoObjects 添加方法

发布于 2024-09-19 04:00:32 字数 460 浏览 5 评论 0原文

更新

问题不在于代码,问题在于您显然无法从立即窗口评估动态对象。


我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-09-26 04:00:32

您确定您包含了所有代码吗?

我刚刚测试并运行了以下命令并成功:

dynamic obj = new ExpandoObject();

obj.First = "Hello";
obj.Last = "World!";

obj.AsString = new Func<string>(() => obj.First + " " + obj.Last);

// Displays "Hello World!"
Console.WriteLine(obj.AsString());

Are you sure you included all the code?

I just tested and ran the following and was successful:

dynamic obj = new ExpandoObject();

obj.First = "Hello";
obj.Last = "World!";

obj.AsString = new Func<string>(() => obj.First + " " + obj.Last);

// Displays "Hello World!"
Console.WriteLine(obj.AsString());
不知所踪 2024-09-26 04:00:32

编译器会抱怨

myObj.AsString; // only assignment, call, increment, decrement, and new object expressions can be used as a statement

所以去掉它。当然,还要删除您所说的无法编译的代码行。但是,一旦修复了这些位,其余代码就应该可以工作。示例(加上添加另一个“方法”):

dynamic myObj = new ExpandoObject();
myObj.First = "Stack";
myObj.Last = "Overflow";

Action<int> PrintInt = input => Console.WriteLine(input.ToString());
myObj.PrintInt = PrintInt;
myObj.PrintInt(1);

myObj.AsString = new Func<string>(() => myObj.First + " " + myObj.Last);
string s = myObj.AsString();
Console.WriteLine(s);

The compiler will complain about

myObj.AsString; // only assignment, call, increment, decrement, and new object expressions can be used as a statement

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"):

dynamic myObj = new ExpandoObject();
myObj.First = "Stack";
myObj.Last = "Overflow";

Action<int> PrintInt = input => Console.WriteLine(input.ToString());
myObj.PrintInt = PrintInt;
myObj.PrintInt(1);

myObj.AsString = new Func<string>(() => myObj.First + " " + myObj.Last);
string s = myObj.AsString();
Console.WriteLine(s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文