繁体   English   中英

检查IEnumerable <T> 用反射

[英]Checking for IEnumerable<T> with reflection

编辑

这个问题的简单版本是,如果我有一些object o ,我将如何检查o是否某种类型实现了带有反射的 IEnumerable<string> 最初的问题更具体,但对上述问题的答案也同样好。 对不起,如果我在这个问题上提供了太多细节

结束编辑

以下是一个人为的ValueInjecter POC。 一切都很好,除了最底层的isCollectionMapping方法。 当且仅当源和目标属性都是实现IEnumerable<respectiveTypes>任何对象时,我试图让它返回true。

我试过IsAssignableFromIsInstanceOfType ,但既不似乎工作。

其他一切都有效,因为当我取消注释方法的第二行以明确检查名称“Children”的属性时,它工作正常。

注意 - 我知道此示例存在问题。 也就是说,我正在尝试检查任何旧的IEnumerable<>但是总是知道足以返回List<> ; 在这一点上,它只是一个愚蠢的概念证明。

[TestClass]
public class UnitTest1 {

    [TestMethod]
    public void TestMethod1() {
        List<string> strings = new List<string>();

        Subject S = new Subject() {
            id = 1,
            SubjectName = "S1",
            Children = { new Subject() { id = 2, SubjectName = "S1a" },
                         new Subject() { id = 3, SubjectName = "S1b", Children = { new Subject() { id = 4} } } }
        };

        SubjectViewModel VM = (SubjectViewModel)new SubjectViewModel().InjectFrom<CollectionToCollection>(S); ;


        Assert.AreEqual(2, VM.Children.Count);
        Assert.AreEqual(1, VM.Children.Single(s => s.id == 3).Children.Count);
    }
}


public class Subject {
    public Subject() {
        Children = new List<Subject>();
    }

    public string SubjectName { get; set; }
    public int id { get; set; }

    public List<Subject> Children { get; set; }
}

public class SubjectViewModel {
    public SubjectViewModel() {
        Children = new List<SubjectViewModel>();
    }

    public string SubjectName { get; set; }
    public int id { get; set; }

    public List<SubjectViewModel> Children { get; set; }
}

public class CollectionToCollection : Omu.ValueInjecter.ConventionInjection {
    protected override bool Match(ConventionInfo c) {
        return c.TargetProp.Name == c.SourceProp.Name;
    }

    protected override object SetValue(ConventionInfo c) {
        if (isCollectionMapping(c))
            return (c.SourceProp.Value as IEnumerable<Subject>).Select(s => (SubjectViewModel)(new SubjectViewModel().InjectFrom<CollectionToCollection>(s))).ToList();
        else
            return c.SourceProp.Value;
    }

    private bool isCollectionMapping(ConventionInfo c) {
        return c.SourceProp.Value.GetType().IsInstanceOfType(typeof(IEnumerable<Subject>)) && c.TargetProp.Value.GetType().IsAssignableFrom(typeof(IEnumerable<SubjectViewModel>));

        //return c.SourceProp.Name == "Children" && c.TargetProp.Name == "Children";
    }
}

如果我有一些对象o ,我将如何检查o是否是某种类型实现IEnumerable<string>

很简单:

o is IEnumerable<string>

顺便说一句,你当前的代码没有工作,因为它正在逆转可分配性关系的测试(好像该方法被称为IsAssignableTo ),即它假设:

Bar bar = ...
Foo foo = bar

暗示:

typeof(Bar).IsAssignableFrom(typeof(Foo)) // wrong

实际上,实际意义是:

typeof(Foo).IsAssignableFrom(typeof(Bar))

也就是说,我正在尝试检查任何旧的IEnumerable<>

在这种情况下,您需要测试类型是否实现了通用接口的构造版本:

o.GetType()
 .GetInterfaces()
 .Any(t => t.IsGenericType 
        && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))

这个问题的简单版本是,如果我有一些对象o,我将如何检查o是否是某种类型实现IEnumerable<string>

像这样:

object o = whatever;
bool isSequenceOfStrings = o is IEnumerable<string>;

我可能错过了一些东西(没有阅读你的整个代码示例),但似乎你不需要在这里反思。

如何使用:

if (c.SourceProp.Value is IEnumerable<Subject>)
   return true;

如果您不知道具体类型,请使用泛型:

public bool MyFunction<T>(...)
{
   if (c.SourceProp.Value is IEnumerable<T>)
      return true;
}

或者如果您需要使用界面,请这样做(保存演员表):

var enumerable = c.SourceProp.Value as IEnumerable<string>;
if (enumerable != null)
{
   // Use IEnumerable<string>
   return true;
}

return false;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM