在运行时动态添加Class属性 [英] Add a Class property dynamically at run-time

查看:90
本文介绍了在运行时动态添加Class属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有办法在运行时添加类属性?

如果是这样,怎么样?

任何代码示例?


***通过开发人员指南 http: //www.developersdex.com ***

解决方案

不,我不这么认为。并且,如果可以的话,你会添加代码,再次

这是一个坏主意。


你当然可以使用某种类型的集合,并添加一个

集合,该集合将返回一个值


dim colTest作为新集合

colTest.Add 1,one

colTest.Add 10,ten


msgbox coltest(" ten")

>
以上将返回10


并且,您可以制作一个公共的集合。在一个类对象中。所以你可以在
运行时确实为集合添加新值,并检索它们


clsMyClassObject.MyCollection(" apple")

clsMyClassObject.MyCollection(banana)


所以,可以考虑一下我们班的

对象的apple和banana新属性,但是真的,你只是在这里使用一个系列....

-

Albert D. Kallal(访问MVP)

埃德蒙顿,艾伯塔省加拿大
pl ************* ****@msn.com
http:// www.members.shaw.ca/AlbertKallal


2005年11月18日星期五03:10:12 GMT,Larry< do ***** ***@nospamhere.com>写道:



有没有办法在运行时添加类属性?
如果是这样,怎么样?
任何代码示例?

***通过Developersdex发送 http://www.developersdex .com ***




对于所有意图和目的,不,你不能 - 但是,由于大多数原因,我可以

想到你想要这样做,我可以想到其他编码模式来实现同样的事情。你想要完成什么?


另外,如果你确实需要生成代码,但生成

生成的代码没有在正在生成的项目中,可以使用自动化来完成
。我有理由做这样的事情

并且已经成功完成了。


你可以做到但它过得很快面对COM编程。


COM编程完全是关于接口和合同接口

在COM服务器和COM客户端之间创建。如果你可以随意地向一个类模块添加方法,那么你正在改变合同,即COM条款中不可接受的



为什么你不解释你的问题而不是建议

解决方案并询问如何做。

-

Terry Kreft


" Larry" <做******** @ nospamhere.com>在消息中写道

新闻:oC *************** @ news.uswest.net ...



有没有办法在运行时添加类属性?
如果有,怎么样?
任何代码示例?

***通过Developersdex发送< a rel =nofollowhref =http://www.developersdex.comtarget =_ blank> http://www.developersdex.com ***





is there a way to add a class property an run-time?
If so, how?
any code samples?

*** Sent via Developersdex http://www.developersdex.com ***

解决方案

No, I don''t think so. And, if you could, you would be adding code, and again
that is a bad idea.

You certainly can use a collection of some type, and add a to that
collection, and that collection would return a value

dim colTest as new Collection
colTest.Add 1,"one"
colTest.Add 10,"ten"

msgbox coltest("ten")

The above would return 10

And, you can make a collection "public" in a class object. So you could at
runtime certainly add new values to a collection, and retrieve them

clsMyClassObject.MyCollection("apple")
clsMyClassObject.MyCollection("banana")

So, could sort of consider apple, and banana new properties of our class
object, but really, you are just using a collection here....
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal


On Fri, 18 Nov 2005 03:10:12 GMT, Larry <do********@nospamhere.com> wrote:



is there a way to add a class property an run-time?
If so, how?
any code samples?

*** Sent via Developersdex http://www.developersdex.com ***



For all intents and purposes, no you can''t - however, for most reasons I can
think of that you''d want to do that, I can think of other coding patterns to
accomplish the same thing. What are you trying to accomplish?

Also, if you really do need to generate code, but the code that does the
generating doesn''t have to be in the project that''s being generated, that can
certainly be done using automation. I''ve had reason to do things like that
and have done it successfully.


You may be able to do it but it flies in the face of COM programming.

COM programming is all about interfaces and the contract an interface
creates between the COM server and the COM client. If you can arbitrarily
add methods to a class module then you are changing the contract, that is
unacceptable in COM terms.

Why don''t you explain what your problem is rather than suggesting the
solution and asking how to do it.
--
Terry Kreft

"Larry" <do********@nospamhere.com> wrote in message
news:oC***************@news.uswest.net...



is there a way to add a class property an run-time?
If so, how?
any code samples?

*** Sent via Developersdex http://www.developersdex.com ***



这篇关于在运行时动态添加Class属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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