By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,162 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,162 IT Pros & Developers. It's quick & easy.

GetType() ??

P: n/a
Hi,

I've been trying to load a type and and get all of the methods within
that type. I finally made it happen by using typeof, but im really
stumped how could one implement the same with Type.GetType(). I have
read the documentation and it says that this looks currently executing
assembly and mscorlib.dll. I have couple of questions in this regard i
hope someone will take some time off to answer them and i will
appreciate that.

1-I finally succeeded by using the following code

Type t=typeof(System.Windows.Forms.Button);

MethodInfo[] methods=t.GetMethods();
foreach(MethodInfo in nextmethod in methods)
{
Console.WriteLine(nextmethod.Name)
}

NOW why i can't do same as above using Type.GetType() ? If i can
acheive what i am trying to do above by using Type.GetType() then
please let me know how?

2--By using Google , on various places i have seen people advising that
one must use fully qualified assembly name. How could i get a fully
qualified name by using Type.GetType( ) e.g. if i use following i get
an error
Type t=Type.GetType("System.Windows.Forms.Button");
Console.WriteLine(t.AssemblyQualifiedName);

3-I tried this way but failed , i just would like to know why it
failed. First i loaded an Assembly using Assembly.Load() then i tried
to get the type using Assemblyinstance.GetType("abc"), allright i can't
explain it here just see the code below :)

Assembly asm=Assembly.Load("System.Windows.Forms");
Type asm=asm.GetType("System.Windows.Forms.Button");
MethodInfo[] methods=asm.GetMethods();
foreach(MethodInfo in nextmethod in methods)
{
Console.WriteLine(nextmethod.Name)
}

why the above code *doesn't* show me the methods of type
System.windows.forms.button , i means what is wrong with it ?
I will really appreciate any help.
Thanks in advance.

-Erland

May 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Erland" <Er************@gmail.com> wrote:
I've been trying to load a type and and get all of the methods within
that type. [snip] NOW why i can't do same as above using Type.GetType() ? If i can
acheive what i am trying to do above by using Type.GetType() then
please let me know how?
You can. You have to qualify the type name by assembly, version, culture
and public key token, however. This requirement seems to be particularly
prevalent with .NET 2.0 assemblies that are also .NET 1.1/1.0
assemblies. With my own assemblies, I've rarely had to do more than
simply qualify the type name with just the assembly display name.

---8<---
using System;
using System.Reflection;

class App
{
static void Main(string[] args)
{
foreach (MethodInfo m in Type.GetType(args[0]).GetMethods())
Console.WriteLine(m.Name);
}
}
--->8---

Calling this 'Test', and running this with (without wrapping):

---8<---
../Test 'System.Windows.Forms.Button, System.Windows.Forms,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
--->8---

will list all the methods of Button. There's very little you can leave
out.
2--By using Google , on various places i have seen people advising that
one must use fully qualified assembly name. How could i get a fully
qualified name by using Type.GetType( ) e.g. if i use following i get
an error
The reason this fails is because the assembly in this line isn't fully
qualified:
Type t=Type.GetType("System.Windows.Forms.Button"); Console.WriteLine(t.AssemblyQualifiedName);
You can discover the full name of an assembly by passing its path to a
utility program such as this one:

---8<---
using System;
using System.Reflection;

class App
{
static void Main(string[] args)
{
Console.WriteLine(Assembly.LoadFrom(args[0]).GetName());
}
}
--->8---
3-I tried this way but failed , i just would like to know why it
failed. First i loaded an Assembly using Assembly.Load() then i tried
to get the type using Assemblyinstance.GetType("abc"), allright i can't
explain it here just see the code below :)

Assembly asm=Assembly.Load("System.Windows.Forms");


You have to qualify the assembly, in a similar way to the type:

---8<---
Assembly.Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089");
--->8---

-- Barry
May 14 '06 #2

P: n/a
"Erland" wrote...
2--By using Google , on various places i have seen people
advising that one must use fully qualified assembly name.
How could i get a fully qualified name by using Type.GetType( )
e.g. if i use following i get an error
Type t=Type.GetType("System.Windows.Forms.Button");
Console.WriteLine(t.AssemblyQualifiedName);
You'll need to *know* the qualified name in order to use Type.GetType(...).

Here you tried it the other way around...

It's possible to use, but the qualified name of the assembly is pretty long.
This is an example of how it could look in .NET 2.0:

Type.GetType(
"System.Windows.Forms.Button, " +
"System.Windows.Forms, " +
"Version=2.0.0.0, " +
"Culture=Neutral, " +
"PublicKeyToken=b77a5c561934e089"));

Nota Bene! The qualifield assembly name is hence not only the "name" of the
assembly, but includes the version, culture and Public key token as well!
3-I tried this way but failed , i just would like to know why it
failed. First i loaded an Assembly using Assembly.Load() then i tried
to get the type using Assemblyinstance.GetType("abc"), allright i can't
explain it here just see the code below :)

Assembly asm=Assembly.Load("System.Windows.Forms");
The version of Assembly.Load that takes a string argument must have the
qualified name, just as Type.GetType:

Assembly.Load(
"System.Windows.Forms, " +
"Version=2.0.0.0, " +
"Culture=Neutral, " +
"PublicKeyToken=b77a5c561934e089");
Thanks in advance.


You're welcome.

/// Bjorn A
May 14 '06 #3

P: n/a
Hello Erland,

if the class name is known at compile time, use typeof in lue of the Type.GetType()

E> I've been trying to load a type and and get all of the methods within
E> that type. I finally made it happen by using typeof, but im really
E> stumped how could one implement the same with Type.GetType(). I have
E> read the documentation and it says that this looks currently
E> executing assembly and mscorlib.dll. I have couple of questions in
E> this regard i hope someone will take some time off to answer them and
E> i will appreciate that.
E>
E> 1-I finally succeeded by using the following code
E>
E> Type t=typeof(System.Windows.Forms.Button);
E>
E> MethodInfo[] methods=t.GetMethods();
E> foreach(MethodInfo in nextmethod in methods)
E> {
E> Console.WriteLine(nextmethod.Name)
E> }
E> NOW why i can't do same as above using Type.GetType() ? If i can
E> acheive what i am trying to do above by using Type.GetType() then
E> please let me know how?
E>
E> 2--By using Google , on various places i have seen people advising
E> that
E> one must use fully qualified assembly name. How could i get a fully
E> qualified name by using Type.GetType( ) e.g. if i use following i get
E> an error
E> Type t=Type.GetType("System.Windows.Forms.Button");
E> Console.WriteLine(t.AssemblyQualifiedName);
E> 3-I tried this way but failed , i just would like to know why it
E> failed. First i loaded an Assembly using Assembly.Load() then i tried
E> to get the type using Assemblyinstance.GetType("abc"), allright i
E> can't explain it here just see the code below :)
E>
E> Assembly asm=Assembly.Load("System.Windows.Forms"); Type
E> asm=asm.GetType("System.Windows.Forms.Button");
E>
E> MethodInfo[] methods=asm.GetMethods();
E> foreach(MethodInfo in nextmethod in methods)
E> {
E> Console.WriteLine(nextmethod.Name)
E> }
E> why the above code *doesn't* show me the methods of type
E> System.windows.forms.button , i means what is wrong with it ?
E>
E> I will really appreciate any help.
E> Thanks in advance.
E> -Erland
E>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.