473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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?
Jul 26 '06 #1
3 5957
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
Jul 26 '06 #2
Kürşat <ku***********@hotmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 26 '06 #3
Cheers Jon; I must have somehow missed that... that's my "something
new" for today ;-p

Marc

Jul 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: shmeian | last post by:
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...
11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
15
by: Brian Rogers | last post by:
Hello everyone, I apologize for the cross and re-post, but I am still searching for an answer. Why can C++ can create this object, but C# can't? I am trying to create an instance of the...
7
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass...
3
by: System.Reflection Activator | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
3
by: JJ | last post by:
Hi, I need to pass a dataset to another win form along with a SqldataAdapter. I don't want to recreate the SqlDataAdapter again either. So to pass to another Win form in my windows form app, do...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
2
by: jcomber | last post by:
Hi, I'd be grateful for some help... I'm trying to create a late bound object using: Activator.CreateInstance(Type, ConstructorParameters()) The constructor of the object takes a delegate...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
2
by: muriwai | last post by:
Hi, VC++ 2008 /clr: I have an unmanaged class: "class U" and managed one: "class ref M{ M( const U* ); }". In other words, the constructor of the managed class M takes the pointer to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.