By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

Activator.CreateInstance Method

P: n/a
I have the following code which works fine.
However I want to pass the object I'm instantiating a string for its
constructor.
I can't get the syntax right. Can someone give me an example of
Activator.CreateInstance where they pass an argument with the object type?

Assembly a = Assembly.GetExecutingAssembly();
Type type = a.GetType("MyAssemb.MyClass");
Object o = Activator.CreateInstance(type);
MyClass myObj= (MyClass)Activator.CreateInstance(type, args[]?????);
Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
This should do it:

MyClass myObj = (MyClass)Activator.CreateInstance(type, new Object[]
{"string arg"});

Basically, you need to passing an array of objects for the argument(s).

hope that helps..
Imran.

"shmeian" <sh*****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
I have the following code which works fine.
However I want to pass the object I'm instantiating a string for its
constructor.
I can't get the syntax right. Can someone give me an example of
Activator.CreateInstance where they pass an argument with the object type?

Assembly a = Assembly.GetExecutingAssembly();
Type type = a.GetType("MyAssemb.MyClass");
Object o = Activator.CreateInstance(type);
MyClass myObj= (MyClass)Activator.CreateInstance(type, args[]?????);

Nov 22 '05 #2

P: n/a
Thanks Imran. It works!

"Imran Koradia" wrote:
This should do it:

MyClass myObj = (MyClass)Activator.CreateInstance(type, new Object[]
{"string arg"});

Basically, you need to passing an array of objects for the argument(s).

hope that helps..
Imran.

"shmeian" <sh*****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
I have the following code which works fine.
However I want to pass the object I'm instantiating a string for its
constructor.
I can't get the syntax right. Can someone give me an example of
Activator.CreateInstance where they pass an argument with the object type?

Assembly a = Assembly.GetExecutingAssembly();
Type type = a.GetType("MyAssemb.MyClass");
Object o = Activator.CreateInstance(type);
MyClass myObj= (MyClass)Activator.CreateInstance(type, args[]?????);


Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.