PC Review


Reply
Thread Tools Rate Thread

(Activator.CreateObject) : Passing parameters to constructor while creating object at run-time

 
 
Kürşat
Guest
Posts: n/a
 
      26th Jul 2006
Hi,

I want to set a plug-in architecture and need object creation at run-time. I
can create objects at run-time using Activator.CreateObject method, so far
so good. But some objects have constructors other than default one and I
should pass required parameters to use them. Is it possible to instantiate
objects at run-time using non-default construcotors?


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      26th Jul 2006
I'm guessing you mean for a type that isn't known at compile-time; if so,
then by reflection, yes; once you have the Type definition (via Type.GetType
[static] or Assembly.GetType [instance]), call e.g.

Type t = null; // TODO: aquire the type-def
object newObj = t.GetConstructor(new Type[] {typeof(string),
typeof(int)}).Invoke(new object[] {"abc",123});

Marc


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Jul 2006
Kürşat <(E-Mail Removed)> wrote:
> I want to set a plug-in architecture and need object creation at run-time.. I
> can create objects at run-time using Activator.CreateObject method, so far
> so good. But some objects have constructors other than default one and I
> should pass required parameters to use them. Is it possible to instantiate
> objects at run-time using non-default construcotors?


Activator.CreateInstance has overloads allowing you to specify the
arguments to pass.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      27th Jul 2006
Cheers Jon; I must have somehow missed that... that's my "something
new" for today ;-p

Marc

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters by reference to a VC COM Object Marja Ribbers-de Vroed Microsoft VC .NET 7 16th Dec 2005 08:11 PM
Passing a Delegate to a Constructor using Activator.CreateInstance() jcomber@southwestwater.co.uk Microsoft VB .NET 2 30th Sep 2005 10:02 AM
Passing output parameters to SQL Server stored procedures through an SqlCommand object's Parameters collection Mark Rae Microsoft ADO .NET 8 26th Sep 2005 03:28 PM
Passing a constructor parameter with Activator.CreateInstance =?Utf-8?B?am1obWFpbmU=?= Microsoft Dot NET Framework Forms 5 29th Jun 2005 02:42 AM
Activator.CreateInstance as CreateObject in VB -> late bind and COM+ Xavier MT Microsoft C# .NET 0 11th May 2004 06:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:12 PM.