java - C# 相当于 java Class<E> 和 E extends Enum<E>

标签 java c# generics enums

我正在尝试将一个 Java 库转换为 C# 代码,由于我是 C# 新手(直到最近才使用过泛型),所以我有点挣扎......

对于Java泛型,我正在尝试(并且或多或少地了解)它的操作方式。虽然我已经检查过这个Why not extending from Enum<E extends Enum<E>>还有这个How to use Class<T> in Java? ,我无法真正理解这段 Java 代码中发生了什么,因此,我无法考虑等效的 C# 代码。

public abstract class TraciObject<E extends Enum<E>> {
    private final String id;    
    private final EnumMap<E, ReadObjectVarQuery<?>> readQueries;    
    protected TraciObject(String id, Class<E> enumClass) {
        this.id = id;
        readQueries = new EnumMap<E, ReadObjectVarQuery<?>>(enumClass);
    }
    ...
}

到目前为止,我在 C# 中的方法如下:

public abstract class TraciObject<E> where E : Enum<E> { //Non-generic type Enum cannot be used with type arguments
private readonly string id;

private readonly Dictionary<E, ReadObjectVarQuery<E>> readQueries;

protected TraciObject(String id, E enumClass)
{
    this.id = id;
    readQueries = new Dictionary<E, ReadObjectVarQuery<E>>(enumClass); //cannot convert from E  to 'System.Collections.Generic.IEqualityComparer'
}

当我有readQueries = new Dictionary<E, ReadObjectVarQuery<E>>(enumClass);时我试图获得与Java的EnumMap(Class<K> keyType)相对应的,但检查有关字典 https://msdn.microsoft.com/es-es/library/xfhwa508(v=vs.110).aspx 的文档我不确定这是否可能

最佳答案

来自Java Doc :EnumMap(Class<K> keyType) :

Creates an empty enum map with the specified key type.

看来Dictionary您正在寻找的构造函数是无参数的构造函数。

来自MSDN :

Dictionary<TKey, TValue>(): Initializes a new instance of the Dictionary class that is empty, has the default initial capacity, and uses the default equality comparer for the key type.

关于java - C# 相当于 java Class<E> 和 E extends Enum<E>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37142685/

相关文章:

java - 在 Android Service 和 Activity 之间保持 Messenger 的 Activity 状态

c# - 在 Visual Studio C# 中以 MS Access 数据库形式获取 OLE(位图)对象,我的代码有什么问题?

c# - 使用 'this' 作为通用参数,转换问题

java - 递归查找arraylist中的所有组合

java - 从文本文件中删除评论

c# - 从任何域中提取域名的正则表达式

c# - 静态构造函数性能以及为什么我们不能指定 beforefieldinit

c# - 使用通用子类型注册依赖项 - 对于 Mediatr

c# - 将对象转换为其他对象的类型

java - jtextfield 不检索从组合框中选取的值