动态对象.NET

发布于 2024-10-09 02:49:58 字数 395 浏览 2 评论 0原文

我现在面临一个有趣的问题。 我知道这并不那么漂亮,但无论如何我都必须应对它。

我正在调用一个返回(字符串数组)类似内容的网络服务(按索引)
0 - 表名称(“员工”)
1 - 返回的所有字段的列表(“ID、姓名、出生日期”)
2 - 第一个字段值(“1”)
3 - 第二个字段值(“Bobby”)
4 - 第三个字段值(“1970-01-01”)

我想要完成的是在索引 0 之后创建一个对象名称,让索引 1 中的所有字符串都是所有属性,并为其提供正确的值。

实现这一目标的最佳方法是什么?我已经开始阅读有关 ExpandoObject 的内容,但在深入之前我想听听您对此的看法。

问候

编辑:我将有很多不同的表来从中获取数据,这就是导致我出现问题的原因。

I am facing an interesting problem right now.
I know it is not that pretty but I have to cope with it anyway.

I am calling a webservice that returns (array of string) something like that (by index)
0 - Table Name ("Employees")
1 - List of all fields returned ("Id, Name, Birthdate")
2 - First field value ("1")
3 - Second field value ("Bobby")
4 - Third field value ("1970-01-01")

What I would like to accomplish is to create a object name after the index 0, having all the strings in the index 1 being all the properties and feed them with their proper value.

What would be the best way to accomplish this? I have started reading about the ExpandoObject but before going too far I would like to hear your take on this.

Regards

EDIT: I will have quite a few different tables to fetch the data from, that is what is causing me the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

森末i 2024-10-16 02:49:58

这取决于您的用例,但我怀疑 dynamic 是否适合这里。我只会有一个 name-value 集合,如下所示:

class Result {
    Dictionary<string, string> values = new Dictionary<string, string>();
    public string this[string name] {
        get {
            return this.values[name];
        }
        set {
            this.values[name] = value;
        }
    }

    // some details
}

然后您可以从 Web 服务填充值并通过说等查询值

Result result = PopulateResultFromWebservice();
string id = result["Id"];

。如果您知道类型,您可以创建 object 而是将值解析为适当的类型并将它们存储为这些类型的对象。

dynamic 的问题是,如果字段的名称未知,您将如何编写利用这些字段的代码?

编辑:我将有很多不同的表来从中获取数据,这就是导致我出现问题的原因。

老兄,每天都在名值收藏。

It depends on your use-case but I doubt that dynamic is the way to go here. I would just have a name-value collection like so:

class Result {
    Dictionary<string, string> values = new Dictionary<string, string>();
    public string this[string name] {
        get {
            return this.values[name];
        }
        set {
            this.values[name] = value;
        }
    }

    // some details
}

Then you can populate the values from the webservice and query the values by saying

Result result = PopulateResultFromWebservice();
string id = result["Id"];

etc. If you knew the types you could make this indexer of type object instead and parse the values to their appropriate type and store them as those typed objects.

The issue with dynamic is that if the names of the fields are an unknown, how are you possibly going to write code that utilizes those fields?

EDIT: I will have quite a few different tables to fetch the data from, that is what is causing me the problem.

Dude, name-value collection all day every day.

彡翼 2024-10-16 02:49:58

如果您总是接收具有不可预测字段集的不同类型的对象,那么使用 ExpandoObject 是一个好主意(请查看 Clay在本例中为 库)。在其他情况下,您只需要创建一些反映对象结构的类型,然后具体化它们。

显然,您可以只使用一个简单的字典,但使用动态对象可以为您提供更多功能,您可以决定。真实的使用案例会让我给你更好的帮助。

If you recive always different kinds of objects with non-predictable set of fields that using ExpandoObject is a good idea (Take a look on Clay library in this case). In other case you just need to create some types that reflects the structure of your object and then materialize them.

Obviously you can use just a simple dictionary, but Using Dynamic object can give you more power, you decide. Real usage case will allow me to give you a better help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文