基类如何引用超类型并仍然可以编译?

Aar*_*her 0 c#

这个类怎么编译?

public class ReflectionHelper
{
    public static IReflectionHelper CreateHelper( object subject )
    {
        return ( IReflectionHelper )Activator.CreateInstance( typeof( ReflectionHelper<> ).MakeGenericType( GetFirstGenericArgument( subject ) ) );
    }
    public static Type GetFirstGenericArgument( object subject )
    {
        return subject.GetType().GetGenericArguments()[ 0 ];
    }
}

public class ReflectionHelper<T> : ReflectionHelper, IReflectionHelper where T : new()
    {}
Run Code Online (Sandbox Code Playgroud)

注意ReflectionHelper <T>继承自ReflectionHelper,它反映了ReflectionHelper的类型<>由于某种原因,我期待循环引用编译错误.

Jon*_*eet 5

你必须详细说明你的"某些原因",以便我们说出它为什么不是一个问题..NET中存在循环引用 - 它们不是问题,只要它在同一个程序集中.对于更简单的非通用示例:

// Compiles with no problem
class Foo
{
    Bar other;
}

class Bar
{
    Foo other;
}
Run Code Online (Sandbox Code Playgroud)

(在BCL 中实际上有圆形装配参考......但那只是讨厌和邪恶.)