在ObservableCollection中获取项目类型 [英] Get type of items in ObservableCollection

查看:225
本文介绍了在ObservableCollection中获取项目类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图(由一个listview组成)绑定到一些observable集合。换句话说,我有:

I have a view (consists of a listview) that is binded to some observable collection. In other words I have:

var someCollection = new ObservableCollection<ClassFoo>();
// initialize someCollection

ListView someView = new ListView();
someView.DataContext = someCollection;
someView.ItemsSource = someCollection;

注意:


  • someView 位于 Project2上的 Project1.dll someCollection .dll
  • Project2.dll 引用 Project1.dll Project1.dll 没有对 Project2.dll 的引用

  • someView is located on Project1.dll and someCollection on Project2.dll
  • Project2.dll has a reference to Project1.dll AND Project1.dll DOES NOT have a reference to Project2.dll

因此在我的视图中, someCollection类型 ObservableCollection< object> ,因为我将得到一个编译错误,如果它的实际类型 ObservableCollection< ClassFoo> 因为我必须添加一个引用Project2.dll。

Therefore on my view I have a reference to someCollection of type ObservableCollection<object> because I will get a compilation error if it where to be of its actual type ObservableCollection<ClassFoo> because I would have to add a reference to Project2.dll.

由于某种原因,我不能添加该引用,我的老板要我创建命令等。 ..

For some reason I cannot add that reference and I my boss wants me to create commands etc...

最后一部分只是解释为什么我想这样做,但总之我在寻找:

The last part just explains why I want to do this but in short I am looking for:

  ObservableCollection<object> myUnknownObservableCollection = someReference;
  // I know that someReferce is of type ObservableCollection<ClassFoo>
  var x = myUnknownObservableCollection.GetType().GetTypeOfItems.....

end我将喜欢x等于 typeof(ClassFoo)如何使用反射,假设someReference是类型 ObservableCollection< ClassFoo> / code>?

at the end I will like x to be equal to typeof(ClassFoo) how can I do that with reflection given that someReference is of type ObservableCollection<ClassFoo>?

得到解决方案!这是:

    class Person 
    {
        public string Name { get { return "Antonio"; } }
    }

    .. 

    // view code:

    IEnumerable<object> uncknownObject;

    // view model does this:
    uncknownObject = new ObservableCollection<Person>( );

    // continuation of view code:

    var observCol = uncknownObject.GetType( );

    var x = ( ( dynamic )observCol ).GenericTypeArguments[ 0 ]; 

    var instance = ( Person )Activator.CreateInstance( x );

    Console.WriteLine( instance.Name ); // Print Antonio!!!

这将是很高兴能够做到没有动态数据类型

It will be nice to be able to do it without the dynamic data type though

这里是一个运行.net 4.0而不使用动态类型的解决方案

推荐答案

如果你打算使用mvvm,那么动态类型将无法帮助绑定。
您可能必须使用SLaks建议的界面

If you are planning to use mvvm, then dynamic type will not help in binding. You might have to use an interface as SLaks suggested

public interface IClassFoo
{
   string Name { get; }
}

public class ClassFoo : IClassFoo
{
    public string Name { get { return "Antonio"; } }
}

//Usage 
var someCollection = new ObservableCollection<IClassFoo>();

这篇关于在ObservableCollection中获取项目类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆