繁体   English   中英

C# 参数中的键值对

[英]Key value pairs in C# Params

我正在寻找一种具有以下功能的方法:

myFunction({"Key", value}, {"Key2", value});

我确信有些匿名类型的东西会很容易,但我没有看到。

我能想到的唯一解决方案是使用params KeyValuePair<String, object>[] pairs参数,但最终结果类似于:

myFunction(new KeyValuePair<String, object>("Key", value),
           new KeyValuePair<String, object>("Key2", value));

不可否认,这要丑得多。

编辑:

澄清一下,我正在编写一个Message类来在 2 个不同的系统之间传递。 它包含一个指定消息类型的ushort和一个字符串字典,用于与消息关联的“数据”。 我希望能够在构造函数中传递所有这些信息,所以我可以这样做:

Agent.SendMessage(new Message(MessageTypes.SomethingHappened, "A", x, "B", y, "C", z));

或类似的语法。

当语法对其他体面的模式不利时,请更改语法。 怎么样:

public void MyFunction(params KeyValuePair<string, object>[] pairs)
{
    // ...
}

public static class Pairing
{
    public static KeyValuePair<string, object> Of(string key, object value)
    {
        return new KeyValuePair<string, object>(key, value);
    }
}

用法:

MyFunction(Pairing.Of("Key1", 5), Pairing.Of("Key2", someObject));

更有趣的是向string添加扩展方法以使其可配对:

public static KeyValuePair<string, object> PairedWith(this string key, object value)
{
    return new KeyValuePair<string, object>(key, value);
}

用法:

MyFunction("Key1".PairedWith(5), "Key2".PairedWith(someObject));

编辑:您还可以通过从Dictionary<,>派生来使用没有通用括号的字典语法:

public void MyFunction(MessageArgs args)
{
    // ...
}

public class MessageArgs : Dictionary<string, object>
{}

用法:

MyFunction(new MessageArgs { { "Key1", 5 }, { "Key2", someObject } });

从 C# 7.0 开始,您可以使用值元组。 C# 7.0 不仅引入了一种新类型,而且还为元组类型和元组值引入了简化的语法。 元组类型可以简单地写成用大括号括起来的类型列表:

(string, int, double)

相应的元素被命名为Item1Item2Item2 您还可以指定可选的别名。 这些别名只是语法糖(C# 编译器的一个技巧); 元组仍然基于不变(但通用) System.ValueTuple<T1, T2, ...> struct

(string name, int count, double magnitude)

元组值具有类似的语法,不同之处在于您指定表达式而不是类型

("test", 7, x + 5.91)

或使用别名

(name: "test", count: 7, magnitude: x + 5.91)

带有params数组的示例:

public static void MyFunction(params (string Key, object Value)[] pairs)
{
    foreach (var pair in pairs) {
        Console.WriteLine($"{pair.Key} = {pair.Value}");
    }
}

也可以像这样解构元组

var (key, value) = pair;
Console.WriteLine($"{key} = {value}");

这将在两个单独的变量keyvalue提取元组的项目。

现在,您可以轻松地使用不同数量的参数调用MyFunction

MyFunction(("a", 1), ("b", 2), ("c", 3));

它允许我们做这样的事情

DrawLine((0, 0), (10, 0), (10, 10), (0, 10), (0, 0));

请参阅: C# 7.0 中的新功能

有趣的是,我刚刚(几分钟前)创建了一个允许这样做的方法,使用匿名类型和反射:

MyMethod(new { Key1 = "value1", Key2 = "value2" });


public void MyMethod(object keyValuePairs)
{
    var dic = DictionaryFromAnonymousObject(keyValuePairs);
    // Do something with the dictionary
}

public static IDictionary<string, string> DictionaryFromAnonymousObject(object o)
{
    IDictionary<string, string> dic = new Dictionary<string, string>();
    var properties = o.GetType().GetProperties();
    foreach (PropertyInfo prop in properties)
    {
        dic.Add(prop.Name, prop.GetValue(o, null) as string);
    }
    return dic;
}

有点小技巧,但您可以让Message类实现IEnumerable接口并给它一个Add方法。 然后,您将能够使用集合初始值设定项语法:

Agent.SendMessage
(
    new Message(MessageTypes.SomethingHappened) {{ "foo", 42 }, { "bar", 123 }}
);

// ...

public class Message : IEnumerable
{
    private Dictionary<string, object> _map = new Dictionary<string, object>();

    public Message(MessageTypes mt)
    {
        // ...
    }

    public void Add(string key, object value)
    {
        _map.Add(key, value);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable)_map).GetEnumerator();
        // or throw a NotImplementedException if you prefer
    }
}

使用字典:

myFunction(new Dictionary<string, object>(){
  {"Key", value}, 
  {"Key2", value}});

这是直接的,您只需要一个new Dictionary<K, V> ,而不是每个参数。 获取键和值很简单。

或者使用匿名类型:

myFunction(new {
  Key = value, 
  Key2 = value});

在函数内部使用不是很好,您需要反射。 这看起来像这样:

foreach (PropertyInfo property in arg.GetType().GetProperties())
{
  key = property.Name;
  value = property.GetValue(arg, null);
}

(直接从我的脑海中,可能有些错误......)

使用字典...

void Main()
{
    var dic = new Dictionary<string, object>();
    dic.Add( "Key1", 1 );
    dic.Add( "Key2", 2 );   

    MyFunction( dic ).Dump();
}

public static object MyFunction( IDictionary dic )
{
   return dic["Key1"];
}

以下是更多相同的内容:

static void Main(string[] args)
{
    // http://msdn.microsoft.com/en-us/library/bb531208.aspx
    MyMethod(new Dictionary<string,string>()
    {
        {"key1","value1"},
        {"key2","value2"}
    });
}

static void MyMethod(Dictionary<string, string> dictionary)
{
    foreach (string key in dictionary.Keys)
    {
        Console.WriteLine("{0} - {1}", key, dictionary[key]);
    }
}

可以在此处找到有关初始化字典的一些详细信息。

在 C# 4.0 中使用动态类型:

public class MyClass
{
    // Could use another generic type if preferred
    private readonly Dictionary<string, dynamic> _dictionary = new Dictionary<string, dynamic>();

    public void MyFunction(params dynamic[] kvps)
    {
        foreach (dynamic kvp in kvps)
            _dictionary.Add(kvp.Key, kvp.Value);
    }
}

调用使用:

MyFunction(new {Key = "Key1", Value = "Value1"}, new {Key = "Key2", Value = "Value2"});

你可以这样做:

TestNamedMethod(DateField => DateTime.Now, IdField => 3);

and are supposed to be a 'string' identifiers.其中应该是“字符串”标识符。

:

public static string TestNameMethod(params Func<object, object>[] args)
{
    var name = (args[0].Method.GetParameters()[0]).Name;
    var val = args[0].Invoke(null);
    var name2 = (args[1].Method.GetParameters()[0]).Name;
    var val2 = args[1].Invoke(null);
    Console.WriteLine("{0} : {1}, {2} : {3}", name, val, name2, val2);
}

性能比使用 Dictionary 快 5%。 缺点:不能使用变量作为键。

您还可以引用 nugetpackage“valuetuple”,它允许您执行以下操作:

public static void MyFunction(params ValueTuple<string, object>[] pairs)
{
    var pair = pairs[1];
    var stringValue = pair.item1;
    var objectValue = pair.item2;
}

然后,您可以像这样调用该方法:

MyFunction(("string",object),("string", object));

您可以使用元组来实现类似于@Bryan Watts 的Pairing.OfPairing.Of而无需额外的类:

public static void MyFunction(params Tuple<string, int>[] pairs)
{
}

MyFunction(Tuple.Create("foo", 1), Tuple.Create("bar", 2));

所以我是新手,目前无法添加评论,但这只是一个建议,通过使其成为通用类来改进 @Bryan Watts 对Pairing.of类的想法,使其可以被其他类轻松使用。

public class Pairing 
{
    public static KeyValuePair<TKey, TValue> of<TKey, TValue>(TKey key, TValue value)
    {
        return new KeyValuePair<TKey, TValue>(key, value);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM