我如何知道某个属性是否是通用集合

发布于 2024-08-08 01:04:50 字数 200 浏览 3 评论 0原文

我需要使用 PropertyInfo 类了解类中属性的类型是否是通用集合(List、ObservableCollection)。

foreach (PropertyInfo p in (o.GetType()).GetProperties())
{
    if(p is Collection<T> ????? )

}

I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class.

foreach (PropertyInfo p in (o.GetType()).GetProperties())
{
    if(p is Collection<T> ????? )

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-08-15 01:04:50
Type tColl = typeof(ICollection<>);
foreach (PropertyInfo p in (o.GetType()).GetProperties()) {
    Type t = p.PropertyType;
    if (t.IsGenericType && tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) ||
        t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == tColl)) {
        Console.WriteLine(p.Name + " IS an ICollection<>");
    } else {
        Console.WriteLine(p.Name + " is NOT an ICollection<>");
    }
}

您需要测试 t.IsGenericTypex.IsGenericType,否则如果类型不是泛型,GetGenericTypeDefinition() 将引发异常。

如果属性声明为 ICollection,则 tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) 将返回 true

如果该属性被声明为实现 ICollection 的类型,则
t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == tColl) 将返回 true

请注意,例如,对于 ListtColl.IsAssignableFrom(t.GetGenericTypeDefinition()) 返回 false


我已经测试了所有这些组合的 MyT o = new MyT();

private interface IMyCollInterface1 : ICollection<int> { }
private interface IMyCollInterface2<T> : ICollection<T> { }
private class MyCollType1 : IMyCollInterface1 { ... }
private class MyCollType2 : IMyCollInterface2<int> { ... }
private class MyCollType3<T> : IMyCollInterface2<T> { ... }

private class MyT
{
    public ICollection<int> IntCollection { get; set; }
    public List<int> IntList { get; set; }
    public IMyCollInterface1 iColl1 { get; set; }
    public IMyCollInterface2<int> iColl2 { get; set; }
    public MyCollType1 Coll1 { get; set; }
    public MyCollType2 Coll2 { get; set; }
    public MyCollType3<int> Coll3 { get; set; }
    public string StringProp { get; set; }
}

输出:

IntCollection IS an ICollection<>
IntList IS an ICollection<>
iColl1 IS an ICollection<>
iColl2 IS an ICollection<>
Coll1 IS an ICollection<>
Coll2 IS an ICollection<>
Coll3 IS an ICollection<>
StringProp is NOT an ICollection<>
Type tColl = typeof(ICollection<>);
foreach (PropertyInfo p in (o.GetType()).GetProperties()) {
    Type t = p.PropertyType;
    if (t.IsGenericType && tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) ||
        t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == tColl)) {
        Console.WriteLine(p.Name + " IS an ICollection<>");
    } else {
        Console.WriteLine(p.Name + " is NOT an ICollection<>");
    }
}

You need the tests t.IsGenericType and x.IsGenericType, otherwise GetGenericTypeDefinition() will throw an exception if the type is not generic.

If the property is declared as ICollection<T> then tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) will return true.

If the property is declared as a type which implements ICollection<T> then
t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == tColl) will return true.

Note that tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) returns false for a List<int> for example.


I have tested all these combinations for MyT o = new MyT();

private interface IMyCollInterface1 : ICollection<int> { }
private interface IMyCollInterface2<T> : ICollection<T> { }
private class MyCollType1 : IMyCollInterface1 { ... }
private class MyCollType2 : IMyCollInterface2<int> { ... }
private class MyCollType3<T> : IMyCollInterface2<T> { ... }

private class MyT
{
    public ICollection<int> IntCollection { get; set; }
    public List<int> IntList { get; set; }
    public IMyCollInterface1 iColl1 { get; set; }
    public IMyCollInterface2<int> iColl2 { get; set; }
    public MyCollType1 Coll1 { get; set; }
    public MyCollType2 Coll2 { get; set; }
    public MyCollType3<int> Coll3 { get; set; }
    public string StringProp { get; set; }
}

Output:

IntCollection IS an ICollection<>
IntList IS an ICollection<>
iColl1 IS an ICollection<>
iColl2 IS an ICollection<>
Coll1 IS an ICollection<>
Coll2 IS an ICollection<>
Coll3 IS an ICollection<>
StringProp is NOT an ICollection<>
捂风挽笑 2024-08-15 01:04:50

GetGenericTypeDefinitiontypeof(Collection<>) 将完成这项工作:

if(p.PropertyType.IsGenericType && typeof(Collection<>).IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())

GetGenericTypeDefinition and typeof(Collection<>) will do the job:

if(p.PropertyType.IsGenericType && typeof(Collection<>).IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文