Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateInstanceFromAndUnWrap

123 views
Skip to first unread message

IanT8

unread,
Jun 27, 2008, 10:01:02 AM6/27/08
to
Is it possible to create an instance of a type using
"AppDomain.CreateInstanceFromAndUnwrap" by somehow calling a non-default
constructor?

This is how I'm doing it now using the default parameterless constructor:

IPlugin plug =
AppDomain.CreateInstanceFromAndUnwrap(strAssembly, strTypeName) as IPlugin;
plug.SetDispatcher(msgDispatcher);

I have to call SetDispatcher afterwards to inject another object.

I would rather the CreateInstanceFromAndUnwrap(...) call do it all.

I've tried this:

return domain.CreateInstanceFromAndUnwrap(
assemblyName, // assembly name
typeName, // type name
false, // ignore case
BindingFlags.CreateInstance, // binding flags
null, // binder
new object[] { dispatcher }, // args
null, // culture
null, // activationAttributes
null // securityInfo
) as IPlugin;

but it doesn't work.

Any ideas?

I've looked at the MSDN docs but there are no examples of how to use the
last two versions of CreateInstanceFromAndUnWrap.

Regards,
Ian.

Jeroen Mostert

unread,
Jun 27, 2008, 2:21:24 PM6/27/08
to
IanT8 wrote:
> Is it possible to create an instance of a type using
> "AppDomain.CreateInstanceFromAndUnwrap" by somehow calling a non-default
> constructor?
>
It should be.

> This is how I'm doing it now using the default parameterless constructor:
>
> IPlugin plug =
> AppDomain.CreateInstanceFromAndUnwrap(strAssembly, strTypeName) as IPlugin;
> plug.SetDispatcher(msgDispatcher);
>
> I have to call SetDispatcher afterwards to inject another object.
>
> I would rather the CreateInstanceFromAndUnwrap(...) call do it all.
>
> I've tried this:
>
> return domain.CreateInstanceFromAndUnwrap(
> assemblyName, // assembly name
> typeName, // type name
> false, // ignore case
> BindingFlags.CreateInstance, // binding flags
> null, // binder
> new object[] { dispatcher }, // args
> null, // culture
> null, // activationAttributes
> null // securityInfo
> ) as IPlugin;
>
> but it doesn't work.
>

Ah, that's always a problem. Doesn't work *how*? What are the actual
parameter values? What's the actual exception you get?

The online MSDN doesn't have examples, but the copy that comes with Visual
Studio does. Try BindingFlags.Default rather than
BindingFlags.CreateInstance, and make sure you specify full names for both
the assembly and the type. Also, note that you've specified that the search
should be case-sensitive, so make sure that's exactly right.

--
J.

IanT8

unread,
Jun 28, 2008, 4:47:01 AM6/28/08
to
Thanks, I'll check my local MSDN for examples.

The concrete implementation of IPlugIn has a default ctor and a non-default
ctor that takes a single argument of type IDispatcher. IDispatcher is my own
interface and not from the .NET Framework. IDispatcher has a single method
with this signature: void EnqueueMessage(string messageText).

When I call the many-argument version of CreateInstanceFromAndUnwrap, a
transparent proxy representing IPlugin is returned, but when I try to call
methods on the returned IPlugin I get an exception. As I'm at home now, I'll
have to post the details of the exception on Monday when I'm back in work.

Thanks.
Ian.

The "extended" CreateInstanceAndUnwrap

Brian Rasmussen [C# MVP]

unread,
Jul 5, 2008, 3:14:38 AM7/5/08
to
> Is it possible to create an instance of a type using
> "AppDomain.CreateInstanceFromAndUnwrap" by somehow calling a non-default
> constructor?

It is - just use this ctor:
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/62d28d33-bd19-357f-9673-f220b143ece3.htm

Unfortunately that means that you have to supply a lot of arguments, but it
can be done.

--
Regards,
Brian Rasmussen [C# MVP]
http://kodehoved.dk

"IanT8" <Ia...@discussions.microsoft.com> wrote in message
news:58283604-D4A3-4B91...@microsoft.com...

Ilya Albrekht

unread,
Jul 5, 2008, 4:04:17 PM7/5/08
to
I'm usually use Activator.CreateInstance.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

0 new messages