all groups > dotnet clr > july 2004 >
You're in the

dotnet clr

group:

Invoking complex constructors through reflection. Is there any way?


Invoking complex constructors through reflection. Is there any way? Bern McCarty
7/16/2004 3:59:03 PM
dotnet clr:
I am using reflection to successfully find a System.Type that derives from an abstract class that my
code knows about. I want to create an instance of this derived type. The type in question can be
created only with a complex constructor (it has one parameter). Is there any way for me to create
an instance of this type?

Bern McCarty
Bentley Systems, Inc.

Re: Invoking complex constructors through reflection. Is there any way? Jon Skeet [C# MVP]
7/16/2004 9:36:41 PM
[quoted text, click to view]

And if that doesn't specify the appropriate constructor to use (eg if=20
there are two constructors, one taking object and one taking string,=20
and you want to pass a string to the constructor taking object) you can=20
use Type.GetConstructor and then invoke it.

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: Invoking complex constructors through reflection. Is there any way? Mattias Sjögren
7/16/2004 10:23:33 PM
Bern,

[quoted text, click to view]

Sure, as long as you know what to pass to those parameters.
System.Activator.CreateInstance() has overloads that let you specify
an array of arguments to the constructor.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button