Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何确定对象是否是实现公共接口的对象集合_C#_.net - Fatal编程技术网

C# 如何确定对象是否是实现公共接口的对象集合

C# 如何确定对象是否是实现公共接口的对象集合,c#,.net,C#,.net,我有以下方法,当PropertyInfo实例引用IDataExtractor对象集合中的对象时,该方法返回true: private bool IsCollectionOfIDataExtractors( PropertyInfo propInfo ) { var result = false; var extractors = propInfo.GetValue(dataExtractor, null); if (typeof

我有以下方法,当PropertyInfo实例引用IDataExtractor对象集合中的对象时,该方法返回true:

    private bool IsCollectionOfIDataExtractors( PropertyInfo propInfo )
    {
        var result = false;

        var extractors = propInfo.GetValue(dataExtractor, null);

        if (typeof ( ICollection ).IsAssignableFrom(extractors.GetType() ) ||
            typeof ( ICollection<> ).IsAssignableFrom(extractors.GetType() ) )
        {

            IEnumerator extractor = ((ICollection)extractors).GetEnumerator();

            extractor.MoveNext();

            if (typeof ( IDataExtractor ).IsAssignableFrom(
                extractor.Current.GetType()) )
            {
                result = true;
            }
        }

        return result;
    }

你到底不喜欢演员的哪一点?如果我能帮忙的话,我总是尽量不去演。我意识到,如果我已经检查了分配能力,那么在。但是我不确定typeof(ICollection)是否为真,那么cast是否有效?我现在添加测试来验证所有这些。我更一般的问题是我真正感兴趣的是什么,有人知道实现这种方法的更好方法吗?是的<代码>ICollection实现
ICollection
,因此强制转换不会失败。但如果是我,我会对
null
执行“as”转换并进行测试。我可能还会测试/转换
IEnumerable
而不是
ICollection
,除非您需要的
ICollection
接口有非常具体的内容。@Toby,
ICollection
不能保证实现
ICollection
。BCL集合同时实现了这两种功能,但自定义集合可能实现也可能不实现这两种功能。确切地说,您对强制转换有什么不喜欢的地方?如果我能提供帮助,我总是尝试不强制转换。我意识到,如果我已经检查了分配能力,那么在。但是我不确定typeof(ICollection)是否为真,那么cast是否有效?我现在添加测试来验证所有这些。我更一般的问题是我真正感兴趣的是什么,有人知道实现这种方法的更好方法吗?是的<代码>ICollection实现
ICollection
,因此强制转换不会失败。但如果是我,我会对
null
执行“as”转换并进行测试。我可能还会测试/转换
IEnumerable
而不是
ICollection
,除非您需要的
ICollection
接口有非常具体的内容。@Toby,
ICollection
不能保证实现
ICollection
。BCL集合同时实现了这两种功能,但自定义集合可能实现也可能不实现这两种功能。在撰写本文时,我突然想到,如果集合为空,您将很难确定其成员类型,并且您的原始代码将在
提取器.Current.GetType()
处抛出
NullReferenceException
。谢谢,这减少了我的代码的大小,使它更容易理解。谢谢你,托比。请注意,并非所有实现
ICollection
的对象都必须实现
ICollection
IList
IList
的情况也是如此。我用IEnumerable而不是ICollection重写了它,因为ICollection和ICollection都实现了它。@Dan:D'oh,你说得对。我习惯于看到它们并行实现,以至于在我的脑海中,
ICollection
是一个“继承”的接口。在我写这篇文章时,我突然想到,如果集合是空的,你将很难确定它的成员类型,您的原始代码将在
提取器.Current.GetType()
处抛出一个
NullReferenceException
。谢谢,这减少了我的代码的大小,使它更易于理解。谢谢你,托比。请注意,并非所有实现
ICollection
的对象都必须实现
ICollection
IList
IList
的情况也是如此。我用IEnumerable而不是ICollection重写了它,因为ICollection和ICollection都实现了它。@Dan:D'oh,你说得对。我习惯于看到它们并行实现,以至于在我的脑海中,
ICollection
是一个“继承”的接口。
IEnumerator extractor = ((ICollection)extractors).GetEnumerator();
var extractors = propInfo.GetValue(dataExtractor, null);
var asEnumerable = extractors as IEnumerable;

if (asEnumerable != null)
{
    var enumerator = asEnumerable.GetEnumerator();
    enumerator.MoveNext();

    if (enumerator.Current != null)
        return enumerator.Current is IDataExtractor;
}

return false;