-->

C#.NET delegate keyword as name of the function to

2020-03-30 10:02发布

问题:

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?

回答1:

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. :)