Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

Thanks for the response
i will explain my need in detail

usually i call windows form by creating object.
If i want a screen named frmcompany . i write code as
C#
frmcompany obj=new frmcompany (); 
obj.show(); 


But in my current case this form name will be in one string variable .let it be ls_com_name
C#
ls_com_name="frmcompany ";//this value getting from database not constant 

how to call frmcomapny from string ls_com_name.
there isn't method like ls_com_name.show();
what to use instead..
Posted
Updated 7-Mar-12 0:50am
v4
Comments
Sergey Alexandrovich Kryukov 7-Mar-12 3:11am    
Why did you decide to store a form name is the database in first place. Though there won't be a problem with invocation? Is it a name of the form class, or something else. Is it a full name or not? Is the assembly strongly named or not?
--SA
Abhinav S 7-Mar-12 3:25am    
Code tags added.

Lets say you are getting the string as "ClassA" and you want to create its object.

if you can use dynamic type variables then do this
<br />
var d1 = Type.GetType("ClassA");<br />

if not then
<br />
object d1 = Type.GetType("ClassA");
 
Share this answer
 
You should ask yourself how come you first put the names in the database before doing elementary technological research. Now you are asking about invocation… You should think first, make decision later. I don't think this is a good idea.

Basically, you can have a class name, but you should better have a full class name, to make sure you are working with right namespace and right assembly. It's also good to check up assembly signature against the one stored in the database. You see, you have entered in some non-reliable field where the wrong assembly (or a wrong versions) can contain different types with the same names and cases like that.

You can find the type you need by its name using Reflection. This is done using the method System.Reflection.Assembly.GetType(String). Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].

The invocation of the type constructor is required to create an instance (of the form, in your case).
You need to find constructor without parameters, get an instance of the class System.Reflection.ConstructorInfo (apparently, there can be only one constructor with this signature) and create an instance using its method System.Reflection.ConstructorInfo.Invoke.

Please see:
http://msdn.microsoft.com/en-us/library/system.type.aspx[^],
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo.aspx[^],
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx[^].

Even though it looks conceptually complex, it's surprisingly easy to achieve if you simply read on all of the involved types and method of Reflection in the MSDN articles referenced above and do all steps in the order I explained.

But I want to warn you: you approach might work immediately, but nevertheless, it needs reviewing. You are can get yourself in nightmare when you start modifying the system and support it. All kinds of incompatibilities between versions of software and data in database can haunt you badly. You need to plan for all the scenarios very thoroughly. First of all, revise the whole idea of string class names (or some other code-related entities) in the database. This is potentially serious architecture, needs a serious architect…

Good luck,
—SA
 
Share this answer
 
Comments
amritha444 7-Mar-12 4:13am    
thanks for the valuable advice .i will discuss this with my senior engineer
Sergey Alexandrovich Kryukov 7-Mar-12 19:11pm    
Good. And you are very welcome.
Will you consider accepting this answer formally (green button)? After all, I explained how to invoke the form to get its instance by the name, so I answered the question.
--SA

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