如何避免在不同的方法中重写相同的代码

时间:2013-06-25 23:57:01

标签: c#

  

旧问题   所以这就是我想要实现的目标......

     

我有一个现有的抽象类..lets将它命名为Class1.cs。它   包含许多方法的定义。所以现在我已经包括在内   一些需要在每一个中实现的新功能   Class1类的方法。所以对于前 -

        public void Method_1(string arg1, string arg2)
        {
           /*
           //some code implementation specific to Method_1
           */
           Dictionary<string, object> dict= new Dictionary<string, object>();
           //there can be more or less arguments in other methods
           dict.Add("Arg1", arg1);
           dict.Add("Arg2", arg2);
           Method_2(dict);
        }
     

我必须在所有方法中做同样的事情,但是参数   可以变化。所以字典对象可以有“n”个参数。在那儿   我可以避免添加相同代码的手工劳动   反复(如果可能的话,可以使用设计模式)

     

我想我不清楚......捆绑字典一代   机制不是我关心的,我仍然需要添加相同的代码   在所有的方法(约50)..我试图避免手动调用   相同的代码一次又一次50次......

编辑并重新构建问题
我终于决定用私有方法构建字典,并在所有其他方法中调用它。请在本段之前忽略其他所有内容。 我的方法看起来像这样

public void Method1(string a, string b , string c)
{ 
   Dictionary<string,object> dict = BuildDictionary(new {a, b ,c}); 
   /*the dict object should have the following structure
                           key=a,   value=  value of a 
                           key =b , value = value of b
                           key =b , value = value of b*/
}
public void Method2(string x, string y)
{ 
   Dictionary<string,object> dict = BuildDictionary(new {x,y}); 
   /*the dict object should have the following structure
                           key= x,  value=  value of x 
                           key =y , value = value of y */
}
private Dictionary<string,object> BuildDictionary(params object[] values)
{
   //values.ToString() gives  = { a= "Value of a", b== "Vaue of b", c= "Value of c"}
   //Copy pasting Simon's Code here(use of anonymous objects)
   var dictionary = values.GetType()
                       .GetProperties()
                       .ToDictionary(pi => pi.Name, pi => pi.GetValue(values));
   //this dictionary object gives me a count of 7 with keys as the properties of the object datatype(which is not relevant to my case). 
}

那么我需要对BuildDictionary方法进行哪些更改才能获得所需的字典结构?

4 个答案:

答案 0 :(得分:3)

如果没有关于Method_2的性质以及字典中的Keys代表的内容的大量信息,我将建议两个选项。

键始终代表Arg + N

在这种情况下,Method_2的签名可以是params

public void Method_2(params object[] values)
{
    var argNo = 0;
    var dictionary = values.ToDictionary(x => "Arg" + ++argNo);
}

public void Method_1(string arg1, string arg2)
{
    // ...
    Method_2(arg1, arg2);
}

键表示调用者的方法参数名称

执行此操作的一般方法是使用匿名对象。

public void Method_2(object values)
{
    var dictionary = values.GetType()
                           .GetProperties()
                           .ToDictionary(pi => pi.Name, pi => pi.GetValue(values));
}

public void Method_1(string arg1, string arg2)
{
    Method_2(new { arg1, arg2 });
}

编辑:如果Method_2也无法更改,请使用与两个选项之一相同的逻辑构建字典。

回答编辑:您的实现不起作用的原因是因为您获取了对象数组的所有属性而不是(匿名)对象。你没有完全复制我的代码。 params object[] values应为object values

答案 1 :(得分:2)

using System.Linq;

...

// add as many elements as you want
var args = new object[] {arg1, arg2, arg3, arg4};

int i = 0;
var dict = args.ToDictionary(x => "Arg" + ++i, x => x);

Method_2(dict);

这会有效,但我不知道您为什么要将字典传递给Method_2,除非您别无选择。

答案 2 :(得分:0)

您应该使用一个循环,并为您的变量命名方式设置一个模式。在您的方法2中,您应该能够轻松找到这些参数。 下面,我假设您有一个名为args的参数列表,我将它们全部添加到字典中。享受

public void Method_1(string arg1, string arg2)
{
   /*
   //some code implementation specific to Method_1
   */
   var dict = GetArguments(args);
   Method_2(dict);
}

private Dictionary<string, object> GetArguments(List<string> args)
{
    Dictionary<string, object> dict= new Dictionary<string, object>();
   var counter = 1;
   foreach(var argItem in args)
   {
        dict.Add("Arg"+counter++, argItem);
   }
   return dict;
}

答案 3 :(得分:0)

创建一个将构造字典的私有方法。您可以使用简单的循环或者像Matt建议的更简洁的东西。

public void Method_1(string arg1, string arg2)
{
    var dict = BuildDictionary(arg1, arg2);
    Method_2(dict);
}

private Dictionary<string, object> BuildDictionary(params object[] args)
{
    Dictionary<string, object> dict= new Dictionary<string, object>();
    for(int i = 0; i < args.Length; i++)
    {
        dict.Add("Arg" + i, args[i]);
    }
    return dict;
}

使用Matt的版本:

private Dictionary<string, object> BuildDictionary(params object[] args)
{
    return args.ToDictionary(x => "Arg" + ++i, x => x);
}
相关问题