反思问题 [英] reflection question
问题描述
您好 - 我想要打印出是。在
的类旁边实现Interface01,否则为No。这是我的代码。它使用空引用异常崩溃了
,我不知道为什么。想法?
谢谢你的帮助。
Tony
Imports System.Reflection
公开Class Form1
继承System.Windows.Forms.Form
''''Windows窗体设计器生成的代码
接口接口01
结束接口
MustInherit类Abstract01
结束类
Class Class01
Implements Interface01
结束班
Class Class02
结束班
Class Class03
继承Class01
结束班级
Class ClassA2
继承Abstract01
结束类
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System .EventArgs)处理Button1.Click
Dim asmbly As [Assembly] = [Assembly] .GetExecutingAssembly()
Dim asmblyType As Type
For每个asmblyType在asmbly.GetTypes中
如果是asmblyTy pe.BaseType.Equals(GetType(Interface01))然后
Debug.Write(" Yes:")
Else:Debug.Write(" No:" ;)
结束如果
Debug.WriteLine(asmblyType.FullName)
下一页
结束子>
结束班
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
推荐答案
2003年10月3日15:06:28 -0700,到**************** @ hotmail.com (安东尼)
写道:
On 3 Oct 2003 15:06:28 -0700, to****************@hotmail.com (Antony)
wrote:
如果asmblyType.BaseType.Equals(GetType(Interface01))那么
Debug.Write(" Yes:")
If asmblyType.BaseType.Equals(GetType(Interface01)) Then
Debug.Write("Yes: ")
没有基本类型会导致您的问题。改为使用Type.GetInterfaces()
集合。
No base type would cause your problem. Use the Type.GetInterfaces()
collection instead.
嗨Antony,
此外:
SomeType.BaseType将为您提供< class> SomeType继承自。
它永远不会给你一个接口,因为这些不能被继承。
问候,
Fergus
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
实现的接口不是基本类型,因为您可以根据需要实现多个接口:
>
公共类Class01
实现Interface01
实现Interface02
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
这篇关于反思问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!