Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
'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".
 
'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:
 
'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 1-Feb-12 6:27am
Edited 1-Feb-12 9:18am
v2
Comments
Simon_Whale at 1-Feb-12 18:06pm
   
what is mTypeDictionary?
Andy Pfeifer at 20-Apr-12 18:47pm
   
Did you figure this out? I have a similar situation.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
  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)
  Permalink  
Comments
LCARS x32 at 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)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 1 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100