Click here to Skip to main content
15,911,317 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public static Form getFrom()
{
    string formname=Assembly.GetEntryAssembly().GetName().Name+"."+"AddForm";

    //actually formname contain this string "DynamicProject.AddForm" but tp shows null value

    Type tp=Type.GetType(formname);
    baseForm=(Form)Activator.CreateInstance(tp); 
   ///here shows null value does not return
    return baseForm;
}


I have tried so many times,But but it same error, Please Help Me
Posted
Comments
Mehdi Gholam 4-Sep-11 14:08pm    
What is the error you are getting?

C#
var newForm=(Form)Activator.CreateInstance(null, "aFrmInCurrntCntxt").Unwrap();

newForm.Text = "some new form";

newForm.Show();
In this two-string-parameter variant of Activator.CreateInstance, if the first parameter string, "AssemblyName," is 'null:' MS says: "the executing assembly is searched."

Note this flavour of CreateInstance returns a "System.Runtime.Remoting.ObjectHandle" that must be unwrapped.

This technique does not require Reflection.

Tested and verified in a simple WinForms app.

Hope this helps.
 
Share this answer
 
v2
Why not use FindTypes[^] directly?
 
Share this answer
 

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