使用反射检查IEnumerable <T>

Ada*_*kis 14 c# reflection valueinjecter

编辑

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

结束编辑

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

我试过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";
    }
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*Ani 30

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

很简单:

o is IEnumerable<string>
Run Code Online (Sandbox Code Playgroud)

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

Bar bar = ...
Foo foo = bar
Run Code Online (Sandbox Code Playgroud)

暗示:

typeof(Bar).IsAssignableFrom(typeof(Foo)) // wrong
Run Code Online (Sandbox Code Playgroud)

实际上,实际意义是:

typeof(Foo).IsAssignableFrom(typeof(Bar))
Run Code Online (Sandbox Code Playgroud)

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

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

o.GetType()
 .GetInterfaces()
 .Any(t => t.IsGenericType 
        && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
Run Code Online (Sandbox Code Playgroud)

  • 注意记住字符串类型是IEnumerable (3认同)

Eri*_*ert 10

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

像这样:

object o = whatever;
bool isSequenceOfStrings = o is IEnumerable<string>;
Run Code Online (Sandbox Code Playgroud)

  • @Adam:看看你的代码,我不明白为什么你觉得你需要使用反射.有什么问题"(c.SourceProp.Value是IEnumerable <Subject>)&&(c.TargetProp.Value是IEnumerable <SubjectViewModel>)"??? (2认同)

Jos*_*h G 5

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

如何使用:

if (c.SourceProp.Value is IEnumerable<Subject>)
   return true;
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

return false;
Run Code Online (Sandbox Code Playgroud)