转换到一个奇怪的重复派生类

c#

我有一个使用奇怪重复模板模式的代码库,带有一个基类和一个派生类。
在接受基类参数的某个方法中,我想检查我收到的参数是否为派生类型,但我无法强制转换为它。
这是一个说明问题的示例:

class Base<T> where T : Base<T> {
}

class Derived<T> : Base<T>
    where T : Derived<T> { 
}

class DerivedBanana : Derived<DerivedBanana> { 
}

class Program {

    static void DoSomething<T>(T t) where T : Base<T> {
        // CS0311: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Derived<T>'.
        // There is no implicit reference conversion from 'T' to 'Derived<T>'.
        var d = t as Derived<T>;
    }

    static void Main(string[] args) {
        DoSomething(new DerivedBanana());
    }
}

DoSomething如果T我得到的参数实际上是 a ,有没有办法检查我的方法Derived<T>

注意:我正在重构我的代码以避免这种情况,但我仍然想知道如何进行这样的转换:)

注2:建议的重复不回答我的问题:我想知道如何给派生类型,而不仅仅是检查变量是派生类型。

以上是转换到一个奇怪的重复派生类的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>