在C#3.5中获取类型参数约束类的类型

在C#3.5中获取类型参数约束类的类型,c#,generics,types,C#,Generics,Types,我试图获取一个自定义类的类型,但我无权编辑该类,并且该类的声明中有一个参数类型约束。就这样, public class GenericItemCollection<T> where T : System.IEquatable<T> { public GenericItemCollection(); public GenericItemCollection(string json); public int Count { get; } pu

我试图获取一个自定义类的类型,但我无权编辑该类,并且该类的声明中有一个参数类型约束。就这样,

public class GenericItemCollection<T> where T : System.IEquatable<T>
{
    public GenericItemCollection();
    public GenericItemCollection(string json);

    public int Count { get; }
    public List<T> Created { get; }
    public List<T> Current { get; }
    public List<T> Deleted { get; }
    public List<T> Original { get; }
    public List<T> Updated { get; }

    public void AcceptChanges();
    public void AddItem(T item);
    public void BindItem(T item);
    public void DeleteItem(T item);
    public void UpdateItem(T item);
}
公共类GenericItemCollection,其中T:System.IEquatable
{
公共泛型项集合();
公共GenericItemCollection(字符串json);
公共整数计数{get;}
已创建公共列表{get;}
公共列表当前{get;}
已删除公共列表{get;}
公共列表原始{get;}
公共列表已更新{get;}
公共变更();
公共无效附加项(T项);
公共项目(T项);
公共项目(T项);
公共作废更新项(T项);
}
}

所以我想要的是类型GenericItemCollection,当T实际上是某个东西时。即:

private void MyMethod<T>(GenericItemCollection<T> genericList){
    Type listType = typeof(GenericItemCollection<typeof(T)>);
    //...
}
private void MyMethod(GenericItemCollection genericList){
类型listType=typeof(GenericItemCollection);
//...
}
这将被称为:

MyMethod<Foo>(fooGenericList);
MyMethod<Bar>(barGenericList);
MyMethod(fooGenericList);
MyMethod(barGenericList);
在这种情况下,我希望listType是

GenericItemCollection<Foo> 
GenericItemCollection

GenericItemCollection
我知道typeof(T)在运行时之前不存在,但无论如何它都应该返回一个类型,但是VS在

GenericItemCollection

我不太擅长使用泛型,所以我显然遗漏了一些东西,我希望你们指出这是什么。非常感谢。

首先,对
GenericItemCollection
的泛型类型参数应用的任何约束也必须应用于
MyMethod

private void MyMethod<T>(GenericItemCollection<T> genericList) where T: IEquatable<T>

为什么不提供一个
Foo
Bar
都继承的
基类
,并将其作为约束添加到泛型中?是否要限制类型?III这主意不错,但我似乎已经找到了我想要的MakeGenericType。我会做更多的测试并回答我自己的问题,如果有人在测试之前做了,我会做的第一件事就是回答朋友。A得到了一个很好的“没有从'T'到'System.IEquatable'的装箱转换或类型参数转换”错误。@S.O.在添加约束之前,您得到了这个错误,对吗?谢谢,我忽略了我必须向方法声明添加约束的事实。我删除了我的帖子,但我在这里将链接附加到你的fiddle,这样它就不会浪费:dotnetfiddle.net/22JMHR
GenericItemCollection<typeof(T)> 
private void MyMethod<T>(GenericItemCollection<T> genericList) where T: IEquatable<T>
Type listType = typeof(GenericItemCollection<T>);