Adding shadowing properties of a class at runtime

V

Vladimir Davidov

Hello community!

I wonder is there any way to create new fields/properties for the objects at
runtime, let's say I have e form, on which I have 10 controls, and at
runtime I want to add new data fields and/or properties to the object, how
could I do it? Furthermore is it possible, and if yes, then how, could I
override and shadow existing properties and methods in a runtime with my
own? Please do not suggest inheriting controls and adding those
properties/fields in inherited control, this wont do, I need to do it at
runtime.



Vladimir
 
J

Jay B. Harlow [MVP - Outlook]

Vladimir,
Have you looked at the System.ComponentModel.IExtenderProvider interface?

This allows you to create an object that is able to add properties to a
second object, without modifying (inheriting) the second object's class.

Of course the IExtenderProvider object itself has a fixed set of properties.

Realistically you will need to use inheritance to have a place to store the
properties.

You could inherit from the control in question and add a "Dynamic
Properties" property. I would implement the "Dynamic Properties" property as
an indexed property that uses a Hashtable to store the values.

Something like:

Public Class MyControl
Inherits TextBox

Private Readonly m_properties As New HashTable

Public Property DynamicProperties(index As String) As Object
Get
Return m_properties(index)
End Get
Set(value As Object)
m_properties(index) = value
End Set
End Property

End Class

A third way is to implement the ICustomTypeDescriptor (possible in
conjunction with the Dynamic Properties method) that allows you to add
properties that other framework classes then recognize (such as the Property
Grid).

Hope this helps
Jay

"Vladimir Davidov" <vladimirs.davidovs[at]microlink.lv (replace at in square
brackets with @)> wrote in message
news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top