Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi can anyone point me in the right direction,

i'm writing a front end application, that will have around 10-15 models in the project.
the front end will look exactly the same regardless of which model you select, the only difference being that there will be more or less columns in the table depending on which model you select from a dropDown.

how do i then use a string e.g "name of model i clicked" to then create a new instance of that model, load the model so then i can get the ojbect info so i can count the amount of properties to then loop over them generating column headers and then add the column names (using propertyInfo.getValue or something).?

is this possible, i'm wanting to make generic re-usable code. so pseudo code:

UnknownType type = new UnknownType();

getPropertyCount(type);
for each(property in type){
getAllPropertyInfo();
buildHtml();

}
Posted
Comments
Sinisa Hajnal 3-Feb-15 5:59am    
Check Interfaces (to have all models conform to it so you're able to use same methods) and Factory pattern (in which you pass some parameter and get certain type instantiated as "real" type and another (interface) back.

1 solution

you can make use of reflection to get instance of the type you want

something like this

C#
(type of object)Activator.CreateInstance("name of assembly", "name of class");



C#
Activator.CreateInstance
is what you can use to create instance of the class from the string its part of reflection assembly in .net framework
 
Share this answer
 
v2

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