473,544 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection and Type.GetType

Is there any way to use Type.GetType to return a type that's in an
Assembly loaded from disk?

I sadly _have_ to use Type.GetType, as it's in framework code I can't
change.

At the moment, I'm loading the Assembly from disk (which works fine)
and adding it to the current AppDomain (just to be on the safe side),
but even when I do that, Type.GetType fails to find it.

Any suggestions?

Andy D

Nov 25 '05 #1
16 6500
Are you passing in the fully qualified name to Type.GetType("M yNameSpace.MyCl ass") ?
i.e Namespace.Class

Kalpesh
Nov 25 '05 #2
>Are you passing in the fully qualified name to Type.GetType("M yNameSpace.MyCl ass") ?
i.e Namespace.Class


You have to include the assembly name as well to get a truly FQ type
name.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 25 '05 #3
Andrew Ducker <an****@ducker. org.uk> wrote:
Is there any way to use Type.GetType to return a type that's in an
Assembly loaded from disk?

I sadly _have_ to use Type.GetType, as it's in framework code I can't
change.

At the moment, I'm loading the Assembly from disk (which works fine)
and adding it to the current AppDomain (just to be on the safe side),
but even when I do that, Type.GetType fails to find it.

Any suggestions?


Yes - specify the assembly name in the call to Type.GetType. For
instance:

using System;

public class Test
{
static void Main()
{
Type type = Type.GetType("S ystem.Data.SqlC lient.SqlComman d, " +
"System.Dat a, "+
"Version=1.0.50 00.0, "+
"Culture=neutra l, "+
"PublicKeyToken =b77a5c561934e0 89");
Console.WriteLi ne (type);
}
}
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 25 '05 #4
Thanks for correcting Mattias

Kalpesh
Nov 26 '05 #5
Jon,

What if the assembly (not signed) is loaded form filesystem (not from GAC) ?

How does one still find the type in such assembly ?
(except Assembly.Get... . methods())

How can I use Type.GetType with this kind of assembly ?

Kalpesh
Nov 26 '05 #6
Hi Jon,

The MSDN Doc says (for Type.GetType)
"GetType only works on assemblies loaded from disk"

So, in order to get a class, this is the naming scheme - it should use
TopNamespace.Su bNameSpace.Cont ainingClass+Nes tedClass,MyAsse mbly

Right?

Kalpesh
Nov 26 '05 #7
<Kalpesh/> <ka*****@disc.m icrosoft.com> wrote:
The MSDN Doc says (for Type.GetType)
"GetType only works on assemblies loaded from disk"

So, in order to get a class, this is the naming scheme - it should use
TopNamespace.Su bNameSpace.Cont ainingClass+Nes tedClass,MyAsse mbly


Yes, that's right.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 26 '05 #8
Hmm, in which case this should work - but doesn't:

OpenFileDialog fd = new OpenFileDialog( );
fd.ShowDialog(t his);
Assembly a = Assembly.LoadFi le(fd.FileName) ;
Type t= a.GetType("Test Class.FirstTest Class");
MessageBox.Show (t.AssemblyQual ifiedName);
Type t2 = Type.GetType(t. AssemblyQualifi edName);
MessageBox.Show (t2.AssemblyQua lifiedName);

In fact, the a.GetType works (providing you've created a class called
FirstTestClass in the Namespace TestClass), but the Type.GetType
doesn't.

Any ideas why?

Andy D

Nov 27 '05 #9
Andrew Ducker <an****@ducker. org.uk> wrote:
Hmm, in which case this should work - but doesn't:

OpenFileDialog fd = new OpenFileDialog( );
fd.ShowDialog(t his);
Assembly a = Assembly.LoadFi le(fd.FileName) ;
Type t= a.GetType("Test Class.FirstTest Class");
MessageBox.Show (t.AssemblyQual ifiedName);
Type t2 = Type.GetType(t. AssemblyQualifi edName);
MessageBox.Show (t2.AssemblyQua lifiedName);

In fact, the a.GetType works (providing you've created a class called
FirstTestClass in the Namespace TestClass), but the Type.GetType
doesn't.

Any ideas why?


Yes - Type.GetType looks for assemblies in the "normal" places - it
doesn't know where to find "other" assemblies loaded from elsewhere on
the disk. I'm slightly surprised that it doesn't look at all the
assemblies loaded in the current AppDomain first, but that's life, I
guess...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 27 '05 #10

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

Similar topics

9
3675
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can...
0
8420
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. xxx======================================================================
10
7343
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the...
11
12729
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
1
2271
by: Rathish P S | last post by:
Hi friends, I am trying with Reflection and Generics in C#. But I get confused with some of these new features. I am trying to get the type of generic parameter with the type.Assembly.GetType(name,false,false) method. It is return as null. Can anybody tell can help me to find the basic reason of this issue? this is the sample code that I...
5
2409
by: Roger Odermatt | last post by:
Hello I have the follow Code. --------------------------------- Dim myType As Type myType = BusinessObject.GetType Dim myP As System.Reflection.PropertyInfo
2
3623
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
7
2336
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide) http://msdn2.microsoft.com/en-us/library/ms366789.aspx
15
2124
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to have an attribute which can be applied to a class definition of a class which inherits from a base, mustinherit class. I want to define methods in the...
4
2598
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
0
7643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7798
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7738
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5956
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
688
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.