Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm stumped. Please help! I've googled till my fingers hurt!

I have an instance of a type (created through reflection) and need to use it's type in the declaration of a generic class. Emample:

VB
'actually created with reflection, but for the sake of simplicity
dim myObject as Foo 

dim myGeneric as SomeGenericClass.GetAll(of myObject.getType)() 'does not work
dim myGeneric as SomeGenericClass.GetAll(of Foo)() 'works


I won't know the the class type during design time. It's loaded from a database at runtime. I'm using reflection to find and create an instance of the correct class, but I can't use it's type in the generic declaration. Is this even possible? I'd hate to resort to a Select statement as the class has over a hundred possibilities and we plan on the list of classes changing often.

For anyone curious as to the endgame, the program is connecting to SQL Server, reading one database using the Entity Framework, and then moving that data to a CRM Webapp using the CRM SDK (which is very similar to the entity framework in the way queries are created).


Update

For clarification: I am using the entity framework to get data from an SQL database and the CRM SDK to connect to our CRM project to put the data from the SQL database in. We made a class based on IRepository. The Entity Framework generates classes and properties based on the tables in our database. This data is held in the repository. So if my SQL database had a table called "Employee" with two columns "ID" and "Name", I would use the following to get information on that employee. In this example, our repository is called "SQLdata".

VB
'get all employees
dim emps as list(of Employee) = SQLdata.GetAll(of Employee)()

'write employees out to console window
for each curEmp as Employee in emps
  console.writeLine("ID: " & curEmp.ID & " Name: " & curEmp.Name)
next


That works fine. Now imagine that you don't know that you need the employee's information. There's a list of strings that contains "Employee.Name", "Supervisor.Name", "Project.ID", etc. I have to load this list, figure out which entity framework class is a match, and get the data for it. Using reflection I've been able to get the class. But I can't use it in the GetAll() method of the repository. This is what I want to do:

VB
'get the class type from the dictionary created by my reflection code
'it takes a string and returns the System.Type that string represents
dim myType as System.Type = myTypeDictionary("Employee") 

'get all records for that type
dim emps as list(of myType) = SQLdata.GetAll(of myType)()'does not work :(


The above code says "'myType' is not defined."

Please save me Code Project! I trust in your awesomeness!!
Posted
Updated 1-Feb-12 8:18am
v2
Comments
Simon_Whale 1-Feb-12 18:06pm    
what is mTypeDictionary?
a_pfeifer 20-Apr-12 18:47pm    
Did you figure this out? I have a similar situation.

1 solution

not sure what you are after

is it that you have a rountine lets call if foobar which takes a generic argument t and you want to create an instance of t inside that rountine?

e.g.

VB
 public sub foobar(of t)(byref Data as t)
   'This now creates a variable which will then become an object
   'of the type t.
   dim myObject as object = activator.createinstance(gettype(t))

 end sub

'You would call this rotine like so, from somewhere in your code
dim stringData as string = "SIMON"
foobar(stringData)
 
Share this answer
 
Comments
LCARS x32 1-Feb-12 14:19pm    
Hi Simon, not quite. I've updated my question with more information. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900