2013-06-25 28 views
1

老問題
原來這就是我想實現...如何避免重寫相同的代碼在不同的方法

我有現成的抽象類..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次調用 相同的代碼...

編輯並重提問題
我有fina lly決定用一種私人方法建立字典,並用其他所有方法調用它。請在本段之前忽略其他所有內容。 我的方法看起來像這樣

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方法,以獲得所需的字典結構?

+1

你如何在Method_2中使用字典?這可能有助於將簽名更改爲該方法以簡化流程。鑰匙是否遵循一種模式(即ArgN),或者這僅僅是爲了演示的目的? –

+0

歡迎來到「循環」的美好世界! – Tdorno

+0

@SimonBelanger:我編輯了這個問題.....捆綁字典生成機制不是我關心的問題,我仍然需要在所有方法中添加相同的代碼(大約50)..我試圖避免手動一次又一次地調用相同的代碼50次... – blank

回答

3

沒有對Method_2的性質和鍵在字典中表示,我會建議兩種選擇什麼太多的信息。

的鍵始終代表精氨酸+ 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

+0

選項2的輸出不完全是我正在尋找的... if arg1 =「a」和agr2 =「b」,那麼Method_2中的登錄會創建包含7個關鍵字的字典對象,如長度,長度,Rank ...我猜是對象數據類型的屬性 – blank

+0

@Simon ..我編輯過再次提問...請看看編輯的問題 – blank

+0

構建字典應該是一個'object',而不是'params object []'。創建一個匿名類型創建一個對象,這將永遠是這種情況,除非你嘗試傳遞多個對象與失敗的目的 –

0

你應該使用一個循環,並有一個模式來說明如何命名變量。在您的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; 
} 
+0

我編輯了這個問題.....捆綁字典生成機制不是我關心的,我仍然需要添加相同的代碼在所有的方法(大約50)..我試圖避免手動調用相同代碼再次50次 – blank

+0

您可以將此字典作爲類級字段並更新args列表的setter以在您添加項目時更新此字典。這樣你就不需要做任何迭代。但是,請注意在開始使用之前重置此列表/字典,如果這對您的邏輯造成問題 –

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除非你別無選擇。

+0

我編輯了問題.....捆綁字典生成機制不是我關心的問題,我仍然需要添加所有方法中的相同代碼(大約50)..我試圖避免手動調用相同的代碼一次又一次50次 – blank

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; 
} 

與馬特的版本:

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

我編輯了問題.....捆綁字典生成機制不是我關心的,我仍然需要在所有方法中添加相同的代碼(大約50)..我試圖避免手動調用相同的代碼一次又一次50次 – blank

相關問題