是否有可能与变量/动态组字段的声明在C#匿名类型? [英] Is it possible to declare an anonymous type in C# with a variable/dynamic set of fields?

查看:481
本文介绍了是否有可能与变量/动态组字段的声明在C#匿名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我想弄清楚它是否可以声明匿名类型,其中字段是不知道,直到运行时。

In C#, I would like to figure out if it's possible to declare an anonymous type where the fields are not known until run-time.

例如,如果我有键/值对的列表,我可以声明基于列表的内容匿名类型?具体的情况下,我用的参数传递到小巧精致的,在这里我不知道提前有多少参数将有工作。

For example, if I have a List of key/value pairs, can I declare an anonymous type based on the contents of that list? The specific case I'm working with is passing parameters to Dapper, where I don't know ahead of time how many parameters I will have.

List<Tuple<string, string>> paramList = new List<Tuple<string, string>>() {
    new Tuple<string, string>("key1", "value1"),
    new Tuple<string, string>("key2", "value2")
    ...
};



我想这个列表(或等效图)转换成一个匿名类型,我可以传递给小巧玲珑的查询参数。所以,理想情况下,上面的列表也将结束这样看,如果定义为一个匿名类型:

I'd like to convert this List (or an equivalent Map) into an anonymous type that I can pass to Dapper as query parameters. So ideally, the above list would wind up looking like this, if defined as an anonymous type:

new { key1=value1, key2=value2, ... }

我见过的StackOverflow上几个问题问的延长的匿名类型后,他们声明(extendo对象),或者它的创建后,在对象上声明任意字段,但我并不需要做...我只需要申报的各类动态起来 - 前一次。我的猜测是,这将需要一些花哨的反映,如果有可能的。

I've seen several questions on StackOverflow asking about extending anonymous types after they are declared ("extendo objects"), or declaring arbitrary fields on an object after it's created, but I don't need to do that... I just need to declare the types dynamically up-front once. My suspicion is that it will require some fancy reflection, if it's possible at all.

我的理解是,编译器在编译期引擎盖下的定义为匿名类类型时间,所以如果类的字段将不可用,直到运行时,我可能是出于运气。我的用例实际上可能实际上不是使用extendo对象来定义任意的领域,只要没有什么不同。

My understanding is that the compiler defines a type for anonymous classes under the hood at compile-time, so if the fields of that class are not available until run-time, I might be out of luck. My use case may in fact be no different in actuality than using an "extendo object" to define arbitrary fields, whenever.

另外,如果任何人有更好的方式来知道通过查询参数短小精悍(而非声明一个匿名类),我很想听到这一点。

Alternatively, if anyone knows of a better way to pass query parameters to Dapper (rather than declaring an anonymous class), I would love to hear about that as well.

谢谢!

更新

对不起,在又回到了这个延迟!这些答案都是伟大的,我希望我可以给大家给点意见。最后我用jbtule的解决方案(由萨姆藏红花编辑),传递给IDynamicParameters短小精悍,所以我觉得我必须给出答案给他。其他的答案也不错,回答说,我问的具体问题。我真的很感激大家的时间对这个!

Sorry for the delay in getting back to this one! These answers were all great, I wish I could give points to everyone. I ended up using jbtule's solution (with edit by Sam Saffron), passing IDynamicParameters to Dapper, so I felt I had to give the answer to him. The other answers were also good, and answered specific questions that I had asked. I really appreciate everyone's time on this!

推荐答案

精致小巧的创作者非常清楚这个问题。是真正需要的插入更新助手这样的功能。

Dapper's creators were very aware of this problem. This kind of functionality is really needed for INSERT and UPDATE helpers.

查询执行 QueryMultiple 方法采取在动态参数。这可以是匿名类型,具体类型或对象实现 IDynamicParameters

The Query, Execute and QueryMultiple methods take in a dynamic parameter. This can either be an anonymous type, a concrete type or an object that implements IDynamicParameters.

public interface IDynamicParameters
{
    void AddParameters(IDbCommand command, Identity identity);
}

这界面是非常方便的, AddParameters 只是运行任何SQL之前被调用。这不仅可以让您对发送到SQL参数丰富的控制。它可以让你挂接数据库特定DbParameters,因为你必须访问命令(你可以将它转换为数据库特定的一个)。这允许支持表的值的参数等。

This interface is very handy, AddParameters is called just before running any SQL. Not only does this give you rich control over the parameters sent to SQL. It allows you to hook up DB specific DbParameters, since you have access to the command (you can cast it to the db specific one). This allows for support of Table Values Parameters and so on.

小巧玲珑包含此接口,可以用来为您的目的名为 DynamicParameters 的实现。这允许您同时连接在一起匿名参数袋,并添加特定的值。

Dapper contains an implementation of this interface that can be used for your purposes called DynamicParameters. This allows you to both concatenated anonymous parameter bags and add specific values.

您可以使用方法的 AddDynamicParams 来追加匿名类型。

You can use the method AddDynamicParams to append an anonymous type.

var p = new DynamicParameters();
p.AddDynamicParams(new{a = "1"});
p.AddDynamicParams(new{b = "2", c = "3"});
p.Add("d", "4")
var r = cnn.Query("select @a a, @b b, @c c, @d d", p);
// r.a == 1, r.b == 2, r.c == 3, r.d == 4

这篇关于是否有可能与变量/动态组字段的声明在C#匿名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆