C# 确定列表是否<;类型>;包含ICollection<;动态>;

C# 确定列表是否<;类型>;包含ICollection<;动态>;,c#,generics,C#,Generics,我需要确定列表是否包含ICollection,其中T是动态的,并且在编译时未知。以下是我的代码,以便更好地理解我的意思: private void RefreshDataSource<T>(ICollection<T> dataSource) where T : IEquatable<T> { dynamic row = view.GetFocusedRow(); //Get's the focused row from a DevExpress

我需要确定列表是否包含ICollection,其中T是动态的,并且在编译时未知。以下是我的代码,以便更好地理解我的意思:

private void RefreshDataSource<T>(ICollection<T> dataSource) where T : IEquatable<T>
{
   dynamic row = view.GetFocusedRow(); 
   //Get's the focused row from a DevExpress-Grid
   //I don't know the type because it's MasterDetail and the view can be a DetailView. In this case type T isn't the underlying type.

   //Getting all Properties
   var dummy = dataSource.FirstOrDefault();
   var props = dummy.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

   //Now comes the real problem, I need to determine the Detail Datasource
   //and to do this I want to check if there is a Property from ICollection<typeof(row)>

   //How can I check on ICollection<typeof(row)> instead of row
   //IsAssignableFrom would better fit my needs but I don't get how to solve my problem with it.
   var detailSource = props.FirstOrDefault(p => p.PropertyType.IsInstanceOfType(row));
}

所以row是AnyClass类型,在运行时已知。但是如何在T的PropertyCollection中找到这个
ICollection
属性呢?这是我的问题。

一般来说,这应该可以

.GetType().GetInterfaces().Any(intf => intf == typeof(ICollection<dynamic>));
如果你想要T和它的子类型,它会变得更复杂

.GetType().GetInterfaces().Any(intf => intf.IsGenericType && 
                                        intf.GetGenericTypeDefinition() == typeof(ICollection<>) && 
                                        intf.GetGenericArguments()[0].IsAssigneableFrom(typeof(T)));
.GetType().GetInterfaces().Any(intf=>intf.IsGenericType&&
intf.GetGenericTypeDefinition()==typeof(ICollection)&&
intf.GetGenericArguments()[0]。IsAssignableFrom(typeof(T));
编辑因为您计算了实际需要的内容:

.GetType().GetInterfaces().Any(intf => intf.IsGenericType && 
                                       intf.GetGenericTypeDefinition() == typeof(ICollection<>)); 
.GetType().GetInterfaces().Any(intf=>intf.IsGenericType&&
intf.GetGenericTypeDefinition()==typeof(ICollection));

它可以简单得多,但是
ICollection
没有实现
ICollection

我喜欢反射,如果您正在描述确切的问题,我可以帮助您。你想知道ICollection中“T”的类型名吗?@Karthik AMR不,我代码的最后一行是最重要的一行。在这里,我想检查props是否包含一个条目,其中p=>p.PropertyType属于icollection类型,您真的是指
dynamic
还是T`?我真的是指dynamic。因为T是数据源的类型,但如果我在DetailView上,则基础类型将是来自T的ICollection属性。@Sebi解释您想要做什么,而不是如何尝试做<代码>列表实现
i收集
,这意味着它可以被视为
i收集
。您的意思是该列表可以包含其他集合吗?其中一些可能是
列表
列表
?如果props包含条目ICollection,并且在运行时行是MyClass,则可以使用
.OfType
从IEnumerableSo检索特定类型的所有项。这是真的吗?我认为必须有一个属性ICollection,而不是…我不确定这会有多好-
dynamic
是一组编译器技巧,这些对象的实际声明类型是
object
-因此我认为
typeof
将生成与
typeof(ICollection)相同的类型
will-所以可能会出现一些误报。@CSharpie我测试了这个,但似乎不起作用。例如,检查ICollection与ICollection类型的属性不匹配。@Sebi当然不匹配。ICollection不是ICollection。@CSharpie是的,但这正是我需要的。我需要比较一个ICollection,其中泛型部分在运行时已知,但在此之前不知道。
.GetType().GetInterfaces().Any(intf => intf.IsGenericType && 
                                        intf.GetGenericTypeDefinition() == typeof(ICollection<>) && 
                                        intf.GetGenericArguments()[0].IsAssigneableFrom(typeof(T)));
.GetType().GetInterfaces().Any(intf => intf.IsGenericType && 
                                       intf.GetGenericTypeDefinition() == typeof(ICollection<>));