How do you use Reflection to create an instance of a class?

Denaes

Senior Contributor
Joined
Jun 10, 2003
Messages
956
Getting to the basics, a function takes in a string (the name of the class).

For example "CustomerDataset".

Given this string, I want to find all the tables in the Class.

something like:

Dim DS as Class = Reflection.ConvertStringToClass("CustomerDataset")

or

Dim DS as getType("CustomerDataset")

I'm not picky about how I'm doing this, just so long as I end up with access to the tables collection of the dataset, being given a name.

It might help to say I'm using this in a Macro, so I'm not sure if DTS will help at all with this.

Thanks if you have any info
 
I'm not going to even pretent to understand exactly what your trying to achieve. As such this might be of no use to you at all, if thats the case, just ignore my babblings.

Creating an instance of a class from a string can be achieved through the use of
Activator.CreateInstace().
 
I believe that Activator requires a System.Type or an assembly qualified type name (string) to create an instance. The easiest way, then, to instantiate a class given only the name of the class is to enumerate through the assemblies that are loaded in the current application domain, and on each assembly enumerate through the defined types, until you find a type whose name matches the name entered by a user. If you already know which assembly this type is located in, you can skip the first step and just enumerate the types in the assembly you want to search. You also need to note that names in compiled IL are not exactly the same as the names in code. The generic list class is listed as something like System.Collections.Generic.List`1, and a nested class, such as ListView.ListViewItem, would be listed as System.Windows.Forms.ListView+ListViewItem. There is no nifty function built into .Net to do this for you (maybe I can whip something up and put it in the code library, but not right now).

I'm not sure of exactly what it is you want to do, but if you want to examine the type (i.e. fields/properties/functions/etc.) you don't need to create it. Just use the functions on the System.Type object you obtain. If you need to actually interact with an instance of the object (get a value from a field/property or invoke a method), you can use the System.Type you've obtained to instantiate the object through the Activator class.

Hope I didn't hurt your head to bad. If I knew exactly what you were doing, I could slightly more specific help.
 
Well as to what I was doing:

Creating a macro that could be called from within any dataset class and have a template created based on the tables the dataset has.

What I did:

1. I used DTE.ActiveDocument to get at the file I was calling the Macro from and DTE.ActiveDocument.ProjectItem.ContainingProject. This got me the full filename I was running the Macro from and the name of the project. Since I follow the convention of keeping the classname the same as the filename, that works out well.
2. Using the complete filename, I dynamically knew where the assembly was. I created an instance of the Assembly using the path from the file, adding in "bin\debug\" and the project name ".dll" which got me the assembly. Yeah, so this means the project has to be built after adding a class & prior to using the macro.
3. A loop for each Type in Assembly.GetTypes and checked that the type contained the ClassName & "TableAdapters."

Oh, and Marble... yesh that did make my head spin. But thank you. I really need to learn more about the tricks of reflection, especially for these Macros that this project will need. You're right that I don't actually need an instance of the class, so much as I need access to find out the names of the tables the dataset has. So I might be revising how I did things once I understand some of these reflection methods better, but it's workable now.
 
Back
Top