[已解决问题] 求接口使用getType的方法的问题
提问时间: 2007-12-27 16:14
悬赏分:20 浏览:657 次

C#代码如下:
    public interface IConfigInfo
    {
    }
    public class A : IConfigInfo
    {

    }
    public class b
    {
        void bb(IConfigInfo a)
        {
            Type t = a.GetType();
        }
    }

VB.net代码如下:
Public Interface IConfigInfo
End Interface
Public Class A
    Implements IConfigInfo
   
End Class
Public Class b
    Private Sub bb(ByVal a As IConfigInfo)
        Dim t As Type = a.GetType()
    End Sub
End Class
请大家注意红色部分,其中C#代码运行正常,而vb的哪个就是不行,错误原因为:
a.GetType() 提示:GetType不是IConfigInfo 的成员,求助大家解决办法

提问者:没剑 - 菜鸟二级
最佳答案
如果你不想转的话你在可以在接口里定义GetType方法,,,
好久没用vb了,刚才还想在vb里试试的,不过却发现vs里竟然没选装vb,晕
2007/12/27 16:55:51 回答者:t-mac.NET


提问者对于答案的评价:t-mac.NET : 你的想法很不错啊,只是有一个错要修改一下. Public Function GetType1() As System.Type Implements IConfig.GetType Return Me.GetType() End Function --- 这里的:Me.GetType()修改为:MyBase.GetType才可以正常运行
其它回答(4)
把a 先转成object试试.
7个月前   回答者:沙加 - 老鸟四级
好久没用VB,不记得了
7个月前   回答者:BirdsHover - 菜鸟二级
在VB.NET中,接口不能从Object类中获取内容
7个月前   回答者:Anders Cui - 小虾三级
Dim o As Object = i
Dim t As Type = o.GetType

这个估计是编译器问题,在VB里面隐藏了接口的基类,认为接口没有Gettype方法。
所以,只能用Object包装一下在用了
7个月前   回答者:G yc {Son of VB} - 小虾三级
评论
7个月前   没剑 :
TNND胸,  
Dim t As Type = ctype(a,object).GetType() 

不过为什么要这样子呢?有解释的方法吗?
7个月前   沙加 :
这个想象是两种语言下的编译器行为的差异吧.
7个月前   没剑 :
如果你不想转的话你在可以在接口里定义GetType方法,,,
---
t-mac.NET 怎么样在接口里定义gettype方法?
7个月前   t-mac.NET :
Public Interface IConfig
    Function [GetType]() As System.Type
End Interface

Public Class Config
    Implements IConfig

    Public Function GetType1() As System.Type Implements IConfig.GetType
        Return Me.GetType()
    End Function

    Public Shared Sub test()
        Dim config As IConfig
        Dim type As Type = config.GetType()
    End Sub
End Class

以上代码是1.1的,

由于C#和vb的接口实现是不一样的,在vb中必须带有Implements IConfig.GetType语句,而C#中只要实现类实现了即可,即使是父类的方法也没关系
7个月前   没剑 :
t-mac.NET : 你的想法很不错啊,只是有一个错要修改一下.
Public Function GetType1() As System.Type Implements IConfig.GetType
Return Me.GetType()
End Function
---
这里的:Me.GetType()修改为:MyBase.GetType才可以正常运行
7个月前   t-mac.NET :
to 没剑 

不好意思哈,,,我已经2年没用vb了,,疏忽了
   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除