欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#如何判断委托是实例方法还是静态方法

程序员文章站 2023-11-18 12:58:58
一. 委托的分类 通过用什么类型的方法来声明为委托,可以分为两类: 1. 委托静态方法:把一个静态方法给委托 2. 委托实例方法:把一个实例对象的成员方法给委托 (这两个名字是博主取的,可能不是很专业只是为了好区分) 二. 原理部分 委托是将函数指针和实例对象打包在一起的类,它有两个重要的成员,一个 ......

一. 委托的分类

通过用什么类型的方法来声明为委托,可以分为两类:

1. 委托静态方法:把一个静态方法给委托

2. 委托实例方法:把一个实例对象的成员方法给委托

(这两个名字是博主取的,可能不是很专业只是为了好区分)

二. 原理部分

委托是将函数指针和实例对象打包在一起的类,它有两个重要的成员,一个用来保存实例对象,一个用来保存函数的指针。从源码中我们可以查看system.delegate,如下:

1. 将会调用方法所在的对象

// _target is the object we will invoke on
[system.security.securitycritical]
internal object _target;

2. 将会调用的方法指针

// _methodptr is a pointer to the method we will invoke
// it could be a small thunk if this is a static or um call
[system.security.securitycritical]
internal intptr _methodptr;

另外,我们查看system.delegate的属性,我们可以看到一个属性 target

public object target
{
    get
    {
        return gettarget();
    }
}    

来看一下这个 gettarget() 方法的功能

[system.security.securitysafecritical]
internal virtual object gettarget()
{
    return (_methodptraux.isnull()) ? _target : null;
}

可以看到这边有一个字段 _methodptraux,这是一个intptr类型的指针,可以看到注释可以看出,当把一个静态方法给委托的时候,将会返回一个 null,如果是一个实例方法的时候,将会返回当前方法所在的实例对象(this)

// in the case of a static method passed to a delegate, this field stores
// whatever _methodptr would have stored: and _methodptr points to a
// small thunk which removes the "this" pointer before going on
// to _methodptraux.
[system.security.securitycritical]
internal intptr _methodptraux;

三. 测试代码

测试类 test.cs:

public class test
{
    /// <summary>
    /// 实例方法
    /// </summary>
    public void showhelloworld1()
    {
        console.writeline("hello world! -- 1");
    }

    /// <summary>
    /// 静态方法
    /// </summary>
    public static void showhelloworld2()
    {
        console.writeline("hello world! -- 2");
    }
}

委托声明:

public delegate void showhelloworldmethod();

上端测试代码:

//声明测试对象
test test = new test();

//构造实例方法的委托
showhelloworldmethod del = test.showhelloworld1;

//判断一下target是不是指向方法所在的对象
console.writeline(del.target is test);//true

//调用一下
((test)del.target).showhelloworld1();//hello world! -- 1

//构造静态方法的委托
showhelloworldmethod del2 = test.showhelloworld2;

//判断一下target是不是null
console.writeline(del2.target == null);//true

测试结果符合我们的预期:

C#如何判断委托是实例方法还是静态方法

 

 

四. 总结

如果委托的target属性为null说明是静态方法的委托,如果委托的target属性不为null说明是实例方法的委托。