C#.NET委托关键字作为要使用委托对象/构造函数调用的函数的名称 [英] C#.NET delegate keyword as name of the function to be called using delegate object/constructor

查看:248
本文介绍了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的此值是一个名为委托()的函数.为什么? 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传递给第二个参数,即proxy()的"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,它包含DataTable中的单个记录/实体/行(函数中的读者种类是reader.read).

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

那么,在这种情况下.NET如何使用委托关键字呢?

解决方案

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

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

上面delegate的排序答案是

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

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

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

delegate用于声明anonymous method.一句话,它是没有名字的方法.更多说明在这里 MSDN

最后但并非最不重要的一点是,将来您应该对自己的问题更加具体. :)

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

Now if seen carefully:

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

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?


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

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

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

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)

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

解决方案

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

The sort answer for the delegate in the above is

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

In the second case like below

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

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委托关键字作为要使用委托对象/构造函数调用的函数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆