Instantiate new object based off a Type provided by custom EventArgs

I have a set of custom EventArgs that contain a Type, which in the course of this event occuring what type of object I need to instantiate.  I can't figure out how to create a new instance of an object of the specified type without doing a switch or if/else statement.  Is there something elegant I can use?

There's some real choppy pseudo-code below, but the general idea should come across.  Basically, I just want to set a public instance of an object derived from a common interface using the Type that is sent through the event args.  I'd like to do this without having to use a switch or if/else, as that defeats the purpose of having the event args.

(pseudo-code)
MyEventArgs : EventArgs
{
     public Type TypeYouNeedToCreate;
    ... this gets set appropriately on the constructor.
}

(on a page elsewhere in the application)
public IMyObject MyObject;
MyUserControl_OnMyEvent(object sender, MyEventArgs e)
{
    MyObject = new e.TypeYouNeedToCreate();  //TypeYouNeedToCreate implements IMyObject
}
(pseudo-code)
MyEventArgs : EventArgs
{
     public Type TypeYouNeedToCreate;
    ... this gets set appropriately on the constructor.
}
 
(on a page elsewhere in the application)
public IMyObject MyObject;
MyUserControl_OnMyEvent(object sender, MyEventArgs e)
{
    MyObject = new e.TypeYouNeedToCreate();  //TypeYouNeedToCreate implements IMyObject
}

Open in new window

hifisoftwareAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AUmidhCommented:
I think you can declare this of System.Object Type.
[Reason] Since every type in .NET is derieved directly or indirectly from System.Object

Object MyObject = new e.TypeYouNeedToCreate();

and where ever you want to use it just parse like.

int intObject=(int)MyObject
0
patrickrivaCommented:
You can use the following
Activator.CreateInstance(typeYouNeedToCreate);
 
If your constructor has parameters you need
 
 
Activator.CreateInstance(typeYouNeedToCreate, new object[] { blah, blah } );

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.