有没有办法检查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);
但它不起作用。有什么想法吗?
答案 0 :(得分:2)
ConstructorInfo [] constructorInfos = typeof(T)。GetConstructors();
ConstructorInfo constructorInfo = constructorInfos [0];
ParameterInfo [] paramsVar = constructorInfo.GetParameters();
IEnumerable collectionParams = paramsVar.Where(
x =&gt; x.ParameterType.GetElementType()IsAssignableFrom(typeof运算(ICollection的)))。
很容易混淆a.IsAssignableFrom(b)
与b.IsAssignableFrom(a)
!
@BartoszKP有right answer.
答案 1 :(得分:2)
试试这个:
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
)