【发布时间】:2016-06-19 12:37:52
【问题描述】:
所以我有两个 if 语句来处理字典值。如果方法是 POST,我只需将 status 参数添加到字典中。所有其他参数都相同。
var stringBuilder = new StringBuilder();
var dictionary = new SortedDictionary<string, string>();
if (method == "GET")
{
stringBuilder.Append("GET&");
stringBuilder.Append(Uri.EscapeDataString(url));
stringBuilder.Append("&");
dictionary = new SortedDictionary<string, string>
{
{ "oauth_version" , oauthVersion },
{ "oauth_consumer_key", consumerKey },
{ "oauth_nonce" , oauthNonce },
{ "oauth_signature_method" , oauthSignatureMethod },
{ "oauth_timestamp" , oauthTimeStamp },
{ "oauth_token" , accessToken },
};
}
if (method == "POST")
{
stringBuilder.AppendFormat("POST&{0}&", Uri.EscapeDataString(url));
dictionary = new SortedDictionary<string, string>
{
{ "oauth_version" , oauthVersion },
{ "oauth_consumer_key", consumerKey },
{ "oauth_nonce" , oauthNonce },
{ "oauth_signature_method" , oauthSignatureMethod },
{ "oauth_timestamp" , oauthTimeStamp },
{ "oauth_token" , accessToken },
{ "status" , status }
};
}
无论如何要避免在我的代码中重复。任何建议都会很方便。
请注意。
【问题讨论】:
-
“重复”是什么意思?就字典的键而言,不能有任何重复项。我觉得你的问题很难理解,你不想复制什么?
-
@VisualVincent 我的意思是如果方法是 POST 我只需要添加状态参数。get 和方法中的所有参数都是相同的。
标签: c# asp.net asp.net-mvc code-duplication