IEditableCollectionView 添加的对象是什么类型?
使用 IEditableCollectionView addNew() 方法添加对象非常不错。但是我不确定它与我的通用代码的配合效果如何。
我有一个基类的 ObservableCollection。根据用户想要看到的内容,可以用 DerivedA 或 DerivedB (或多个其他派生类型类)填充。它的类型永远不会超过 1。我对此有以下问题,其中第一个问题是主要问题。
1)调用addNew()方法时添加什么类型的对象。我猜是有根据的,但不确定。如果 Base 是抽象的怎么办?无论如何,我是否可以确保它添加特定类型,或者是否已实现它添加了最相关的类型?
2)在使用派生类时,我是否会遇到 ObservableCollection 的麻烦?
3) 派生类都单独实现 IEditableObject,但 Base 没有。我的愚蠢是错误的,因为我认为当你实现时,你必须定义方法。我可以将它们定义为抽象,然后在派生类中重写它们,对吧?如果我不这样做,我会遇到 IEditableCollection 的麻烦。
更新:
我成功修复了项目 3)基类实现 iEditableObject 并将抽象虚拟方法抛出到它的派生类。即使列表中充满了派生项,addNew 方法也不起作用。
我见过一种半解决方案。 .Net Framework 4.0 实现了一个名为 AddNewItem 的新方法,您可以在其中定义要添加的项目。应该使用非无参数构造函数和基类来解决问题。但是我正在 3.5 中工作,
我能做些什么吗?
Adding objects with the IEditableCollectionView addNew() method is pretty decent. However I'm not sure how well it works with my generic code I have.
I have a ObservableCollection of my base class. Depending on what the user wants to see it can be filled with DerivedA or DerivedB ( or mutliple other derived type classes ). It never has a more then 1 type. I have the following questions about this with number 1 being the main question.
1) What type of object is added when the addNew() method is called. I'm guessing base but not sure. What if Base is abstract? Is there anyway I can ensure it adds a specific type or is it implemented that it adds the most relevent type already?
2) Am I asking for trouble having ObservableCollection when using derived classes?
3) The derived classes all Implement IEditableObject individually however Base does not. My stupidity as at fault because I thought when you implement you would have to define the methods. I can just define them as abstract and then override them in the Derived classes right? If I don't do this will I get into trouble with IEditableCollection.
UPDATE:
I succesfully fixed item 3) base class implements iEditableObject and the throws the abstract virtual methods down to it's derived classes. The addNew method dosesn't work, even if the list is filled with derived item.
I've seen one semi solution. .Net framework 4.0 implements a new method called AddNewItem where you can define the item to be added. Should solve the problem with non parameterless constructer and base classes. However I'm working in 3.5
Anything I can do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于此类问题,最好记住 .net 框架代码不是黑匣子,您可以使用 ILSpy,查看它正在做什么,或者 Microsoft 如何让某些东西发挥作用。我建议您查看 4.0 方法,看看您是否可以在 3.5 中出于自己的目的复制它,并将其添加为扩展方法。
For this type of issue, it's nice to remember that the .net framework code is not a black box, you can disassemble it with ILSpy, to view what it's doing, or how Microsoft got something to work. I'd suggest you look at the 4.0 method, and see if you can't duplicate it for your own purposes in 3.5, and add it as an extension method.