从String转换为自定义对象 [英] Casting from String to custom object

查看:445
本文介绍了从String转换为自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个简单的类,如下所示:
公共类Person
Public Sub New(以字符串形式的名称)名称为字符串



















("Joe Schmoe",Person)

Person类只是一个简单的示例,需要CType起作用的真正原因是我正在编写一个自定义列表框,而VS 2005尝试转换字符串

Say you have a simple class somewhat like this:
Public Class Person
    Public Sub New(Name as string)
       me.Name = Name
    End Sub

    Public Name as string
    Public Age as integer
End Class

I want to be able to CType from string to my Person class.
I want this to work: Dim person as person = CType("Joe Schmoe", Person)

The Person class is just a simple example, the real reason I need CType to work is that I'm writing a custom listbox, and VS 2005 tries to cast a string to my custom ListItem class.

推荐答案

您将需要实现类型转换器.在msdn上有一个示例,很好地解释了其工作原理:

You'll need to implement a Type Convertor. There an example on msdn which explains pretty good how it works:


这篇关于从String转换为自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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