简体   繁体   English

等效Java通用循环引用C#

[英]Equivalent Java generic circular reference C#

I am porting a Java library in C#, and I found this weird class that uses a generics circular reference: 我在C#中移植一个Java库,我发现这个奇怪的类使用了泛型循环引用:

public static class UserAgentAnalyzerDirectBuilder<UAA extends UserAgentAnalyzerDirect, B extends UserAgentAnalyzerDirectBuilder<UAA, B>> 
{
   // Implementation
}

I can't well understand how a circular reference can work, but as I see is indeed possible. 我不太清楚循环引用如何工作,但我认为确实可行。

That code can be easily translated to C#: 该代码可以很容易地转换为C#:

public class UserAgentAnalyzerDirectBuilder<UAA, B> where UAA: UserAgentAnalyzerDirect where B: UserAgentAnalyzerDirectBuilder<UAA, B>
{
  // Implementation
}

Assuming my equivalent class is correct, I need to implement the method that instantiate that weird class. 假设我的等效类是正确的,我需要实现实例化那个奇怪的类的方法。

In Java I have this code: 在Java中我有这个代码:

public static UserAgentAnalyzerDirectBuilder<? extends UserAgentAnalyzer, ? extends UserAgentAnalyzerDirectBuilder<?, ?>> newBuilder() {
    return new UserAgentAnalyzerDirectBuilder<>(new UserAgentAnalyzer());
}

In C# I tried to reproduce with: 在C#中我尝试重现:

public static UserAgentAnalyzerDirectBuilder<UAA, UserAgentAnalyzerDirectBuilder<UAA, B>> newBuilder<UAA, B>() 
            where UAA: UserAgentAnalyzerDirect
            where B: UserAgentAnalyzerDirectBuilder<UAA, B>
        {
            return new UserAgentAnalyzerDirectBuilder<UAA, UserAgentAnalyzerDirectBuilder<UAA, B>> (new UserAgentAnalyzer());
        }

But it doesn't work. 但它不起作用。 I am wondering if I did something wrong or if in C# generics circular references are indeed possible. 我想知道我是否做错了,或者在C#泛型中是否确实存在循环引用。

What you see is not a circular reference. 你看到的不是循环参考。 The type parameter constraint just lets you pass in a type which is a descendant of the generic type specified by the constraint. 类型参数约束只允许您传入一个类型,该类型是约束指定的泛型类型的后代。

The following code example compiles and I think will do what you need: 下面的代码示例编译,我认为将做你需要的:

public class UserAgentAnalyzerDirect { }

public class UserAgentAnalyzerDirectBuilder<UAA, B> 
    where UAA : UserAgentAnalyzerDirect 
    where B : UserAgentAnalyzerDirectBuilder<UAA, B>
{
    // this method is supposed to implement the effect of the 
    // constructor in the original Java code
    public void SetUAA(UAA a) { }

    // further implementation
}

public static UserAgentAnalyzerDirectBuilder<UAA, B> NewBuilder<UAA, B>()
    where UAA : UserAgentAnalyzerDirect, new()
    where B : UserAgentAnalyzerDirectBuilder<UAA, B>, new()
{
    // Unlike in Java, C# allows instantiating generic type parameters only using 
    // a parameter-less constructor. Hence we use the SetUAA method here instead.
    var a = new UAA();
    var b = new B();
    b.SetUAA(a);
    return b;
}

Then you can create custom descendants of the generic classes above like this: 然后你可以创建上面的泛型类的自定义后代,如下所示:

public class CustomUserAgentAnalyzerDirect : UserAgentAnalyzerDirect { }

public class CustomUserAgentAnalyzerDirectBuilder : UserAgentAnalyzerDirectBuilder<CustomUserAgentAnalyzerDirect, CustomUserAgentAnalyzerDirectBuilder> { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM