c#其中对于泛型类型,约束类可能不是

c#其中对于泛型类型,约束类可能不是,c#,generics,constraints,C#,Generics,Constraints,我想排除一些在泛型类中使用的类型。我知道如何进行约束,以确保泛型类型是某种(接口)类型。但我似乎不知道如何排除(多个)类型 例如:我想要一个泛型类来排除int和uint(但不要排除DateTime,所以不是所有的原语都可以被排除) 我不能这样做: public class SomeWhereTest2<T> where T : !System.Int32 { } 公共类SomeWhereTest2其中T:!System.Int32 { } 有人能帮我一次排除多种类型吗 我

我想排除一些在泛型类中使用的类型。我知道如何进行约束,以确保泛型类型是某种(接口)类型。但我似乎不知道如何排除(多个)类型

例如:我想要一个泛型类来排除int和uint(但不要排除DateTime,所以不是所有的原语都可以被排除)

我不能这样做:

public class SomeWhereTest2<T> where T : !System.Int32 
{ 
}
公共类SomeWhereTest2其中T:!System.Int32
{ 
}
有人能帮我一次排除多种类型吗


我创建了一个特殊的类,它的作用类似于一个字典,也是一个具有索引的FiFo集合,其中0是最近的值,1是以前的值等(
public class FiFoDictionary:IDictionary
,它使用
OrderedDictionary
作为内部指令。)


但由于索引是通过
int
给出的,因此当字典的键是
int
时会出现问题。因为这样就得到了链接到键的值,而不是索引。或者有没有任何方法可以强制使用索引而不是键作为
有序字典的索引?

这是不可能的无法使用and==运算符,因为无法保证具体类型参数将支持这些运算符

在C#中没有这样的东西,但是你可以用一种可能帮助你实现这一点的方式来定义泛型类型的范围。这只是一个例子,我不认为这是解决你问题的一个解决方案。
class MyGeneric<T> where T : new() { } // T must be a class with default constructor
class MyGeneric<T> where T : System.IComparable<T> { }// T must implement IComparable interface 
class MyGeneric<T> where T : System.IComparable<T>, new () { }
class MyGeneric其中T:new(){}//T必须是具有默认构造函数的类
类MyGeneric,其中T:System.IComparable{}//T必须实现IComparable接口
类MyGeneric,其中T:System.IComparable,new(){}

根据评论中的解释,为什么您认为您需要这个:不, 您不需要从泛型类型中排除
int

在泛型类中使用类中的重载方法(仅参数类型不同的方法)时,在编译泛型类时,已决定调用哪种方法,而泛型类独立于随后使用的具体类型

class TestDictionary<TKey, TValue> 
{
    OrderedDictionary orderedDictionary = new OrderedDictionary();

    public void Add(TKey key, TValue value)
    {
        orderedDictionary.Add(key, value);
    }

    public TValue GetByIndex(int index)
    {
        return (TValue)orderedDictionary[index];
    }

    public TValue GetByKey(TKey key)
    {
        return (TValue)orderedDictionary[key];
    }
}

private void buttonTest_Click(object sender, EventArgs e)
{
    TestDictionary<int, string> test = new TestDictionary<int, string>();

    test.Add(42, "Test");

    MessageBox.Show(test.GetByIndex(0)); // Correct output "Test"
    MessageBox.Show(test.GetByKey(42)); // Correct output "Test"
}
例如:

class Test<T>
{
    public void Trigger(T test)
    {
        // Will always call Internal(object) and never call Internal(int) even when T is int.
        Internal(test);
    }

    private void Internal(int test)
    {
        MessageBox.Show("Triggered int");
    }

    private void Internal(object test)
    {
        MessageBox.Show("Triggered object");
    }
}

private void buttonTest_Click(object sender, EventArgs e)
{
    Test<int> test = new Test<int>();
    test.Trigger(42);
}

不可能在编译时执行此操作。约束不是这样工作的。约束的要点是,对
T
的约束提供了
T
功能。如果约束到特定接口或类,则可以调用在该接口或类中定义的方法/属性。如果添加
新的
约束,则可以实例化类型为
T
的对象。您需要在运行时执行此检查(检查类型,如果类型错误,则引发异常)我很好奇您可以编写哪些代码,以保证能够适用于所有可能的类型,包括尚未编写的类型,但不知何故,它无法专门处理两种类型。@Damien_The_Unsivers我创建了一个特殊的类,它的作用类似于一个字典,也是一个带有索引的FiFo集合,其中0是最新的值,1是以前的值,等等。(公共类FiFoDictionary:IDictionary,它使用OrderedDictionary作为内部命令。)但是,由于索引是通过int给出的,因此当字典的键是int时会出现问题。因为这样会得到链接到键的值,而不是索引。或者有没有办法强制使用索引而不是索引键来创建一个OrderedDictionary?@Flydog57我有点想,但希望看到一些我还没有看到的东西。所以,我猜唯一的方法就是检查运行时。无论如何,谢谢Int32实现了IComparable。所以这将允许int。感谢您花时间帮助我,非常感谢。我认为这种方法会很有效。这很有道理。(我做了一个快速测试,到目前为止我做了工作)。你好,Matthijs