有没有办法检查 ParameterInfo 是否是一个集合?
我试过这个:
ConstructorInfo[] constructorInfos = typeof(T).GetConstructors();
ConstructorInfo constructorInfo = constructorInfos[0];
ParameterInfo[] paramsVar = constructorInfo.GetParameters();
IEnumerable<ParameterInfo> collectionParams = paramsVar.Where(
x => x.ParameterType.GetElementType() is ICollection);
但它不起作用。有什么想法吗?
请您参考如下方法:
试试这个:
ConstructorInfo[] constructorInfos = typeof(T).GetConstructors();
ConstructorInfo constructorInfo = constructorInfos[0];
ParameterInfo[] paramsVar = constructorInfo.GetParameters();
IEnumerable<ParameterInfo> collectionParams = paramsVar.Where(
x => typeof(ICollection).IsAssignableFrom(x.ParameterType));
(请注意,我删除了 GetElementType 调用并切换了 typeof(ICollection) 和 x.ParameterType)
