简体   繁体   English

访问基类集合中的派生类属性

[英]Access Derived class properties from base class collection

I have created a derived class from a base class and have added the derived class objects to the base class collection.Later when i try to cast the collection objects to derived class,it is throwing error and i am not able to get my override methods and properties.How could i get around this.? 我已经从基类创建了派生类,并将派生类对象添加到基类collection。稍后,当我尝试将集合对象强制转换为派生类时,它会引发错误,并且我无法获取覆盖方法和属性。我该如何解决?

I am trying to override the stroke class in wpf inkcanvas.But the collection available is of base ink collection.So after serializing ans desalinizing,the new properties that i added is not accessible.Pls help 我试图覆盖wpf墨水画布中的笔触类。但是可用的集合是基础墨水集合。因此,在序列化和脱盐之后,无法访问我添加的新属性。请帮助

Your problem is that StrokeCollection implements a custom TypeConverter which serializes the stroke data in an efficient binary format called Ink Serialization Format (ISF) using the StrokeCollection.Save() method. 您的问题是StrokeCollection实现了一个自定义的TypeConverter ,它使用StrokeCollection.Save()方法以一种称为墨水序列化格式 (ISF)的有效二进制格式对笔划数据进行了序列化 When your strokes are converted to ISF all the extra data is lost, so when they are reloaded they are instantiated as ordinary Stroke objects (not your subclass). 当您的笔划转换为ISF时,所有额外的数据都将丢失,因此在重新加载它们时,它们将被实例化为普通的Stroke对象(而不是您的子类)。 This is why you cannot cast them to your subclass. 这就是为什么您不能将它们强制转换为子类的原因。

Some of your options are: 您的一些选择是:

  • Subclass StrokeCollection and implement a new TypeConverter for your subclass 子类StrokeCollection并为子类实现新的TypeConverter
  • Use a different serialization technique that ignores the TypeConverter 使用另一种忽略TypeConverter序列化技术
  • Manually serialize your StrokeCollection (eg. by copying the Strokes into a List<Stroke> and serializing that) 手动序列化您的StrokeCollection (例如,通过将Strokes复制到List<Stroke>并对其进行序列化)

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

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