0

Few years ago in create-class-dynamically-at-runtime

Has been explained how to dynamically create a class using CreateClass function. Function works fine, and I successfully enumerated properties of newly created class.

I know that I can create single instance of newly created class using

        Dim varNewType = CreateClass("clsTest2", TypeDict)
    Dim NewVar = Activator.CreateInstance(varNewType)

However, I don’t have I clue how to access its properties, how to set them or get their values.

Also, I need to create a list of this new created type. So please, if anyone has any idea, do share.

Thanks in advance.

Community
  • 1
  • 1
  • You would use Reflection to access members and, in the case of properties, their values. – jmcilhinney Apr 17 '17 at 02:56
  • Hi jmcilhinney, I did realize that, but I’m not sure how to do that. If you would please put just few rows of code so I would know what objects and methods from Reflection I have to use. Also, if you have any Idea how to instantiate new list(of newtype), I would be very greatful. – Ivica Nesic Apr 17 '17 at 14:58
  • You need to do your own research on Reflection first. Try what you think is required and then, if it doesn't work, post what you have and tell us what happened. – jmcilhinney Apr 17 '17 at 15:37

1 Answers1

0

Since you are using VB code, then its easy to access the methods and properties directly.

e.g.:

Dim varNewType = CreateClass("clsTest2", TypeDict)
Dim NewVar as object = Activator.CreateInstance(varNewType)

Then, in case it has a method Meth1, and a property Prop1, you can do thefollowing:

NewVar.Meth1(any params...)
Dim obj as object = NewVar.Prop1

No need for reflection or casting!

Abusnake
  • 168
  • 11
  • Hi Abusnake, Thanks for your answer during weekend. However, your solution doesn’t work here; this isn’t something I wouldn’t remember to try myself. If you opened the link I had provided in question, you’d see that function made by someone smarter than me actually creates new type dynamically during runtime. At the design time, instance variable of that type is actually of type object. You cannot reach any of its properties through VS at the design time, because at that moment they are not yet created. I need to know how to access those properties, and how to create list of that type. – Ivica Nesic Apr 17 '17 at 14:49
  • At design time surly will not work. That will work at runtime. For design time, you need to add reference to that component in the visual studio. References->Add reference – Abusnake Apr 17 '17 at 15:28
  • @IvicaNesic, you would need to turn `Option Strict Off` and then you can use late-binding, which does allow you to refer to any member without knowing whether or not it will exist at run time. If it does exist at run time then it will work and if it doesn't then an exception will be thrown. The thing is, you still have to know the names of the members at design time. Do you know those names? Reflection not only lets you invoke the members but also lets you discover them, where late-binding requires you to know they will exist. – jmcilhinney Apr 17 '17 at 15:36
  • @Abusnake How to add a reference to something that does not exist in design time? – Ivica Nesic Apr 17 '17 at 16:38
  • @jmcilhinney I have a few excel books over 100M in size. I need to import data from them. I could open them, see the structure, and create classes representing every single sheet, but it takes days. Theoretically I do know names of properties, but then I don’t need Reflection. Practically, I know how to pick the sheet’s structure, and I do know properties’ names, but I don’t know how to access them from instance, and I don’t know how to make a list(of newtype). I’ll bow to your suggestion to dig in Reflection documentation, which would be the best. – Ivica Nesic Apr 17 '17 at 16:58
  • You could create a `List(Of T)` using Reflection too but I'm not sure that it would be worth it. I'd probably suggest just creating a `List(Of Object)` if you're going to use Reflection to access the item properties anyway. – jmcilhinney Apr 17 '17 at 23:51