[英]Loop over (optional) method parameters
I'm writing a simple library, which provides access to a REST Webservice which has multiple optional parameters. 我正在编写一个简单的库,该库提供对具有多个可选参数的REST Web服务的访问。
Sample URL:
http://localhost/doSomething?a=a&b=b&c=c
So my starting point is a method with optional parameters. 所以我的出发点是带有可选参数的方法。 Something like this:
像这样:
public byte[] DoSomething(string a = null, string b = null, string c = null)
{
string query = "doSomething";
//Get Parameters
if (a != null)
{
//Handle first ?
query = String.Format("{0}&{1}={2}", query, "a", a);
}
[...]
}
You can imagine, this leads to a long method, IF you have a lot of parameters. 您可以想像,如果您有很多参数,这将导致很长的方法。 One way to reduce the code size would be to add every parameter to a collection:
减少代码大小的一种方法是将每个参数添加到集合中:
public byte[] DoSomething(string a = null, string b = null, string c = null)
{
string query = "doSomething";
var parameters = new Dictionary<string, string> {{"a", a}, {"b", b}, {"c", c}};
foreach (var parameter in parameters)
{
if (!String.IsNullOrEmpty(parameterPair.Value))
{
//Handle first ?
query = String.Format("{0}&{1}={2}", query, parameter.Key, parameter.Value);
}
}
[...]
}
This is a bit more suiteable, but I'm curious if there's a better method to solve such a problem without creating long if statements or manually creating a collection. 这有点适合,但我很好奇是否有更好的方法可以解决此类问题,而无需创建长if语句或手动创建集合。
This is an option: 这是一个选择:
public byte[] DoSomething(Tuple<string,string>[] kvp)
{
...
}
One way is to use an anonymous type and reflection, like this: 一种方法是使用匿名类型和反射,如下所示:
public byte[] DoSomething(string a = null, string b = null, string c = null)
{
var p = new { a, b, c };
var parts = from property in p.GetType().GetProperties()
let value = property.GetValue(p) as string
where !string.IsNullOrEmpty(value)
select string.Format("{0}={1}", property.Name, value);
var query = "?" + string.Join("&", parts);
[...]
}
Here's an example using params
: 这是一个使用
params
的示例:
byte[] DoSomething(params KeyValuePair<string, string>[] parameters)
{
var builder = new StringBuilder();
for (int i = 0; i < parameters.Length; i++)
{
builder.AppendFormat("{0}={1}", parameters[i].Key, parameters[i].Value);
if (i != parameters.Length - 1)
{
builder.Append("&");
}
}
string urlParams = builder.ToString(); // contains "param1=value1¶m2=value2"
...
}
And here's how to use it: 以及使用方法:
DoSomething(new[] {
new KeyValuePair<string, string>("param1", "value1"),
new KeyValuePair<string, string>("param2", "value2"),
});
Based on the answers of T McKeown, Nasreddine and John Gibb I came up with a "ParameterModel". 根据T McKeown,Nasreddine和John Gibb的回答,我提出了“ ParameterModel”。 There are WebService Methods, which have basicly the same parameters + some additional ones, so I came up this way to use inheritance.
有WebService方法,它们具有基本相同的参数+一些其他参数,因此我提出了使用继承的方法。
public class BaseABCModel
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public BaseABCModel(string a = null, string b = null, string c = null)
{
A = a;
B = b;
C = c;
}
public Dictionary<string, string> GetParameters()
{
return GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.ToDictionary(propertyInfo => propertyInfo.Name, propertyInfo =>
(String) propertyInfo.GetValue(this));
}
}
//Methods:
public byte[] DoSomething(BaseABCModel model)
{
string query = GetQuery("doSomething", model.GetParameters())
}
public string GetQuery(string methodName, Dictionary<string, string> parameters)
{
string parameterString = parameters.Where(parameter => !String.IsNullOrEmpty(parameter.Value))
.Aggregate(String.Empty, (current, parameter) => String.Format(
String.IsNullOrEmpty(current) ? "{0}?{1}={2}" : "{0}&{1}={2}",
current, parameter.Key, parameter.Value));
return methodName + parameterString;
}
Here is what I came up with. 这是我想出的。 I was messing around with the idea of not doing the a = null, b = null params.
我正在搞乱不执行a = null,b = null参数的想法。
public byte[] mainoutput(string text)
{
byte[] retval = null;
char[] delimeterChars = { '?' };
string[] newparsm = text.Split(delimeterChars);
string query = "";
int count = 0;
foreach (string s in newparsm)
{
count += 1;
if (s.Length > 2)
{
if (count == 1)
{
query = query + "?" + s;
}
else
{
query = query + "&" + s;
}
}
}
Console.WriteLine(query);
return retval;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.