473,382 Members | 1,202 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,382 software developers and data experts.

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 6484
Are you passing in the fully qualified name to Type.GetType("MyNameSpace.MyClass") ?
i.e Namespace.Class

Kalpesh
Nov 25 '05 #2
>Are you passing in the fully qualified name to Type.GetType("MyNameSpace.MyClass") ?
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("System.Data.SqlClient.SqlCommand, " +
"System.Data, "+
"Version=1.0.5000.0, "+
"Culture=neutral, "+
"PublicKeyToken=b77a5c561934e089");
Console.WriteLine (type);
}
}
--
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
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.SubNameSpace.ContainingClass+NestedCl ass,MyAssembly

Right?

Kalpesh
Nov 26 '05 #7
<Kalpesh/> <ka*****@disc.microsoft.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.SubNameSpace.ContainingClass+NestedCl ass,MyAssembly


Yes, that's right.

--
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
Nov 26 '05 #8
Hmm, in which case this should work - but doesn't:

OpenFileDialog fd = new OpenFileDialog();
fd.ShowDialog(this);
Assembly a = Assembly.LoadFile(fd.FileName);
Type t= a.GetType("TestClass.FirstTestClass");
MessageBox.Show(t.AssemblyQualifiedName);
Type t2 = Type.GetType(t.AssemblyQualifiedName);
MessageBox.Show(t2.AssemblyQualifiedName);

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(this);
Assembly a = Assembly.LoadFile(fd.FileName);
Type t= a.GetType("TestClass.FirstTestClass");
MessageBox.Show(t.AssemblyQualifiedName);
Type t2 = Type.GetType(t.AssemblyQualifiedName);
MessageBox.Show(t2.AssemblyQualifiedName);

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.com>
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
Well, at least that means it can't be done, so I can stop trying.

I've found an alternative to GetType that should work in this case:

foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
Type t2 = assembly.GetType(TypeName);
if (t2 != null)
MessageBox.Show(t2.AssemblyQualifiedName);
}

So I'll pass that onto the framework people and see if they can
incorporate it.

Andy D

Nov 27 '05 #11
Well, I dont know - whether how good this is & applicable ?

How about AppDomain.CurrentDomain.CreateInstanceFromAndUnwra p ?
(this will load assembly from filesystem & create an instance of the type that you specify)

This will get you an instance of the class & you can call .GetType() on it

Does this help ?

Kalpesh
Nov 27 '05 #12
>Any ideas why?
Type.GetType expects a typeName parameter which, in it full form,
includes all the necessary info where to locate the type. Thus, when
you omit the assembly spec in typeName, the method makes use of
"defaults", and searches the calling assembly, then mscorlib.dll. The
reason it does not search other loaded assemblies is that it would
violate that parameter's semantics.

Thi
http://thith.blogspot.com

Nov 28 '05 #13
However, with dynamically loaded Types, it can't find them, no matter
what you pass in as the TypeName (see my example code a few entries
up).

Andy D

Nov 28 '05 #14
Hi Andy,

It does not matter whether you loaded the assembly or not. Type.GetType
uses some specific rules to look for the assembly (these rules are in
..NET documentation, search google for ""How the Runtime Locates
Assemblies"").

Thi - http://thith.blogspot.com

Nov 28 '05 #15
Cheers for that. It's a shame that Type.GetType can't do what I want -
thankfully iterating through the
CurrentDomain.Assemblies.GetType(ClassName) calls does the trick.

Andy D

Nov 28 '05 #16
Hi Andrew,

Check out my article, it might be helpful.

http://www.developersdex.com/gurus/articles/739.asp

Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

"You always have to look beyond the horizon and can never be complacent
-- God forbid we become complacent."

Jozef Straus

*** Sent via Developersdex http://www.developersdex.com ***

Nov 29 '05 #17

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

Similar topics

9
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...
0
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. ...
10
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...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
1
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...
5
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
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
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)...
15
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...
4
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.