Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:13:54+00:00 2026-05-13T13:13:54+00:00

Experimenting using different approaches and reducing code maintenance I have ended up using reflection

  • 0

Experimenting using different approaches and reducing code maintenance I have ended up using reflection to create new forms in my MDI application.

The reason for this ‘solution’ is to provide a central place to setup new forms, implement security checks and possible future needs.

I currently have a static Activator class that is instantiated with the MDI parent form, and whenever I need to create setup new forms for the application i call a method i have created : private static T CreateForm<T>(params args[]) where T: class. This method uses the System.Activator class to instantiate the given class. I have other static methods like public static void ShowCustomers() that in reality uses the ‘reflection’ method by calling CreateForm<frmCustomers>() which sets up the MDI parent and so on.

The object creation occurs within a try/catch block and I show a message if the form couldn’t be created and the reasons why.

What I’ve explained so far does possibly not validate the need to use reflection to create MDI forms in a business application, but I wish to add another use this has in my application. I have a component that implements the ISupportInitialize interface, and when I drop this onto a form it performs security checks and throws a System.Security.SecurityException in the EndInit method. This exception is cought in the CreateFormmethod and a user friendly message is displayed to the user.

Also, I was thinking of possibly storing a MRU (most recently used) list of the forms that are created and where better to do this than in the CreateForm method.

I’m not familiar with terms like code smell, design smell or such but I’ve seen the words used often enough, but my question basically boils down to this:

Given the information i’ve provided (hopefully understandable), does this approach smell ‘bad’?

If so, which approach is better suited?

Any comments are welcome.

Thanks,
Stefan

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)
        {
            // Check if the inner exception is a security exception
            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>();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:13:54+00:00Added an answer on May 13, 2026 at 1:13 pm

    You built yourself a framework. It solves your problem and, as you said, it reduces code maintenance. If you are comfortable with code, you’re done.

    Good idea would be to document it, just in case you get/hire more developers to work on this project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting with sending messages from two .NET Windows Forms applications using
I have a multi-tenant application and I'm experimenting with using the i18n gem to
I have some forms in my application which have different states depending on what
I am using JDK 1.6.0.18 for my Swing application. I am experiencing very different
We use SVN for our source-code revision control and are experimenting using it for
I'm experimenting with using a string for storing different kind of data types in
I'm experimenting with a treeview in a little C#/Winforms application. I have programatically assigned
I have a use case that I am experimenting with - sandboxing many different
I'm experimenting ideas around using AppDomain to manage some legacy code contains lots of
i'm currently experimenting wpf communication between two views of different modules using prism IEventAggregator.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.