行动与代表之间的可访问性 [英] Accessibility between Action and Delegate

查看:56
本文介绍了行动与代表之间的可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我创建了一个名为 Mhd 的委托-就像 Action 委托一样。
我的问题:如果两个委托都是公开,为什么为什么只有 Action 委托在另一个类中可见,而不是 Mhd

In the code below I created a delegate named Mhd - just like the Action delegate.
My question: if the two delegates are public, why only Action delegate is visible from another class and not Mhd?

static void Main(string[] args)
    {
        new Test().Yaser(); //this can be done
        new Test().mhd(); //this can not be done
    }
    class Test
    {
        public Action Yaser;
        public delegate void Mhd();
    }

    //and Action definition is   public delegate void Action();

不胜感激:)

推荐答案

为使我的答案有意义,记住委托的定义很重要。根据 MSDN

For my answer to make sense, it's important to remember the definition of a delegate. According to MSDN:


委托是一种引用类型,可用于封装命名方法或匿名方法。

A delegate is a reference type that can be used to encapsulate a named or an anonymous method.

委托是引用?!?!!

如果您熟悉C ++,就知道 引用 的另一种说法是 指针 。 (实际上,C ++开发人员通过函数指针获得与C#委托人的相似功能。)

If you are familiar with C++, you know that another way of saying reference is pointer. (In fact, C++ developers get similar functionality to C# delegates via function pointers.)

委托人被引用的意义是什么?在普通类型系统提供的基本构造中,.NET Framework具有另一种引用类型: class 。说委托是引用类型,就像说委托是类一样。让我们回顾一下如何使用类。

What is the significance of delegates being references? Among the basic constructs provided by the common type system, the .NET Framework has another reference type: class. Saying that a delegate is a reference type is just like saying that a delegate is a class. Let's review how we use classes.

在使用类的实例之前,您需要遵循以下三个步骤:

There are 3 steps you need to follow before you can use an instance of a class:


  • 类型声明: class Test {public Action Yaser; }

  • 实例声明: class测试testClassObject;

  • 实例化: testClassObject = new Test();

  • Type declaration: class Test { public Action Yaser; }
  • Instance declaration: class Test testClassObject;
  • Instantiation: testClassObject = new Test();

(通常,我们将实例声明和实例化。)

(Typically, we combine instance declaration and instantiation).

我们说委托是类。因此,委托使用遵循相同的模式:

We said that delegates are classes. Therefore, delegate use follows the same pattern:


  • 类型声明:公共委托void Mhd();

  • 实例声明: public Mhd myMhd;

  • 实例: myDelegateField = new Mhd(SomeMethod);

  • Type declaration: public delegate void Mhd();
  • Instance declaration: public Mhd myMhd;
  • Instantiation: myDelegateField = new Mhd(SomeMethod);

但是等等,什么是 SomeMethod ?没关系,没关系。我们所知道的是它的签名必须与Mhd的签名匹配。换句话说, void SomeMethod()

But wait, what is SomeMethod? Truly, it doesn't matter. All that we know is that its signature must match that of Mhd. In other words, void SomeMethod()

让我们检查并修复您的类声明。可能的实现如下所示:

Let's inspect and fix your class declaration. A possible implementation is shown below:

class Test
{
    public Action Yaser;        // instance declaration
    public delegate void Mhd(); // type declaration
    public Mhd myMhd;           // instance declaration

    public Test()
    {
        // instantiation
        this.myMhd = new Mhd(this.SomeMethod);
    }

    private void SomeMethod()
    {
        // your implementation
    }
}

这篇关于行动与代表之间的可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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