List Info

Thread: Reflection, Type.GetType(...) and Activator.CreateInstance




Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 14:23:28
I'm having difficulty getting Type.GetType(...) and
Activator.CreateInstance
working.

I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
can look at the AppDomain.GetAssemblies() method and see
that the assembly
is loaded.  However, when I try to create a type from that
assembly I get
this exception:

Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system
cannot find the file specified.
However, if I use the Assembly object from the
AppDomain.GetAssemblies()
method and execute the CreateInstance() method it works.

Any ideas what's going on here?

I was under the impression that Type.GetType(...) searched
the AppDomain for
the assembly.

Thanks,
Mike

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
country flaguser name
United States
2008-06-06 14:43:54
Type.GetType does do what you think it does, but if you used
LoadFrom that
puts your loaded assembly into the 'LoadFrom' context and
normal type
resolution won't be able to find it.

It's a much better idea to find a way to NOT use LoadFrom
and if you do, use
this instead which works how you want it:

AssemblyName name = AssemblyName.GetAssemblyName(
"C:\your.dll" );
Assembly.Load( name );

// this will now work
Type type = Type.GetType( "your.type, your.assembly,
Version=xxx,
Culture=xxx, PublicKeyToken=xxx" );

The first two from Suzanne Cook should provide more info:
ht
tp://www.google.com/search?q=.net+loadfrom+context&rls=c
om.microsoft&ie=

UTF-8&oe=UTF-8&startIndex=&startPage=1

Adam..

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Mike
Andrews
Sent: Friday, June 06, 2008 2:23 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Reflection, Type.GetType(...)
and
Activator.CreateInstance

I'm having difficulty getting Type.GetType(...) and
Activator.CreateInstance
working.

I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
can look at the AppDomain.GetAssemblies() method and see
that the assembly
is loaded.  However, when I try to create a type from that
assembly I get
this exception:

Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system
cannot find the file specified.
However, if I use the Assembly object from the
AppDomain.GetAssemblies()
method and execute the CreateInstance() method it works.

Any ideas what's going on here?

I was under the impression that Type.GetType(...) searched
the AppDomain for
the assembly.

Thanks,
Mike

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 14:44:19
http://blogs.msdn.com/thott
ams/archive/2007/05/11/load-contexts-and-type-gettype.aspx

On Fri, Jun 6, 2008 at 2:23 PM, Mike Andrews
<outdoor.jellyrollgmail.com> wrote:
> I'm having difficulty getting Type.GetType(...) and
Activator.CreateInstance
> working.
>
> I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
> can look at the AppDomain.GetAssemblies() method and
see that the assembly
> is loaded.  However, when I try to create a type from
that assembly I get
> this exception:
>
> Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system
> cannot find the file specified.
> However, if I use the Assembly object from the
AppDomain.GetAssemblies()
> method and execute the CreateInstance() method it
works.
>
> Any ideas what's going on here?
>
> I was under the impression that Type.GetType(...)
searched the AppDomain for
> the assembly.
>
> Thanks,
> Mike
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com

>



--
Phyllis Diller  - "Never go to bed mad. Stay up and
fight."

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
country flaguser name
United States
2008-06-06 14:43:54
Type.GetType does do what you think it does, but if you used
LoadFrom that
puts your loaded assembly into the 'LoadFrom' context and
normal type
resolution won't be able to find it.

It's a much better idea to find a way to NOT use LoadFrom
and if you do, use
this instead which works how you want it:

AssemblyName name = AssemblyName.GetAssemblyName(
"C:\your.dll" );
Assembly.Load( name );

// this will now work
Type type = Type.GetType( "your.type, your.assembly,
Version=xxx,
Culture=xxx, PublicKeyToken=xxx" );

The first two from Suzanne Cook should provide more info:
ht
tp://www.google.com/search?q=.net+loadfrom+context&rls=c
om.microsoft&ie=

UTF-8&oe=UTF-8&startIndex=&startPage=1

Adam..

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Mike
Andrews
Sent: Friday, June 06, 2008 2:23 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Reflection, Type.GetType(...)
and
Activator.CreateInstance

I'm having difficulty getting Type.GetType(...) and
Activator.CreateInstance
working.

I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
can look at the AppDomain.GetAssemblies() method and see
that the assembly
is loaded.  However, when I try to create a type from that
assembly I get
this exception:

Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system
cannot find the file specified.
However, if I use the Assembly object from the
AppDomain.GetAssemblies()
method and execute the CreateInstance() method it works.

Any ideas what's going on here?

I was under the impression that Type.GetType(...) searched
the AppDomain for
the assembly.

Thanks,
Mike

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 14:44:19
http://blogs.msdn.com/thott
ams/archive/2007/05/11/load-contexts-and-type-gettype.aspx

On Fri, Jun 6, 2008 at 2:23 PM, Mike Andrews
<outdoor.jellyrollgmail.com> wrote:
> I'm having difficulty getting Type.GetType(...) and
Activator.CreateInstance
> working.
>
> I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
> can look at the AppDomain.GetAssemblies() method and
see that the assembly
> is loaded.  However, when I try to create a type from
that assembly I get
> this exception:
>
> Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system
> cannot find the file specified.
> However, if I use the Assembly object from the
AppDomain.GetAssemblies()
> method and execute the CreateInstance() method it
works.
>
> Any ideas what's going on here?
>
> I was under the impression that Type.GetType(...)
searched the AppDomain for
> the assembly.
>
> Thanks,
> Mike
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com

>



--
Phyllis Diller  - "Never go to bed mad. Stay up and
fight."

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 14:51:00
Assembly.LoadFrom()... that way lies madness.

What's happening when you call LoadFrom() is that you're
getting a private
copy of the assembly. It doesn't act like an implicitly
loaded assembly, so
when you try to create a type based on its fully qualified
name, your
privately loaded copy is not able to provide it.

I'd strongly advise to do this instead:

    Assembly.Load(AssemblyName.GetAssemblyName(filename));

rather than Assembly.LoadFrom().

On Fri, Jun 6, 2008 at 12:23 PM, Mike Andrews
<outdoor.jellyrollgmail.com>
wrote:

> I'm having difficulty getting Type.GetType(...) and
> Activator.CreateInstance
> working.
>
> I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
> can look at the AppDomain.GetAssemblies() method and
see that the assembly
> is loaded.  However, when I try to create a type from
that assembly I get
> this exception:
>
> Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The
> system
> cannot find the file specified.
> However, if I use the Assembly object from the
AppDomain.GetAssemblies()
> method and execute the CreateInstance() method it
works.
>
> Any ideas what's going on here?
>
> I was under the impression that Type.GetType(...)
searched the AppDomain
> for
> the assembly.
>
> Thanks,
> Mike
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>



--
http://bradwilson.type
pad.com/
http://www.fl
ickr.com/photos/dotnetguy/
http://www.last.fm
/user/dotnetguy/

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 14:51:00
Assembly.LoadFrom()... that way lies madness.

What's happening when you call LoadFrom() is that you're
getting a private
copy of the assembly. It doesn't act like an implicitly
loaded assembly, so
when you try to create a type based on its fully qualified
name, your
privately loaded copy is not able to provide it.

I'd strongly advise to do this instead:

    Assembly.Load(AssemblyName.GetAssemblyName(filename));

rather than Assembly.LoadFrom().

On Fri, Jun 6, 2008 at 12:23 PM, Mike Andrews
<outdoor.jellyrollgmail.com>
wrote:

> I'm having difficulty getting Type.GetType(...) and
> Activator.CreateInstance
> working.
>
> I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).  I
> can look at the AppDomain.GetAssemblies() method and
see that the assembly
> is loaded.  However, when I try to create a type from
that assembly I get
> this exception:
>
> Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The
> system
> cannot find the file specified.
> However, if I use the Assembly object from the
AppDomain.GetAssemblies()
> method and execute the CreateInstance() method it
works.
>
> Any ideas what's going on here?
>
> I was under the impression that Type.GetType(...)
searched the AppDomain
> for
> the assembly.
>
> Thanks,
> Mike
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>



--
http://bradwilson.type
pad.com/
http://www.fl
ickr.com/photos/dotnetguy/
http://www.last.fm
/user/dotnetguy/

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 15:24:23
That helps out quite a bit.  Thank you.

I still can't get it to work right but with the information
all of you sent
I'm on the right track for figuring it out.
I also read the website by Thottam R. Sriram and it was
quite informative as
well.

Thanks,
Mike

On Fri, Jun 6, 2008 at 2:51 PM, Brad Wilson
<dotnetguygmail.com> wrote:

> Assembly.LoadFrom()... that way lies madness.
>
> What's happening when you call LoadFrom() is that
you're getting a private
> copy of the assembly. It doesn't act like an implicitly
loaded assembly, so
> when you try to create a type based on its fully
qualified name, your
> privately loaded copy is not able to provide it.
>
> I'd strongly advise to do this instead:
>
>   
Assembly.Load(AssemblyName.GetAssemblyName(filename));
>
> rather than Assembly.LoadFrom().
>
> On Fri, Jun 6, 2008 at 12:23 PM, Mike Andrews
<outdoor.jellyrollgmail.com
> >
> wrote:
>
> > I'm having difficulty getting Type.GetType(...)
and
> > Activator.CreateInstance
> > working.
> >
> > I've loaded an assembly into the AppDomain using
Assembly.LoadFrom(...).
>  I
> > can look at the AppDomain.GetAssemblies() method
and see that the
> assembly
> > is loaded.  However, when I try to create a type
from that assembly I get
> > this exception:
> >
> > Could not load file or assembly 'xxxxxxxxx,
Version=1.0.0.0,
> > Culture=neutral, PublicKeyToken=null' or one of
its dependencies. The
> > system
> > cannot find the file specified.
> > However, if I use the Assembly object from the
AppDomain.GetAssemblies()
> > method and execute the CreateInstance() method it
works.
> >
> > Any ideas what's going on here?
> >
> > I was under the impression that Type.GetType(...)
searched the AppDomain
> > for
> > the assembly.
> >
> > Thanks,
> > Mike
> >
> > ===================================
> > This list is hosted by DevelopMentor(R)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com

> >
>
>
>
> --
> http://bradwilson.type
pad.com/
> http://www.fl
ickr.com/photos/dotnetguy/
> http://www.last.fm
/user/dotnetguy/
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 15:34:49
We've literally wasted dozens of hours trying to get this
stuff to work just
right with xUnit.net, so I feel your pain. 

On Fri, Jun 6, 2008 at 1:24 PM, Mike Andrews
<outdoor.jellyrollgmail.com>
wrote:

> That helps out quite a bit.  Thank you.
>
> I still can't get it to work right but with the
information all of you sent
> I'm on the right track for figuring it out.
> I also read the website by Thottam R. Sriram and it was
quite informative
> as
> well.
>
> Thanks,
> Mike
>

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Reflection, Type.GetType(...) and Activator.CreateInstance
user name
2008-06-06 15:39:39
LOL.

I can imagine so after the stuff I've gone through.

On Fri, Jun 6, 2008 at 3:34 PM, Brad Wilson
<dotnetguygmail.com> wrote:

> We've literally wasted dozens of hours trying to get
this stuff to work
> just
> right with xUnit.net, so I feel your pain. 
>
> On Fri, Jun 6, 2008 at 1:24 PM, Mike Andrews
<outdoor.jellyrollgmail.com>
> wrote:
>
> > That helps out quite a bit.  Thank you.
> >
> > I still can't get it to work right but with the
information all of you
> sent
> > I'm on the right track for figuring it out.
> > I also read the website by Thottam R. Sriram and
it was quite informative
> > as
> > well.
> >
> > Thanks,
> > Mike
> >
>
>  ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


[1-10] [11-13]

about | contact  Other archives ( Real Estate discussion Medical topics )