加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 获取课程类型?

发布时间:2020-12-16 01:38:37 所属栏目:百科 来源:网络整理
导读:特定 public class A{ public static void Foo() { // get typeof(B) }}public class B : A{} B.Foo()是否有可能在.NET 4中获得typeof(B)?请注意,Foo是静态的. 解决方法 不幸的是,这是不可能的,as dtb explains. 另一种方法是像这样制作A泛型: public clas
特定

public class A
{
    public static void Foo()
    {
        // get typeof(B)
    }
}

public class B : A
{

}

B.Foo()是否有可能在.NET 4中获得typeof(B)?请注意,Foo是静态的.

解决方法

不幸的是,这是不可能的,as dtb explains.

另一种方法是像这样制作A泛型:

public class A<T>
{
    public static void Foo()
    {
        // use typeof(T)
    }
}

public class B : A<B>
{
}

另一种可能性是使A.Foo方法通用,然后在派生类型中提供存根方法,然后调用“基础”实现.

我并不热衷于这种模式.如果你绝对需要保持B.Foo调用约定,你不能使A本身通用,并且你在A.Foo中有很多共享逻辑,你不想在你的派生类型中重复,这可能是值得的.

public class A
{
    protected static void Foo<T>()
    {
        // use typeof(T)
    }
}

public class B : A
{
    public static void Foo()
    {
        A.Foo<B>();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读