有谁知道获取接口属性的基础属性名称的方法?
例如,以Juliet 的接口为例,我稍作修改:
Module1
Interface ILifeform
ReadOnly Property Name() As String
Sub Speak()
Sub Eat()
End Interface
Class Dog
Implements ILifeform
Public ReadOnly Property Name() As String Implements ILifeform.Name
Get
Return "Doggy!"
End Get
End Property
Public Sub Talk() Implements ILifeform.Speak
Console.WriteLine("Woof!")
End Sub
Public Sub Eat() Implements ILifeform.Eat
Console.WriteLine("Yum, doggy biscuits!")
End Sub
End Class
Class Ninja
Implements ILifeform
Public ReadOnly Property Name() As String Implements ILifeform.Name
Get
Return "Ninja!!"
End Get
End Property
Public Sub Speak() Implements ILifeform.Speak
Console.WriteLine("Ninjas are silent, deadly killers")
End Sub
Public Sub Eat() Implements ILifeform.Eat
Console.WriteLine("Ninjas don't eat, they wail on guitars and kick ass")
End Sub
End Class
Class Monkey
Implements ILifeform
Public ReadOnly Property Name() As String Implements ILifeform.Name
Get
Return "Monkey!!!"
End Get
End Property
Public Sub Speak() Implements ILifeform.Speak
Console.WriteLine("Ook ook")
End Sub
Public Sub Eat() Implements ILifeform.Eat
Console.WriteLine("Bananas!")
End Sub
End Class
Sub Main()
Dim lifeforms As ILifeform() = New ILifeform() {New Dog(), New Ninja(), New Monkey()}
For Each x As ILifeform In lifeforms
HandleLifeform(x)
Next
Console.ReadKey(True)
End Sub
Sub HandleLifeform(ByVal x As ILifeform)
Console.WriteLine("Handling lifeform '{0}'", x.Name)
x.Speak()
x.Eat()
Console.WriteLine()
End Sub
End Module
对于 Dog 类,实现“Speak”的属性称为“Talk”。
如果我收到一个作为“对象”类型的狗对象,我可以做这样的事情吗?
If TypeOf(object) Is ILifeForm Then
Dim str as string = CType(object, ILifeForm).GetUnderLyingPropertyName("Speak"))
' str now contains "Talk"
End If