如何解决“对象必须实现IConvertible”。 [英] How to solve "Object must implement IConvertible."

查看:1717
本文介绍了如何解决“对象必须实现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


http://forums.asp.net/t/1657293.aspx/1 [ ^ ]



查看此链接..希望它会有所帮助......
http://forums.asp.net/t/1657293.aspx/1[^]

check this link..hope it will help...


这篇关于如何解决“对象必须实现IConvertible”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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