在未知类中设置属性 [英] Setting properties in unknown classes

查看:60
本文介绍了在未知类中设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在设计时设置一个你不知道的类属性的最佳方法是什么?

这些类没有相同的基类,但它们都是扩展的。具有相同的属性,方法,....

实际上它是继承自Typed DataSet的类。

我需要设置MyProperty。在tdsExtended1,tdsExtended2,tdsExtended3,....同时itteratingDataSetCollection


当然,最好的方法是拥有一个BaseClassMyTypedDataSet。然后在其中实现由VisualStudio生成的TypedDataSets,但我不认为这是可能的。


我想我必须使用类似的东西:

Dim pi As PropertyInfo = t.GetProperty([PropertyName])


如果不是pi则什么都没有


pi.SetValue(什么,没有,没有什么)


结束如果

但是我无法得到它。 (我猜这是第一个参数)


TIA,

Michael

Hi,

What would be the best way to Set a property of a class which you don''t know at design-time which that would be?
The classes haven''t got the same base-class, but they''re all "extended" with the same properties, methods, ....
In fact it are classes inheriting from Typed DataSets.
I need to set "MyProperty" on tdsExtended1, tdsExtended2, tdsExtended3, .... while itterating the "DataSetCollection"

Of course the best approach would be to have a BaseClass "MyTypedDataSet" and then implement the TypedDataSets Generated by VisualStudio in it, but I don''t think that''s possible.

I think I have to use something like:
Dim pi As PropertyInfo = t.GetProperty([PropertyName])

If not pi Is Nothing Then

pi.SetValue(Nothing, True, Nothing)

End If
But I can''t get it work. (I guess it''s the first parameter)

TIA,
Michael

推荐答案

Michael


我认为问题出在SetValue上,是GetProperty工作


尝试:theClass .GetType.GetProperty(pi.Name)。SetValue(Class,objNewValue,Nothing


希望它有帮助

Chris


----- Michael Maes写道:----





什么是最好的设置方式在设计时你不知道的一个类的属性,这将是

这些类没有相同的基类,但它们都是扩展的;具有相同的属性,方法,......

实际上它是继承自Typed DataSet的类

我需要在tdsExtended1,tdsExtended2,tdsExtended3上设置MyProperty ,....当itteratingDataSetCollection


当然最好的方法会要有一个BaseClassMyTypedDataSet然后在其中实现由VisualStudio生成的TypedDataSets,但我不认为这是可能的


我想我必须使用像
$ b $这样的东西b Dim pi As PropertyInfo = t.GetProperty([PropertyName]


如果不是pi则没有什么


pi.SetValue(Nothing,True什么都没有


结束我

但是我不能让它工作。(我想这是第一个参数


TIA

Michae

Michael

I think the problem lies in the SetValue, is the GetProperty working

Try: theClass.GetType.GetProperty(pi.Name).SetValue(the Class, objNewValue, Nothing

Hope it helps
Chris

----- Michael Maes wrote: ----

Hi

What would be the best way to Set a property of a class which you don''t know at design-time which that would be
The classes haven''t got the same base-class, but they''re all "extended" with the same properties, methods, ...
In fact it are classes inheriting from Typed DataSets
I need to set "MyProperty" on tdsExtended1, tdsExtended2, tdsExtended3, .... while itterating the "DataSetCollection

Of course the best approach would be to have a BaseClass "MyTypedDataSet" and then implement the TypedDataSets Generated by VisualStudio in it, but I don''t think that''s possible

I think I have to use something like
Dim pi As PropertyInfo = t.GetProperty([PropertyName]

If not pi Is Nothing The

pi.SetValue(Nothing, True, Nothing

End I
But I can''t get it work. (I guess it''s the first parameter

TIA
Michae


嗨克里斯,

不,GetProperty没有工作。

你的小费不能更好。


sub是ComponentChangeService中事件的一部分 - 课程


我相信问题就是上课了。


这里是代码:

Private Sub OnComponentAdded(ByVal sender As Object,ByVal ce As

ComponentEventArgs)


尝试


Dim ot As Object = ce.Component


Dim t As Type = ot.GetType


Dim pi As PropertyInfo


pi = t.GetProperty(" PropertyName")

pi.SetValue(t,False,Nothing)


''额外代码...... ..


Catch ex As Exception


最后


MsgBox(res)


结束尝试


End Sub


" Chris Podmore" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:6B ********************************** @ microsof t.com ...
Hi Chris,

No, the GetProperty isn''t working eigther.
Your tip doesn''t work neighter.

the sub is part of an event in the ComponentChangeService-Class

I beleive the issue is getting the class.

here''s the code:
Private Sub OnComponentAdded(ByVal sender As Object, ByVal ce As
ComponentEventArgs)

Try

Dim ot As Object = ce.Component

Dim t As Type = ot.GetType

Dim pi As PropertyInfo

pi = t.GetProperty("PropertyName")

pi.SetValue(t, False, Nothing)

'' Extra code .....

Catch ex As Exception

Finally

MsgBox(res)

End Try

End Sub

"Chris Podmore" <an*******@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Michael,

我认为问题在于SetValue,GetProperty是否有效?

尝试:theClass。 GetType.GetProperty(pi.Name)。SetValue(Class,objNewValue,
Nothing)
希望它有所帮助。
Chris。

-----迈克尔Maes写道:-----

你好,

设置一个你不想知道的课程属性的最佳方法是什么?在设计时,那将是什么?这些类没有相同的基类,但它们都是
扩展。具有相同的属性,方法,....实际上它是继承自Typed DataSet的类。
我需要设置MyProperty。在tdsExtended1,tdsExtended2,
tdsExtended3,....同时itteratingDataSetCollection
当然最好的方法是拥有一个BaseClass
MyTypedDataSet。然后在其中实现由

VisualStudio生成的TypedDataSets,但我不认为这是可能的。
我想我必须使用类似的东西:
Dim pi作为PropertyInfo = t.GetProperty([PropertyName])

如果不是pi什么都没有那么

pi.SetValue(没什么,没有,没什么)

结束如果

但我无法解决问题。 (我猜这是第一个参数)

TIA,

Michael
Michael,

I think the problem lies in the SetValue, is the GetProperty working?

Try: theClass.GetType.GetProperty(pi.Name).SetValue(the Class, objNewValue, Nothing)
Hope it helps.
Chris.

----- Michael Maes wrote: -----

Hi,

What would be the best way to Set a property of a class which you don''t know at design-time which that would be? The classes haven''t got the same base-class, but they''re all "extended" with the same properties, methods, .... In fact it are classes inheriting from Typed DataSets.
I need to set "MyProperty" on tdsExtended1, tdsExtended2, tdsExtended3, .... while itterating the "DataSetCollection"
Of course the best approach would be to have a BaseClass "MyTypedDataSet" and then implement the TypedDataSets Generated by
VisualStudio in it, but I don''t think that''s possible.
I think I have to use something like:
Dim pi As PropertyInfo = t.GetProperty([PropertyName])

If not pi Is Nothing Then

pi.SetValue(Nothing, True, Nothing)

End If
But I can''t get it work. (I guess it''s the first parameter)

TIA,
Michael



Michael


尝试以下单步执行代码,看看是否得到了正确的结果。是不是正确设置开始

让我知道你是怎么上的


克里斯


昏暗的ot As Object = ce.Component''< ===不等于你期望的类

Dim pp()As PropertyInfo = ot.GetType.GetProperties(

Dim p作为PropertyInf

Dim blnFound As Boolean = Fals

For each p in p

如果p.Name.ToLower =" PropertyName" The

blnFound = Tru

退出Fo

结束我

Nex

如果blnFound

ot.GetType.GetProperty(p.Name)。SetValue(ot,objNewValue,Nothing

结束我
Michael

Try the following stepping through the code to see if you are getting the correct results. Is ot being set correctly to start with
Let me know how you get on

Chris

Dim ot As Object = ce.Component ''<=== Does ot equal the class you expect
Dim pp() As PropertyInfo = ot.GetType.GetProperties(
Dim p As PropertyInf
Dim blnFound As Boolean = Fals
For Each p In p
If p.Name.ToLower = "PropertyName" The
blnFound = Tru
Exit Fo
End I
Nex
If blnFound The
ot.GetType.GetProperty(p.Name).SetValue(ot, objNewValue, Nothing
End I


这篇关于在未知类中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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