Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何避免用不同的方法重写相同的代码_C# - Fatal编程技术网

C# 如何避免用不同的方法重写相同的代码

C# 如何避免用不同的方法重写相同的代码,c#,C#,老问题 所以这就是我想要实现的 我有一个现有的抽象类..让我们把它命名为Class1.cs。信息技术 包含许多方法的定义。所以现在我包括了 需要在每个应用程序中实现的一些新功能 类的方法。前男友也是如此- public void Method_1(string arg1, string arg2) { /* //some code implementation specific to Method_1

老问题
所以这就是我想要实现的

我有一个现有的抽象类..让我们把它命名为Class1.cs。信息技术 包含许多方法的定义。所以现在我包括了 需要在每个应用程序中实现的一些新功能 类的方法。前男友也是如此-

        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);
        }
public void方法_1(字符串arg1,字符串arg2)
{
/*
//某些特定于方法_1的代码实现
*/
Dictionary dict=新字典();
//在其他方法中可以有更多或更少的参数
dict.Add(“Arg1”,Arg1);
dict.Add(“Arg2”,Arg2);
方法2(dict);
}
除了参数之外,我必须在所有方法中做完全相同的事情 可能会有所不同。因此dictionary对象可以有“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). 
}
public void方法1(字符串a、字符串b、字符串c)
{ 
Dictionary dict=BuildDictionary(新的{a,b,c});
/*dict对象应具有以下结构
键=a,值=a的值
键=b,值=b的值
键=b,值=b的值*/
}
公共无效方法2(字符串x、字符串y)
{ 
Dictionary dict=BuildDictionary(新的{x,y});
/*dict对象应具有以下结构
键=x,值=x的值
键=y,值=y的值*/
}
专用字典BuildDictionary(参数对象[]值)
{
//ToString()给出={a=“a的值”,b==“b的值”,c=“c的值”}
//复制粘贴Simon的代码(使用匿名对象)
var dictionary=values.GetType()
.GetProperties()
.ToDictionary(pi=>pi.Name,pi=>pi.GetValue(值));
//这个dictionary对象为我提供了7个计数,其中键作为对象数据类型的属性(这与我的案例无关)。
}

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

您应该使用一个循环,并有一个命名变量的模式。在method2中,您应该能够轻松找到这些参数。 下面,我假设您有一个名为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;
}
public void方法_1(字符串arg1,字符串arg2)
{
/*
//某些特定于方法_1的代码实现
*/
var dict=GetArguments(args);
方法2(dict);
}
专用字典GetArguments(列表参数)
{
Dictionary dict=新字典();
var计数器=1;
foreach(变量args中的argItem)
{
dict.Add(“Arg”+计数器++,argItem);
}
返回命令;
}

您应该使用一个循环,并有一个命名变量的模式。在method2中,您应该能够轻松找到这些参数。 下面,我假设您有一个名为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;
}
public void方法_1(字符串arg1,字符串arg2)
{
/*
//某些特定于方法_1的代码实现
*/
var dict=GetArguments(args);
方法2(dict);
}
专用字典GetArguments(列表参数)
{
Dictionary dict=新字典();
var计数器=1;
foreach(变量args中的argItem)
{
dict.Add(“Arg”+计数器++,argItem);
}
返回命令;
}
这会起作用,但我不知道你为什么要把字典传给
方法2
,除非你别无选择


这将起作用,但我不知道为什么要将字典传递给
方法2
,除非您别无选择。

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

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;
}
public void方法_1(字符串arg1,字符串arg2)
{
var dict=BuildDictionary(arg1,arg2);
方法2(dict);
}
专用字典BuildDictionary(参数对象[]args)
{
Dictionary dict=新字典();
for(int i=0;i
与马特的版本:

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

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

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;
}
public void方法_1(字符串arg1,字符串arg2)
{
var dict=BuildDictionary(arg1,arg2);
方法2(dict);
}
专用字典BuildDictionary(参数对象[]args)
{
Dictionary dict=新字典();
for(int i=0;i
与马特的版本:

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

没有太多关于方法2的性质和字典中的键代表什么的信息,我建议两个选项

键始终表示Arg+N

在这种情况下,方法_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);
}
键表示调用方的方法参数名称