Constructing Dynamic Properties at Runtime in VB .NET
Solution 1
The answer is Reflection.Emit. Not a lot of fun to code, but does what you want.
Solution 2
If you just want a dynamic list of variables, you can always set up a dictionary object as a member of your class, and then set or get a particular dictionary item with a method.
Solution 3
Adding further to my comment, you can add an indexer to your class - which can let you get/set member variable.
EDIT: I am sorry. I didn't know that vb.net doesn't have indexers.
But one can still write code with backing dictionary, which can work like an indexer
Solution 4
Unfortunately there is no way to do alter the structure of a class an runtime. Metadata is fixed at compile time and runs unaltered at runtime.
For the Nitpickers :)
This is not 100% true. The profiling and ENC APIs allow you to change the structure of metadata at runtime. But neither are really applicable for this scenario.
Larry Watanabe
I've done research in artificial intelligence, machine learning, specifically constructive induction and structural decision trees, worked for a variety of companies such in business intelligence, forms automation, and banking. http://www.google.com/profiles/larrywatanabe , http://stackoverflow.com/users/118860/larry-watanabe , http://larrywatanabe.blogspot.com , http://portal.acm.org/citation.cfm?id=125337 , http://www.programmersheaven.com/user/larrywatanabe/ , http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/w/Watanabe:Larry.html , http://portal.acm.org/citation.cfm?id=211243 , http://www.interaction-design.org/references/authors/larry_watanabe.html Last activity , http://www.aaai.org/Papers/AAAI/1990/AAAI90-131.pdf , http://www.wipo.int/pctdb/en/wo.jsp?wo=2004107121
Updated on June 05, 2022Comments
-
Larry Watanabe almost 2 years
Is there a way to dynamically create properties at runtime in VB .NET using introspection?
e.g. Suppose I had a class
Public Class Foo Public Property Bar() As String get ... end get set(ByVal value As String) ... end set End Class
Is there a way to create property Bar at runtime?
Thanks!