c# - 等效的 Java 通用循环引用 C#
问题描述
我正在用 C# 移植一个 Java 库,我发现这个奇怪的类使用了泛型循环引用:
public static class UserAgentAnalyzerDirectBuilder<UAA extends UserAgentAnalyzerDirect, B extends UserAgentAnalyzerDirectBuilder<UAA, B>>
{
// Implementation
}
我不能很好地理解循环引用是如何工作的,但正如我所见,确实是可能的。
该代码可以很容易地转换为 C#:
public class UserAgentAnalyzerDirectBuilder<UAA, B> where UAA: UserAgentAnalyzerDirect where B: UserAgentAnalyzerDirectBuilder<UAA, B>
{
// Implementation
}
假设我的等效类是正确的,我需要实现实例化那个奇怪的类的方法。
在Java中我有这个代码:
public static UserAgentAnalyzerDirectBuilder<? extends UserAgentAnalyzer, ? extends UserAgentAnalyzerDirectBuilder<?, ?>> newBuilder() {
return new UserAgentAnalyzerDirectBuilder<>(new UserAgentAnalyzer());
}
在 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());
}
但它不起作用。我想知道我是否做错了什么,或者在 C# 泛型中循环引用是否确实是可能的。
解决方案
你看到的不是循环引用。类型参数约束只允许您传入一个类型,该类型是约束指定的泛型类型的后代。
下面的代码示例编译,我认为会做你需要的:
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;
}
然后你可以像这样创建上面通用类的自定义后代:
public class CustomUserAgentAnalyzerDirect : UserAgentAnalyzerDirect { }
public class CustomUserAgentAnalyzerDirectBuilder : UserAgentAnalyzerDirectBuilder<CustomUserAgentAnalyzerDirect, CustomUserAgentAnalyzerDirectBuilder> { }
推荐阅读
- csv - Apache Nifi 如何从流文件内容中创建逗号分隔的值列表
- angular - 在 Angular 9 中,将表单设置为本地存储时,如何指定某些参数不保存?
- python - 如何将 Python 连接到 SAS Enterprise Guide (EG) 服务器
- c++ - 将元素添加到 QGroupBox 子时,QFrame 未调整大小
- arrays - Excel拆分出不同颜色的数组
- jsp - 如何在 JSF 中获取上下文路径
- html - 如何在 Shiny 中动态设置 pickerInput 菜单的样式
- reactjs - React.useCallback 不更新依赖
- arrays - Excel VBA:工作表数组,从一个工作簿复制到另一个
- tls1.2 - FAKE F# - 在 https 请求上启用 TLS