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

C#.NET delegate keyword as name of the function to be called using delegate object/constructor(C#.NET 委托关键字作为要使用委托对象/构造函数调用的函数的名称)
本文介绍了C#.NET 委托关键字作为要使用委托对象/构造函数调用的函数的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注一本书,它使用委托关键字(根据我的理解)作为要封装在委托中的函数的名称(使用委托对象名称/构造函数调用的函数).下面是代码:

I am following a book which uses delegate keyword (as per my understanding) as name of the function to be encapsulated in delegate (the one which is called using delegate object name/constructor). below is the code:

    //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 delas 如何使用委托关键字?

has been declared as value of dictionary childCallBacks inside BuildChildCallbacks() function. This value of dictionary childCallBacks is a function named delegate(). WHY? and how does .NET delas with usage of delegate keyword in this case?

我在关注的这本书也使用了函数名而不是使用delegate关键字,BuildChlidCallBack的完整代码如下:

The book i am following also uses function names instead of using delegate keyword, the complete code of BuildChlidCallBack is below:

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 传递给委托()的第二个参数,即childKeyName"功能.使用字典调用delegate()函数的代码如下:

on carefull observation i come to know that delegate keyword has been used in case where dictionary keys are not Foreign keys (not column of DataTable) and calling these delegates passes NULL to the second argument namely "childKeyName" of the delegate() function. The code that calls the delegate() functions using dictionary is below:

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

函数中的 reader 是 IDataReader,它包含 DataTable 中的单个记录/实体/行(函数中的 reader 类型是 reader.read).

where reader in function is IDataReader which contains single record/entity/row from DataTable (sort of reader in function is reader.read).

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

My Question is that why delegate word has been used as name of function to assign to AppendChildData delegate-object in childCallBack dictionary, instead of a function name? (As it is compiling and running fine)

而且,在这种情况下,.NET delas 如何使用委托关键字?

And, how does .NET delas with usage of delegate keyword in this case?

推荐答案

如果我正确理解了你的问题,你对在不同地方使用关键字 delegate 感到困惑.

If I understand your question correctly, you're confused with use of the keyword delegate at different places.

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

上面delegate的排序答案是

委托是一种定义方法签名的类型.实例化委托时,可以将其实例与具有兼容签名的任何方法相关联.您可以通过委托实例调用(或调用)该方法.您可以在 MSDN

A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance. You can find more on MSDN

第二种情况如下

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

delegate 用于声明匿名方法.一句话,是没有名字的方法.更多描述在这里 MSDN

delegate is used to declare anonymous method. In one sentence, it is method without name. More description is here MSDN

最后但并非最不重要的一点是,将来您应该更具体地提出您的问题.:)

And last but not least, in future you should be more specific about your questions. :)

这篇关于C#.NET 委托关键字作为要使用委托对象/构造函数调用的函数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)