我认为你想是这样的,但因为你没有实际上可以问一个问题,我只是猜测:
request.Split('+').ToList<string>().ForEach(p =>
{
string[] tmp = p.Split('=');
if (tmp.Length == 2 && !string.IsNullOrWhiteSpace(tmp[1]))
{
// edit - if your string can have duplicates, use
// Dictionary<U,K>.ContainsKey(U) to check before adding
var key = tmp[0];
var value = tmp[1];
if(!arguments.ContainsKey(key))
{
arguments.Add(key, value);
}
else
{
//overwrite with new value
//could also maybe throw on duplicate or some other behavior.
arguents[key]=value;
}
}
else
throw InvalidOperationException("Bad dictionary string value");
});
另外,我会质疑,如果使用ToList->的foreach这是在我面前的代码审查。你要避免在LINQ的副作用,我将它与传统的foreach这样写:
var itemValues = request.Split('+');
foreach(var item in itemValues)
{
string[] tmp = item.Split('=');
if (tmp.Length == 2 && !string.IsNullOrWhiteSpace(tmp[1]))
arguments.Add(tmp[0], tmp[1]);
else
throw InvalidOperationException("Bad dictionary string value");
});
// Validate and assign
//read values from the dictionary
//use ContainsKey to check for exist first if needed
Console.WriteLine(arguments["firstname"]); // Displays foo
Console.WriteLine(arguments["lastname"]); // Displays foo
Console.WriteLine(arguments["amout"]); // Displays 100.58
编辑2 - 你应该封装逻辑的方法:
private string TryGetValue(IDictionary<string,string> dict,string key)
{
string value = null;
if(dict.ContainsKey(key) && !string.IsNullOrWhiteSpace(dict[key]))
{
value = dict[key];
}
else
{
Logger.Write("Invalid argument : " + key);
}
return value;
}
现在,你可以说:
string firstName = TryGetValue(arguments,"firstname");
string lastName= TryGetValue(arguments,"lastName");
string amount = TryGetValue(arguments,"amount");
bool isValid = firstName!=null && lastName != null && amount != null;
if(isValid)
{
Console.WriteLine(firstName); // Displays foo
Console.WriteLine(lastName); // Displays bar
Console.WriteLine(amout); // Displays 100.58
}
TryGetValue
会成为一个优秀的扩展方法:
public static class Extensions
{
public static string TryGetValue(this IDictionary<string,string> dict, string key)
{
string value = null;
if(dict.ContainsKey(key) && !string.IsNullOrWhiteSpace(dict[key]))
{
value = dict[key];
}
else
{
Logger.Write("Invalid argument : " + key);
}
return value;
}
}
现在调用代码看起来像:
string firstName = arguments.TryGetValue("firstname");
string lastName= arguments.TryGetValue("lastname");
string amount = arguments.TryGetValue("amount");
最后编辑 - 了一份关于extention方法 - 是的,他们是整齐的,但它也很容易在恶劣的情况下过度使用他们意外地得到。阅读关于他们的msdn和博客,请遵循指导原则。避免像object
,string
等通用类型的扩展。
在我的项目,我总是奠定了在根据他们交往的类型,这迫使那些希望利用它们来为类不同的命名空间的扩展方法很阐明它想:
namespace Extensions.IDictionary { ... }
namespace Extensions.string { ... }
namespace Extensions.SomeType { ... }
namespace Extensions.IList { ... }
和消费码本来using
条款相匹配:
using Extensions.IDictionary;
拉在只是扩展你的兴趣,没了。
HM ..什么是真正的问题? – Tigran 2012-04-19 14:35:39
你想要有效吗? – 2012-04-19 14:36:36
例如,我不知道如何检查字典中是否存在firstname,如果它不是空的,以及如何用变量值填充变量“firstname”。事实上,大约有20个参数,验证每个参数变得非常巨大。 – 2012-04-19 14:42:41