c# - 使用反射检查 IEnumerable<T>

标签 c# reflection valueinjecter

编辑

这个问题的基本版本是,如果我有一些 object o , 我将如何检查 o 是否属于实现 IEnumerable<string> 的某种类型有反射(reflection)?最初的问题要具体得多,但对上述问题的回答也一样好。对不起,如果我在这个问题上给出了太多的细节

结束编辑

以下是一个设计的 ValueInjecter POC。除 isCollectionMapping 外一切正常方法在最底层。当且仅当源属性和目标属性都是实现 IEnumerable<respectiveTypes> 的任何对象时,我试图让它返回 true .

我试过了 IsAssignableFrom还有IsInstanceOfType , 但似乎都不起作用。

自从我取消注释方法的第二行以显式检查名称“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";
    }
}

最佳答案

If I have some object o, how would I check to see if o is of some type that implements IEnumerable<string>?

简单如:

o is IEnumerable<string>

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

Bar bar = ...
Foo foo = bar

暗示:

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

实际上,实际的含义是:

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

Namely, I'm trying to check for any old IEnumerable<>:

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

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

关于c# - 使用反射检查 IEnumerable<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5567583/

相关文章:

c# - 获取 chrome 的控制台日志

c# - .net 应用程序的链接器

c# - 列表计数和 null 的条件

c# - 复制 nancyfx 响应的 cookie 和 header ,无需 foreach

c# - ValueInjecter:如何在执行 .InjectFrom<UnflatLoopValueInjection>(data) 时忽略某些属性?

c# - 在对象初始值设定项中的嵌入式集合上使用集合初始值设定项时没有警告

Java:来自字节码的新实例

c# - 如何访问运行方法的名称?

java - 获取Java中所有公共(public)变量的名称和值(包括继承对象的)

c# - 领域模型映射