2013-10-03 36 views
1

我下面它使用委託關鍵字(按我的理解)一書作爲函數的名稱代表被封裝(一種被稱爲其使用委託對象名稱/構造函數調用)。下面是代碼:C#.NET委託關鍵字作爲函數的名稱,使用委託對象/構造

//Declaration of delegate object AppendChildData 
    public delegate void AppendChildData(T entityAggregate, object childEntityKeyValue); 

    //Dictionary of Delegate Objects 
    private Dictionary<string, AppendChildData> childCallbacks; 

    //Creation of dictionary object inside constructor of containing class. Also calling the BuildChildCallbacks() function 
protected SqlRepositoryBase(IUnitOfWork unitOfWork) 
      : base(unitOfWork) 
     { 
      this.childCallbacks = new Dictionary<string, AppendChildData>(); 
      this.BuildChildCallbacks(); 
      //Other Initializations 
     } 

    protected override void BuildChildCallbacks() 
     { 
     this.childCallbacks.Add("allowances", delegate(Project project, object childKeyName) 
      { 
        this.AppendProjectAllowances(project); 
      }); 
     } 

現在如果仔細看過:

delegate(Project project, object childKeyName) 
      { 
        this.AppendProjectAllowances(project); 
      }); 

已被宣佈爲內部BuildChildCallbacks字典childCallBacks的值()函數。字典childCallBacks的這個值是一個名爲delegate()的函數。爲什麼? .NET如何在這種情況下使用委託關鍵字?


這本書我也下使用功能名稱,而不是使用委託關鍵字,BuildChlidCallBack的完整代碼如下:

protected override void BuildChildCallbacks() 
{ 
    this.ChildCallbacks.Add(ProjectFactory.FieldNames.OwnerCompanyId, 
     this.AppendOwner); 
    this.ChildCallbacks.Add(
     ProjectFactory.FieldNames.ConstructionAdministratorEmployeeId, 
     this.AppendConstructionAdministrator); 
    this.ChildCallbacks.Add(ProjectFactory.FieldNames.PrincipalEmployeeId, 
     this.AppendPrincipal); 
    this.ChildCallbacks.Add("allowances", 
     delegate(Project project, object childKeyName) 
     { 
      this.AppendProjectAllowances(project); 
     }); 
    this.ChildCallbacks.Add("contracts", 
     delegate(Project project, object childKeyName) 
     { 
      this.AppendContracts(project); 
     }); 
    this.ChildCallbacks.Add("contacts", 
     delegate(Project project, object childKeyName) 
     { 
      this.AppendContacts(project); 
     }); 
} 

在小心觀察我才知道該委託的關鍵字被使用如果字典鍵不是外鍵(不是DataTable的列),並且調用這些委託將NULL傳遞給第二個參數,即delegate()函數的「childKeyName」。調用使用詞典()函數委託的代碼如下:

protected virtual T BuildEntityFromReader(IDataReader reader) 
{ 
    T entity = this.entityFactory.BuildEntity(reader); 
    if (this.childCallbacks != null && this.childCallbacks.Count > 0) 
    { 
     object childKeyValue = null; 
     DataTable columnData = reader.GetSchemaTable(); 
     foreach (string childKeyName in this.childCallbacks.Keys) 
     { 
      if (DataHelper.ReaderContainsColumnName(columnData, childKeyName)) 
      { 
       childKeyValue = reader[childKeyName]; 
      } 
      else 
      { 
       childKeyValue = null; 
      } 
      this.childCallbacks[childKeyName](entity, childKeyValue); 
     } 
    } 
    return entity; 
} 

其中讀者功能的IDataReader其包含來自數據表的單個記錄/實體/行(排序讀取器的函數是reader.read)。

我的問題是,爲什麼代表字已被用作函數名分配到AppendChildData委託對象在childCallBack字典,而不是功能的名稱? (因爲它是編譯和運行良好)

而且,請問.NET德拉斯在這種情況下,委託關鍵字的使用情況如何?

+0

你需要任何其他解釋嗎?如果不將它標記爲答案,以便其他人也可以使用它。 –

回答

2

如果我正確理解你的問題,你困惑在不同的地方使用關鍵字delegate的。

public delegate void AppendChildData(T entityAggregate, object childEntityKeyValue); 

排序答案在上面的delegate

代表是一種類型,它定義一個方法簽名。當你實例化一個委託時,你可以將它的實例與任何具有兼容簽名的方法相關聯。您可以通過委託實例調用(或調用)該方法。你可以找到更多關於MSDN

在第二種情況下,像下面

protected override void BuildChildCallbacks() 
    { 
    this.childCallbacks.Add("allowances", delegate(Project project, object childKeyName) 
     { 
       this.AppendProjectAllowances(project); 
     }); 
    } 

delegate用於聲明anonymous method。在一句話中,它是沒有名字的方法。更說明這裏MSDN

最後但並非最不重要的,在未來,你應該更具體的瞭解你的問題。 :)