473,396 Members | 2,011 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,396 software developers and data experts.

reflection question

Hello - I am wanting to print out a "Yes" next to classes that
implement "Interface01", otherwise a "No". Here is my code. It crashes
with a null reference exception and I am not sure why. Ideas?
Thank you for you help.
Tony
Imports System.Reflection

Public Class Form1
Inherits System.Windows.Forms.Form

''Windows Form Designer generated code

Interface Interface01
End Interface

MustInherit Class Abstract01
End Class

Class Class01
Implements Interface01
End Class

Class Class02
End Class

Class Class03
Inherits Class01
End Class

Class ClassA2
Inherits Abstract01
End Class

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim asmbly As [Assembly] = [Assembly].GetExecutingAssembly()
Dim asmblyType As Type
For Each asmblyType In asmbly.GetTypes
If asmblyType.BaseType.Equals(GetType(Interface01)) Then
Debug.Write("Yes: ")
Else : Debug.Write("No: ")
End If
Debug.WriteLine(asmblyType.FullName)
Next
End Sub
End Class
Nov 20 '05 #1
12 2328
On 3 Oct 2003 15:06:28 -0700, to****************@hotmail.com (Antony)
wrote:
If asmblyType.BaseType.Equals(GetType(Interface01)) Then
Debug.Write("Yes: ")


No base type would cause your problem. Use the Type.GetInterfaces()
collection instead.

Nov 20 '05 #2
Hi Antony,

Furthermore:

SomeType.BaseType will give you the <class> that SomeType inherited from.
It will never give you an interface as these cannot be inherited.

Regards,
Fergus
Nov 20 '05 #3
Hi, an implemented interface isn't a base type, because you can implement as
many interfaces as you want:

Public Class Class01
Implements Interface01
Implements Interface02
Nov 20 '05 #4
Andy, Fergus - that is clear now. Thank you.
Tony

"Fergus Cooney" <fi******@tesco.net> wrote in message news:<eO**************@TK2MSFTNGP10.phx.gbl>...
Hi Antony,

Furthermore:

SomeType.BaseType will give you the <class> that SomeType inherited from.
It will never give you an interface as these cannot be inherited.

Regards,
Fergus

Nov 20 '05 #5
Thanks Tom.

Just one more thing, if I modify my code (below) it prints out the
names of all classes etc. in a /*project*/. Is it possible to print
out the names of all classes using similar code in a /*solution*/ ?

Thank you
Tony
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim asmbly As [Assembly] = [Assembly].GetExecutingAssembly
Dim asmblyType As Type
For Each asmblyType In asmbly.GetTypes
Debug.WriteLine(asmblyType.FullName)
Next
End Sub


"Tom Spink" <th**********@ntlworld.com> wrote in message news:<en**************@TK2MSFTNGP10.phx.gbl>...
Hi, an implemented interface isn't a base type, because you can implement as
many interfaces as you want:

Public Class Class01
Implements Interface01
Implements Interface02
.
.
.
Implements Interface99
End Class

So, your code should be:

' /// (See footnotes to convert to VS2002)
Private Sub MySub()
Dim asmThis As [Assembly] = [Assembly].GetExecutingAssembly()
Dim blnFoundInterface As Boolean

For Each typType As Type In asmThis.GetTypes

blnFoundInterface = False

For Each typInterface As Type In typType.GetInterfaces
If typInterface Is GetType(Interface01) Then
blnFoundInterface = True
Exit For
End If
Next

If blnFoundInterface Then
Debug.Write("Yes: ")
Else
Debug.Write("No: ")
End If

Debug.WriteLine(typType.FullName)

Next

End Sub
' ///

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
Antony schriebt,
: Hello - I am wanting to print out a "Yes" next to classes that
: implement "Interface01", otherwise a "No". Here is my code. It crashes
: with a null reference exception and I am not sure why. Ideas?
: Thank you for you help.
: Tony
:
:
: Imports System.Reflection
:
: Public Class Form1
: Inherits System.Windows.Forms.Form
:
: ''Windows Form Designer generated code
:
: Interface Interface01
: End Interface
:
: MustInherit Class Abstract01
: End Class
:
: Class Class01
: Implements Interface01
: End Class
:
: Class Class02
: End Class
:
: Class Class03
: Inherits Class01
: End Class
:
: Class ClassA2
: Inherits Abstract01
: End Class
:
: Private Sub Button1_Click(ByVal sender As System.Object, _
: ByVal e As System.EventArgs) Handles Button1.Click
: Dim asmbly As [Assembly] = [Assembly].GetExecutingAssembly()
: Dim asmblyType As Type
: For Each asmblyType In asmbly.GetTypes
: If asmblyType.BaseType.Equals(GetType(Interface01)) Then
: Debug.Write("Yes: ")
: Else : Debug.Write("No: ")
: End If
: Debug.WriteLine(asmblyType.FullName)
: Next
: End Sub
: End Class

Nov 20 '05 #6
One Project is one assembly, a solution can contain many assemblies. You'd
have to perform reflection on all referenced assemblies, by dynamically
loading them.

If your project doesn't reference an assembly, (and you are using .NET 1.1)
then you can call [Assembly].LoadFile to load it into an Assembly object,
then run the code on that assembly.

If you are using .NET 1.0, there is the .Load method, which requires the
fully qualified name of the Assembly.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Antony" <to****************@hotmail.com> wrote in message
news:37**************************@posting.google.c om...
: Thanks Tom.
:
: Just one more thing, if I modify my code (below) it prints out the
: names of all classes etc. in a /*project*/. Is it possible to print
: out the names of all classes using similar code in a /*solution*/ ?
:
: Thank you
: Tony
:
:
: Private Sub Button1_Click(ByVal sender As System.Object, _
: ByVal e As System.EventArgs) Handles Button1.Click
: Dim asmbly As [Assembly] = [Assembly].GetExecutingAssembly
: Dim asmblyType As Type
: For Each asmblyType In asmbly.GetTypes
: Debug.WriteLine(asmblyType.FullName)
: Next
: End Sub
:
:
:
:
:
:
: "Tom Spink" <th**********@ntlworld.com> wrote in message
news:<en**************@TK2MSFTNGP10.phx.gbl>...
: > Hi, an implemented interface isn't a base type, because you can
implement as
: > many interfaces as you want:
: >
: > Public Class Class01
: > Implements Interface01
: > Implements Interface02
: > .
: > .
: > .
: > Implements Interface99
: > End Class
: >
: > So, your code should be:
: >
: > ' /// (See footnotes to convert to VS2002)
: > Private Sub MySub()
: > Dim asmThis As [Assembly] = [Assembly].GetExecutingAssembly()
: > Dim blnFoundInterface As Boolean
: >
: > For Each typType As Type In asmThis.GetTypes
: >
: > blnFoundInterface = False
: >
: > For Each typInterface As Type In typType.GetInterfaces
: > If typInterface Is GetType(Interface01) Then
: > blnFoundInterface = True
: > Exit For
: > End If
: > Next
: >
: > If blnFoundInterface Then
: > Debug.Write("Yes: ")
: > Else
: > Debug.Write("No: ")
: > End If
: >
: > Debug.WriteLine(typType.FullName)
: >
: > Next
: >
: > End Sub
: > ' ///
: >
: > --
: > HTH,
: > -- Tom Spink, Über Geek
: >
: > Please respond to the newsgroup,
: > so all can benefit
: >
: > " System.Reflection Master "
: >
: > ==== Converting to 2002 ====
: > Remove inline declarations
: >
: >
: > Antony schriebt,
: > : Hello - I am wanting to print out a "Yes" next to classes that
: > : implement "Interface01", otherwise a "No". Here is my code. It crashes
: > : with a null reference exception and I am not sure why. Ideas?
: > : Thank you for you help.
: > : Tony
: > :
: > :
: > : Imports System.Reflection
: > :
: > : Public Class Form1
: > : Inherits System.Windows.Forms.Form
: > :
: > : ''Windows Form Designer generated code
: > :
: > : Interface Interface01
: > : End Interface
: > :
: > : MustInherit Class Abstract01
: > : End Class
: > :
: > : Class Class01
: > : Implements Interface01
: > : End Class
: > :
: > : Class Class02
: > : End Class
: > :
: > : Class Class03
: > : Inherits Class01
: > : End Class
: > :
: > : Class ClassA2
: > : Inherits Abstract01
: > : End Class
: > :
: > : Private Sub Button1_Click(ByVal sender As System.Object, _
: > : ByVal e As System.EventArgs) Handles Button1.Click
: > : Dim asmbly As [Assembly] = [Assembly].GetExecutingAssembly()
: > : Dim asmblyType As Type
: > : For Each asmblyType In asmbly.GetTypes
: > : If asmblyType.BaseType.Equals(GetType(Interface01)) Then
: > : Debug.Write("Yes: ")
: > : Else : Debug.Write("No: ")
: > : End If
: > : Debug.WriteLine(asmblyType.FullName)
: > : Next
: > : End Sub
: > : End Class
Nov 20 '05 #7
Hi Tom,

Is there any class which can provide a list of the Assemblies within the
Soloution?

Regards,
Fergus
Nov 20 '05 #8
Hi Fergus,

A Solution isn't compiled into anything, a solution has no meaning in .NET,
it is simply a Visual Studio term for a collection of projects. A Project
could be an assembly, e.g. a C#, J#, VB.NET assembly, or it could be
something completely different like a Help File.

So basically, no, because a solution means nothing in .NET.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:ua*************@TK2MSFTNGP11.phx.gbl...
: Hi Tom,
:
: Is there any class which can provide a list of the Assemblies within
the
: Soloution?
:
: Regards,
: Fergus
:
:
Nov 20 '05 #9
Hi Fergus, yes this is what I am looking for too.

I have a "good feeling" about:

For Each aAll As [Assembly] In AppDomain.CurrentDomain.GetAssemblies()
'Code in here
Next

but I can't get it going and my underlying feeling is that Tom is on
the right when he says "No".

Cheers
Tony
"Fergus Cooney" <fi******@tesco.net> wrote in message news:<ua*************@TK2MSFTNGP11.phx.gbl>...
Hi Tom,

Is there any class which can provide a list of the Assemblies within the
Soloution?

Regards,
Fergus

Nov 20 '05 #10
Hi Tom,

Maybe I should have said 'executable' or 'application' then, for it is the
collection of Assemblies that make up a program that I'm thinking about.

Regards,
Fergus
Nov 20 '05 #11
Hi Tony,

Lol, actually this is out of my area but I was asking on your behalf as I
knew that this was what you were after. :-)

Having just seen Tom's answer, I wanted to get in there quick and steer
him right. I do this quite often (in the group generally - not with Tom). This
time I chose to do it as a question but my arrow fell short. :-((

I think the problem with GetAssemblies is that it will get those that have
been loaded so far, not the entire list. I would hope that there's a way to
get them all. It makes good sense that the information is there somewhere but
it may be distributed rather than centralised.

What do you reckon Tom? ;-)

Regards,
Fergus
Nov 20 '05 #12

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:ur*************@TK2MSFTNGP11.phx.gbl...
Hi Tony,

Lol, actually this is out of my area but I was asking on your behalf as I knew that this was what you were after. :-)

Having just seen Tom's answer, I wanted to get in there quick and steer him right. I do this quite often (in the group generally - not with Tom). This time I chose to do it as a question but my arrow fell short. :-((

I think the problem with GetAssemblies is that it will get those that have been loaded so far, not the entire list. I would hope that there's a way to get them all. It makes good sense that the information is there somewhere but it may be distributed rather than centralised.

Well, might as well through my hat into the ring as I've recently worked
with reflection.

Now, regarding the first question of a collection of the assemblies that
make up an "application" being stored somewhere, Tom's answer is dead on.
Or at least I think the best way to accomplish the task with the way most
people program. That is, you have a primary executable which may call a
series of libraries held in other assemblies. These are all usually loaded
at executation of the main assembly. The only time its not, is if your
doing something like modular coding where you use reflection to dynmiaclly
load assemblies at different times in the programs lifecycle.

If that is true, you could always recusivly push through each reference of
the primary assembly. Use GetReferencedAssemblies and
GetSatelliteAssemblies (if needed). Which both return collections of
assembly references. So, unless you are dynamically invoking methods your
program knows absolutly nothing about from assemblies it knows absolutly
nothing about, (I mean no interfaces either, kinda like a really really
really dynamic unit tester... hmmmmm.. interesting idea). there should be
no need other than those two methods. And as said, going through them
recursivly will tell you every single assembly you want to know about.

Hope it helps, keep the peace, and keep on rockin in the freeworld ted
nugent style.

-CJ
What do you reckon Tom? ;-)

Regards,
Fergus

Nov 20 '05 #13

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

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
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...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.