Hi, HashCode. Tell me, please, how can I add properties to the class in runtime? For example, I get a Dictionary or HashTable as input, and from the keys that they have, generate properties that would return key values. Is it possible? Thank.
|
2 answers
Dynamically adding properties is quite a laborious process using .NET3.0. As already noted, it is also difficult to turn to the methods through reflection. The C # language is strongly typed, although there has been some recent shift in emphasis. If you are still interested, then look towards ExpandObject. Here is a small link to stackoverflow.com
- The emphasis cannot be shifted.
C#was a statically typed language with strong typing, and it remains, anddynamicis just syntactic sugar. - Costantino Rupert - I would not say that DLR is syntactic sugar - Specter
- @Spectre Well, I certainly exaggerate, but it just seems to me that adding a special set of metadata and methods to working with this set should not be called a shift in emphasis. - Costantino Rupert
- All the same,
DLRis not the same asExpandoObject. Runtime, through which a dynamic language likepythoncan be interpreted, is clearly more than just syntactic sugar. Although, I think, to call the emergence ofDLRoffsetC#also not true. Rather, the entire.NETplatform as a whole, rather thanC#. - Ilya Pirogov - Yes, it is not because of what is fashionable, it is rather a necessity. Strong typing will still remain the priority for me - Specter
|
And where are you going to use it? If with components like PropertyGrid , then implement in your ICustomTypeDescriptor object.
|
dynamic- Specter