C#.NET委托关键字作为要使用委托对象/构造函数调用的函数的名称

C#.NET委托关键字作为要使用委托对象/构造函数调用的函数的名称,c#,dictionary,delegates,keyword,C#,Dictionary,Delegates,Keyword,我正在阅读一本书,它使用delegate关键字(根据我的理解)作为要封装在delegate中的函数名(使用delegate对象名/构造函数调用的函数名)。代码如下: //Declaration of delegate object AppendChildData public delegate void AppendChildData(T entityAggregate, object childEntityKeyValue); //Dictionary of Dele

我正在阅读一本书,它使用delegate关键字(根据我的理解)作为要封装在delegate中的函数名(使用delegate对象名/构造函数调用的函数名)。代码如下:

    //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); 
            });
      }
已声明为BuildChildCallbacks()函数中dictionary childCallBacks的值。dictionary childCallBacks的这个值是一个名为delegate()的函数。为什么?在这种情况下,.NETDelas如何使用delegate关键字


我下面这本书也使用函数名而不是委托关键字,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);
        });
}
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;
}
仔细观察后,我知道在字典键不是外键(不是DataTable的列)的情况下使用了delegate关键字,调用这些委托会将NULL传递给delegate()函数的第二个参数,即“childKeyName”。使用字典调用delegate()函数的代码如下:

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);
        });
}
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,它包含来自DataTable的单个记录/实体/行(函数中的读取器类型是reader.read)

我的问题是,为什么在childCallBack字典中将委托词用作要分配给AppendChildData委托对象的函数名,而不是函数名?(因为它正在编译并运行良好)


另外,在这种情况下.NET delas如何使用delegate关键字?

如果我正确理解了您的问题,您会对在不同位置使用delegate关键字感到困惑

public delegate void AppendChildData(T entityAggregate, object childEntityKeyValue);
上述
代表的排序答案为

委托是定义方法签名的类型。实例化委托时,可以将其实例与具有兼容签名的任何方法相关联。您可以通过委托实例调用(或调用)该方法。你可以在网上找到更多信息

在第二种情况下,如下所示

protected override void BuildChildCallbacks()
  {
   this.childCallbacks.Add("allowances", delegate(Project project, object childKeyName) 
        { 
              this.AppendProjectAllowances(project); 
        });
  }
委托
用于声明
匿名方法
。在一句话中,它是没有名字的方法。更多的描述在这里


最后但并非最不重要的一点是,将来你应该更具体地回答你的问题

如果我正确理解了您的问题,您会对关键字
delegate
在不同位置的使用感到困惑

public delegate void AppendChildData(T entityAggregate, object childEntityKeyValue);
上述
代表的排序答案为

委托是定义方法签名的类型。实例化委托时,可以将其实例与具有兼容签名的任何方法相关联。您可以通过委托实例调用(或调用)该方法。你可以在网上找到更多信息

在第二种情况下,如下所示

protected override void BuildChildCallbacks()
  {
   this.childCallbacks.Add("allowances", delegate(Project project, object childKeyName) 
        { 
              this.AppendProjectAllowances(project); 
        });
  }
委托
用于声明
匿名方法
。在一句话中,它是没有名字的方法。更多的描述在这里

最后但并非最不重要的一点是,将来你应该更具体地回答你的问题