By experimenting with different approaches and reducing code maintenance, I ended up using reflection to create new forms in my MDI application.
The reason for this “decision” is to create a center to set up new forms, perform security checks and possible future needs.
At the moment I have a static class the Activator, which is created with the MDI parent form, and whenever I need to create new forms for the application, I call the method that I created: private static T CreateForm<T>(params args[]) where T: class. This method uses the System.Activator class to instantiate this class. I have other static methods, such as public static void ShowCustomers()those that actually use the reflection method, calling CreateForm<frmCustomers>()that sets up the parent MDI, etc.
An object is created in the try / catch block, and I show a message if the form cannot be created and the reasons.
What I have explained so far may not confirm the need to use reflection to create MDI forms in a business application, but I want to add another use that it has in my application. I have a component that implements the ISupportInitialize interface, and when I throw it into the form, it performs security checks and throws a System.Security.SecurityException in the EndInit method. This exception is considered in the method CreateForm, and a user-friendly message is provided to the user.
In addition, I was thinking about whether it is possible to save the MRU list (the most recently used) of the created forms and where it is better to do this than in the method CreateForm.
, , .., , , :
, (, ), ""?
, ?
.
,
public class Activator
{
private static Activator instance;
private MainForm mainForm;
private Activator(MainForm mainForm)
{
this.mainForm = mainForm;
}
private Activator()
{
}
private static Activator Instance
{
get
{
if (instance == null) throw new Exception("Not activated");
else return instance;
}
}
private static void ShowMDIChild<T>(params object[] args) where T : class
{
try
{
System.Windows.Forms.Form frm = Create<T>(args) as System.Windows.Forms.Form;
ShowMDIChild(frm);
}
catch (Exception e)
{
bool isSecurity = false;
if (e.GetType().Equals(typeof(System.Security.SecurityException))) isSecurity = true;
if (!isSecurity && e.InnerException != null && e.InnerException.GetType().Equals(typeof(System.Security.SecurityException))) isSecurity = true;
if(isSecurity)
MessageBox.Show(Instance.mainForm, "You do not have the neccessary privileges to access this resource", "Access denied", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
else
MessageBox.Show(Instance.mainForm, e.Message, "An error has occurred", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
private static void ShowMDIChild(System.Windows.Forms.Form form)
{
Instance.mainForm.ShowMDIChild(form);
}
private static T Create<T>(params object[] args) where T: class
{
T result = System.Activator.CreateInstance(typeof(T), args) as T;
return result;
}
public static void Register(MainForm mainForm)
{
instance = new Activator(mainForm);
}
public static void Customers()
{
ShowMDIChild<Forms.Customers>();
}
}