C# 参数中的键值对
我正在寻找一种方法来实现这样的功能:
myFunction({"Key", value}, {"Key2", value});
我确信有一些带有匿名类型的东西会非常简单,但我没有看到它。
我能想到的唯一解决方案是使用 params KeyValuePair
myFunction(new KeyValuePair<String, object>("Key", value),
new KeyValuePair<String, object>("Key2", value));
不可否认,这要丑陋得多。
编辑:
为了澄清,我正在编写一个 Message
类来在两个不同的系统之间传递。它包含一个指定消息类型的ushort
,以及一个用于与消息关联的“数据”对象的字符串字典。我希望能够在构造函数中传递所有这些信息,因此我能够执行以下操作:
Agent.SendMessage(new Message(MessageTypes.SomethingHappened, "A", x, "B", y, "C", z));
或类似的语法。
I'm looking for a way to have a function such as:
myFunction({"Key", value}, {"Key2", value});
I'm sure there's something with anonymous types that would be pretty easy, but I'm not seeing it.
The only solution I can think of is to have a params KeyValuePair<String, object>[] pairs
parameter, but that ends up being something similar to:
myFunction(new KeyValuePair<String, object>("Key", value),
new KeyValuePair<String, object>("Key2", value));
Which is, admittedly, much uglier.
EDIT:
To clarify, I'm writing a Message
class to pass between 2 different systems. It contains a ushort
specifying the the Message Type, and a dictionary of string to object for "Data" associated with the message. I'd like to be able to pass all this information in the constructor, so I am able to do this:
Agent.SendMessage(new Message(MessageTypes.SomethingHappened, "A", x, "B", y, "C", z));
or similar syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
当语法对于原本不错的模式来说很糟糕时,请更改语法。怎么样:
用法:
更有趣的是向
string
添加扩展方法以使其可配对:用法:
编辑:您还可以使用字典语法而不使用泛型括号由
Dictionary<,>
派生而来:用法:
When the syntax is bad for an otherwise decent pattern, change the syntax. How about:
Usage:
Even more interesting would be to add an extension method to
string
to make it pairable:Usage:
Edit: You can also use the dictionary syntax without the generic brackets by deriving from
Dictionary<,>
:Usage:
从 C# 7.0 开始,您可以使用值元组。 C# 7.0 不仅引入了新类型,还引入了元组类型和元组值的简化语法。元组类型简单地写为用大括号括起来的类型列表:
相应的元素名为
Item1
、Item2
、Item3
。您还可以指定可选的别名。这些别名只是语法糖(C# 编译器的一个技巧);元组仍然基于不变式(但通用)System.ValueTuple
结构。元组值具有类似的语法,只不过您指定表达式而不是类型
或使用别名
使用
params
数组的示例:也可以像这样解构元组
您可以解构 foreach 中的循环变量:
这会在两个单独的变量
key
和value
中提取元组的项目。现在,您可以使用不同数量的参数轻松调用
MyFunction
:它允许我们做类似的事情,
请参阅: C# 7.0 中的新功能
Since C# 7.0, you can use value tuples. C# 7.0 not only introduces new types but a simplified syntax for tuple types and for tuple values. A tuple type is simply written as a list of types surrounded by braces:
The corresponding elements are named
Item1
,Item2
,Item3
. You can also specify optional aliases. These aliases are only syntactic sugar (a trick of the C# compiler); the tuples are still based on the invariant (but generic)System.ValueTuple<T1, T2, ...>
struct.Tuple values have a similar syntax, except that you specify expressions instead of types
or with the aliases
Example with
params
array:It is also possible to deconstruct a tuple like this
You can deconstruct the loop variables in the foreach:
This extracts the items of the tuple in two separate variables
key
andvalue
.Now, you can call
MyFunction
with a varying number of arguments easily:It allows us to do things like
See: New Features in C# 7.0
有趣的是,我刚刚创建了(几分钟前)一个允许使用匿名类型和反射来做到这一点的方法:
Funny, I just created (minutes ago) a method that allows to do that, using anonymous types and reflection :
有点黑客,但您可以让您的
Message
类实现IEnumerable
接口并为其提供一个Add
方法。然后,您将能够使用集合初始值设定项语法:A bit of a hack, but you could have your
Message
class implement theIEnumerable
interface and give it anAdd
method. You'll then be able to use collection initializer syntax:使用字典:
这很简单,您只需要一个
新字典
,而不是每个参数。获取键和值很简单。或者使用匿名类型:
这在函数内部使用不太好,您需要反射。这看起来像这样:(
直接从我的脑海中,可能有一些错误......)
Using a dictionary:
Which is straight forward, you need only one
new Dictionary<K, V>
, not for each argument. It's trivial to get the keys and values.Or with an anonymous type:
Which is not very nice to use inside the function, you'll need reflection. This would look something like this:
(Staight from my head, probably some errors...)
使用 字典 ...
Use a Dictionary ...
更多相同内容:
可以在此处找到有关初始化字典的一些详细信息。
Here's more of the same:
Some details on initialising a dictionary can be found here.
对于 C# 4.0 中的动态类型:
调用使用:
With dynamic type in C# 4.0:
Call using:
您还可以引用 nugetpackage“valuetuple”,它允许您执行以下操作:
然后您可以像这样调用该方法:
You could also reference the nugetpackage "valuetuple", which allows you to do the following:
You can then call the method like this:
您可以这样做:
其中 DateField 和 IdField 应该是“字符串”标识符。
TestNameMethod:
性能比使用字典快 5%。缺点:不能使用变量作为键。
You can do that:
where DateField and IdField are supposed to be a 'string' identifiers.
The TestNameMethod:
Performance is 5% faster than using Dictionary. Disadvantage: you can't use variable as a key.
您可以使用元组来实现类似于 @Bryan Watts 的
Pairing.Of
的功能,而无需额外的类:You could use Tuples to achieve something similar to @Bryan Watts's
Pairing.Of
without the extra class:所以我是新人,目前无法添加评论,但这只是一个建议,通过使其通用化来改进 @Bryan Watts 对 Pairing.of 类的想法,使其易于被使用其他课程。
So I'm new and can't currently add comments, but this is just a suggestion to improve @Bryan Watts's idea of the
Pairing.of
class by making it generic, allowing it to be easily used by other classes.