如何解决“对象必须实现IConvertible”。

编程入门 行业动态 更新时间:2024-10-21 03:39:17
本文介绍了如何解决“对象必须实现IConvertible”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在尝试将一个列表对象转换为其他时遇到错误。 以下是我的代码: ddlListObj.Add( (T)Convert.ChangeType(lstobjUni,typeof(T))); 即使lstobjUni的类型与特定实例的T相同。 有人可以帮忙吗。

I am getting error while trying to convert one list object to other. below is my code: ddlListObj.Add((T)Convert.ChangeType(lstobjUni, typeof(T))); Even when type of lstobjUni is same as T for particular instance. Can someone please help.

推荐答案

你必须在对象上实现IConvertable Covert.ChangeType()需要IConvertable对象。因此,Convert对象知道如何将给定对象转换为您需要的类型 - 即使它是相同的类型。您可能希望研究面向对象编程中的多态性,以便更好地理解为什么方法结构需要IConvertable 。 主要思想是你的对象可能有多种形式的解释。例如,如果你有和Apple对象,苹果对象可以解释为水果(IFruit),可食用(IEatable)和圆形(IShape)。这些标识以不同的方式定义您的苹果对象,尽管它们是同一个对象。现在,当一家杂货店看着你的苹果时,它们会放在水果过道里(不关心它的形状)。当图形设计看苹果时,这是相同的,他们只关心如何呈现苹果的形状。 所以当你使用Convert对象时。只要该对象可以转换为另一个对象(通过实现IConvetable),它并不关心它是什么对象。当你继承IConvertable时,你只需要实现从给定对象到新对象的转换方法。 以下是如何使用IConvertable的参考资料 you have to implement IConvertable on the object because Covert.ChangeType() expects IConvertable object. So Convert object knows how to convert given object to the type that you need - even it is the same type. you may want to research into Polymorphism in object-oriented programming for a better understanding of why the structure of method require IConvertable. The main idea is that your object may have multiple forms of interpretation. For example, if you have and Apple object, the apple object can interpreted as a fruit (IFruit), eatable (IEatable), and round-shape(IShape). These identifications define your apple object in different way, though it is the same object. Now when a grocery store look at your apple, they will place in the fruit aisle (not caring which shape it would be). This is same when an graphic design look at the apple, they would only care how would render the shape of the apple. So when you use Convert object. It does not really care what object it is as long as that object is convertible to another (by implementing IConvetable). And as you inherite IConvertable, you just have to implement method of conversion from given object to the new object. Here is a reference to how to use IConvertable

forums.asp/t/1657293.aspx/1 [ ^ ] 查看此链接..希望它会有所帮助...... forums.asp/t/1657293.aspx/1[^] check this link..hope it will help...

更多推荐

如何解决“对象必须实现IConvertible”。

本文发布于:2023-10-25 08:43:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   对象   IConvertible

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!