使用Type Class [英] Using Type Class

查看:52
本文介绍了使用Type Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为myclass的类。以及包含

" MyClass"类型元素的arraylist。我想获得MyClass属性的值。 (一个字符串

类型)用于其中一个arraylist元素。


我可以使用:

dim b as string

b = DirectCast(myarraylist(0),myclass).myproperty


但是,我想使用一个对象来定义类型MyClass。喜欢:

dim C as type = type.GetType(myarraylist(0))

b = DirectCast(myarraylist(0),C).MyProperty


但是我得到了一个c类型未定义。我怎么能这样做?


感谢您的帮助。

-

Dennis in Houston

I have a class named "myclass" and an arraylist containing elements of type
"MyClass". I want to get the value of a property of "MyClass" (a string
type) for one of the arraylist elements.

I can get this using:
dim b as string
b = DirectCast(myarraylist(0),myclass).myproperty

However, I want to use an object to define the type "MyClass" like:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0), C ).MyProperty

but I get a "c" type not defined. How can I do this?

Thanks for any help.
--
Dennis in Houston

推荐答案

" Dennis" <德**** @ discussions.microsoft.com> schrieb:
"Dennis" <De****@discussions.microsoft.com> schrieb:
我有一个名为myclass的类。和一个arraylist
包含类型为MyClass的元素。


请注意,''MyClass''是VB.NET编程语言中的关键字。

我想得到属性的值。 MyClass的" (一个字符串
类型)用于其中一个arraylist元素。

我可以使用:
dim b as string
b = DirectCast(myarraylist(0) ,myclass).myproperty

但是,我想使用一个对象来定义类型MyClass。喜欢:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0),C).MyProperty
I have a class named "myclass" and an arraylist
containing elements of type "MyClass".
Notice that ''MyClass'' is a keyword in the VB.NET programming language.
I want to get the value of a property of "MyClass" (a string
type) for one of the arraylist elements.

I can get this using:
dim b as string
b = DirectCast(myarraylist(0),myclass).myproperty

However, I want to use an object to define the type "MyClass" like:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0), C ).MyProperty




\\\

如果TypeOf myarraylist(0)是Foo那么

b = DirectCast(myarraylist(0),Foo).MyProperty

ElseIf ...然后

...

....

结束如果

///


-

Herfried K. Wagner [MVP]

< URL:http:// dotnet。 mvps.org/>



\\\
If TypeOf myarraylist(0) Is Foo Then
b = DirectCast(myarraylist(0), Foo).MyProperty
ElseIf...Then
...
....
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Dennis,


我不能为你提供多少,但是这样可以工作

\\\

公共类示例

Public Shared Sub Main()

Dim myarraylist As New ArrayList

Dim mywhatever As New WindowsApplication1.myclass

myarraylist.Add(mywhatever)

Dim b As String = DirectCast(myarraylist(0),_

WindowsApplication1.myclass).myproperty

如果TypeOf myarraylist(0)是WindowsApplication1.myclass那么

MessageBox.Show(DirectCast(myarraylist(0),_

WindowsApplication1.myclass).myproperty)

结束如果

End Sub

结束班级

公共类[myclass]

公共myproperty字符串

Public Sub New()

myproperty =" Hello World"

End Sub

End Class

///


我不会使用那个myclass,因为它非常混乱,因为它是VBNet中的一个

关键字。

(而且我只使用了一个非常简单的方法来处理一个属性保持简短)


我希望这有点帮助吗?


Cor


丹尼斯 < De **** @ discussion.microsoft.com>

..
Dennis,

I cannot offer you much, however this will work
\\\
Public Class Example
Public Shared Sub Main()
Dim myarraylist As New ArrayList
Dim mywhatever As New WindowsApplication1.myclass
myarraylist.Add(mywhatever)
Dim b As String = DirectCast(myarraylist(0), _
WindowsApplication1.myclass).myproperty
If TypeOf myarraylist(0) Is WindowsApplication1.myclass Then
MessageBox.Show(DirectCast(myarraylist(0), _
WindowsApplication1.myclass).myproperty)
End If
End Sub
End Class
Public Class [myclass]
Public myproperty As String
Public Sub New()
myproperty = "Hello World"
End Sub
End Class
///

I would not use that myclass because it is very confusing because it is a
keyword in VBNet.
(And I used an very easy way for a property only to keep it short)

I hope this helps anyway a little bit?

Cor

"Dennis" <De****@discussions.microsoft.com>
..
我有一个名为myclass的类。以及包含
MyClass类型元素的arraylist。我想获得MyClass属性的值。 (一个字符串
类型)用于其中一个arraylist元素。

我可以使用:
dim b as string
b = DirectCast(myarraylist(0) ,myclass).myproperty

但是,我想使用一个对象来定义类型MyClass。喜欢:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0),C).MyProperty

但我得到了一个 ; C"类型未定义。我该怎么做?

感谢您的帮助。

-
丹尼斯在休斯敦
I have a class named "myclass" and an arraylist containing elements of type
"MyClass". I want to get the value of a property of "MyClass" (a string
type) for one of the arraylist elements.

I can get this using:
dim b as string
b = DirectCast(myarraylist(0),myclass).myproperty

However, I want to use an object to define the type "MyClass" like:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0), C ).MyProperty

but I get a "c" type not defined. How can I do this?

Thanks for any help.
--
Dennis in Houston



Dennis,
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0),C).MyProperty


你不能基于Type变量进行强制转换,DirectCast在编译时需要类型名称

(myclass)。


如果你真的想让AnyProperty形成一个AnyClass的ArrayList,那么

" easist"方法是使用CallByName


Dim o As Object = CallByName(list(0)," MyProperty",CallType.Get)

或者我将使用一个PropertyDescriptor或反射。


类似于:


Dim properties As System.ComponentModel.PropertyDescriptorCollection

properties =

System.ComponentModel.TypeDescriptor.GetProperties(list(0))

Dim o As Object = properties(" MyProperty")。GetValue(list( 0))


但是我通常只在更高级的情况下使用PropertyDescriptor ...


希望这有助于

Jay


" Dennis" <德**** @ discussions.microsoft.com>在留言中写道

新闻:1F ********************************** @ microsof t.com ...我有一个名为myclass的类。以及包含
MyClass类型元素的arraylist。我想获得MyClass属性的值。 (一个字符串
类型)用于其中一个arraylist元素。

我可以使用:
dim b as string
b = DirectCast(myarraylist(0) ,myclass).myproperty

但是,我想使用一个对象来定义类型MyClass。喜欢:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0),C).MyProperty

但我得到了一个 ; C"类型未定义。我该怎么做?

感谢您的帮助。

-
丹尼斯在休斯敦
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0), C ).MyProperty
You cannot cast based on a Type variable, the DirectCast needs the name
(myclass) of the type at compile time.

If you truly want to get AnyProperty form an ArrayList of AnyClass, the
"easist" way is to use CallByName

Dim o As Object = CallByName(list(0), "MyProperty", CallType.Get)
Alternatively I will use a PropertyDescriptor or Reflection.

Something like:

Dim properties As System.ComponentModel.PropertyDescriptorCollection
properties =
System.ComponentModel.TypeDescriptor.GetProperties (list(0))

Dim o As Object = properties("MyProperty").GetValue(list(0))

However I normally only use PropertyDescriptor in more advanced cases...

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...I have a class named "myclass" and an arraylist containing elements of type
"MyClass". I want to get the value of a property of "MyClass" (a string
type) for one of the arraylist elements.

I can get this using:
dim b as string
b = DirectCast(myarraylist(0),myclass).myproperty

However, I want to use an object to define the type "MyClass" like:
dim C as type = type.GetType(myarraylist(0))
b = DirectCast(myarraylist(0), C ).MyProperty

but I get a "c" type not defined. How can I do this?

Thanks for any help.
--
Dennis in Houston



这篇关于使用Type Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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