473,609 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetType() ??

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.GetMe thods();
foreach(MethodI nfo in nextmethod in methods)
{
Console.WriteLi ne(nextmethod.N ame)
}

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.WriteLi ne(t.AssemblyQu alifiedName);

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 Assemblyinstanc e.GetType("abc" ), allright i can't
explain it here just see the code below :)

Assembly asm=Assembly.Lo ad("System.Wind ows.Forms");
Type asm=asm.GetType ("System.Window s.Forms.Button" );
MethodInfo[] methods=asm.Get Methods();
foreach(MethodI nfo in nextmethod in methods)
{
Console.WriteLi ne(nextmethod.N ame)
}

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
3 2066
"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.Reflecti on;

class App
{
static void Main(string[] args)
{
foreach (MethodInfo m in Type.GetType(ar gs[0]).GetMethods())
Console.WriteLi ne(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= b77a5c561934e08 9'
--->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.WriteLi ne(t.AssemblyQu alifiedName);
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.Reflecti on;

class App
{
static void Main(string[] args)
{
Console.WriteLi ne(Assembly.Loa dFrom(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 Assemblyinstanc e.GetType("abc" ), allright i can't
explain it here just see the code below :)

Assembly asm=Assembly.Lo ad("System.Wind ows.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= b77a5c561934e08 9");
--->8---

-- Barry
May 14 '06 #2
"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.WriteLi ne(t.AssemblyQu alifiedName);
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=Neutra l, " +
"PublicKeyToken =b77a5c561934e0 89"));

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 Assemblyinstanc e.GetType("abc" ), allright i can't
explain it here just see the code below :)

Assembly asm=Assembly.Lo ad("System.Wind ows.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=Neutra l, " +
"PublicKeyToken =b77a5c561934e0 89");
Thanks in advance.


You're welcome.

/// Bjorn A
May 14 '06 #3
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.GetMe thods();
E> foreach(MethodI nfo in nextmethod in methods)
E> {
E> Console.WriteLi ne(nextmethod.N ame)
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.WriteLi ne(t.AssemblyQu alifiedName);
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 Assemblyinstanc e.GetType("abc" ), allright i
E> can't explain it here just see the code below :)
E>
E> Assembly asm=Assembly.Lo ad("System.Wind ows.Forms"); Type
E> asm=asm.GetType ("System.Window s.Forms.Button" );
E>
E> MethodInfo[] methods=asm.Get Methods();
E> foreach(MethodI nfo in nextmethod in methods)
E> {
E> Console.WriteLi ne(nextmethod.N ame)
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
46043
by: ron | last post by:
Hi, I am currently using the base class GetType() in the following way and was woundering if there was a different way of looking at types and doing a comparison for equality. Thanks Ron foreach(System.Web.UI.Control ctl in this.Controls) {
9
7994
by: Yan Vinogradov | last post by:
Hi, Turns out it's possible to spoof another type with Object.GetType method. If you do this: namespace N { class C { public new Type GetType() { return (String.Empty.GetType()); }
10
11776
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal Dictionary as IDictionary) If Dictionary is Nothing Then Return Dim o as Object = DirectCast(Dictionary, Object)
5
2259
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2 = "otherText" xmlSave("C:\folder\file.xml", mySettings) Here is the sub: Public Shared Sub xmlSave(ByVal path As String, ByVal config As
4
15103
by: Greg Burns | last post by:
What the difference between these two? System.Type.GetType("System.Int32") and GetType(Integer) Or more specifically, why does GetType(Integer) work, but not System.Type.GetType(Integer)? What namespace is the GetType function(?) part of?
4
2857
by: Howard Kaikow | last post by:
For the code below, for both appWord and gappWord, I get the error "Public member 'GetType' on type 'ApplicationClass' not found" I realize the test for appWord is superflous as the parameter is passed in as a known type, but gappWord is has a scope of the class, so a test of the type is valid (making believe the sub does not know the pre-ordained type). TypeOf returns Word.Application. TypeName returns ApplicationClass.
3
4458
by: Joe Adams | last post by:
Hi All, How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>) I need to now if the <MyType> is the same type of class as the <GenericType> without having to add the generic type member members. i.e. I do not want to use GetType(<GenericType>(Of String).IsAssignableFrom(<MyType>)
7
7805
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
2
2510
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer) host.GetService(typeof(IContainer)); IComponent component1 = container1.Components; PropertyInfo info1 = component1.GetType().GetProperty(this.member.Name);
0
8557
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8512
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8203
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8378
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6981
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6047
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4007
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1373
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.