将GetType和变量声明为接口类型 [英] GetType and variables declared as an interface type

查看:130
本文介绍了将GetType和变量声明为接口类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直困扰着我一段时间。


GetType不能用作接口类型的变量,我必须

DirectCast (somevariable,Object)。


例如:

Sub SomeSub(ByVal Dictionary as IDictionary)

如果字典是Nothing然后返回

Dim o as Object = DirectCast(Dictionary,Object)

如果GetType(ISomeInterface).IsAssignableFrom(o.GetType)那么

''< ...一些代码...>

结束如果

结束子


这意味着可以从Object中获得未派生的变量类型

?如果没有,为什么所有变量都不被视为对象?


Bob

解决方案

"鲍勃" <无***** @ nospam.net> schrieb

这一直困扰着我一段时间。

GetType不能用作接口类型的变量,我必须使用DirectCast(一些变量) ,对象)。

例如:

Sub SomeSub(ByVal Dictionary as IDictionary)
如果Dictionary是Nothing则返回
Dim o as Object = DirectCast(Dictionary,Object)


你不需要在这里直播:

dim o as object = dictionary

如果GetType(ISomeInterface).IsAssignableFrom(o.GetType)然后
''< ...一些代码...>
结束如果
结束子

这是否意味着可以使用不是从Object派生的变量类型?


No.

如果不是,为什么所有变量都不被视为对象?




接口没有GetType方法。

-

Armin

"鲍勃" <无***** @ nospam.net> schrieb:

GetType不适用于作为接口类型的十进制变量,我必须使用DirectCast(somevariable,Object)。


''DirectCast''到''对象''没有意义,因为''对象''是更多的b
一般类型。

这是否意味着可以使用不是从Object派生的变量类型?


No.

如果没有,为什么所有变量都不被视为对象?




无法实例化接口,不能有接口类型为

的对象。类可以实现接口,类可以实现实例化。


-

Herfried K. Wagner

MVP·VB Classic,VB.NET
http://www.mvps。 org / dotnet


嗨Bob,


请原谅我,如果这是无关紧要鲍勃,我不是我确实理解了这个问题所以对接口有点絮絮叨叨...


|| Sub SomeSub(ByVal Dictionary as IDictionary)


你不能用任何旧变量调用它 - 它必须是一个类实现给定接口的变量。在这种情况下,对于

示例,一个HashTable。

使用接口作为方法参数的一个目的是限制可以做什么变量。例如,

HashTable提供Clone,ContainsKey和ContainsValue等。这些不适用于Dictionary参数,因为

它们是HashTable特定的。


如果你有两个完全不同的对象,比如大象,它可能更清晰和BananaBalloon(如在海边)。这两个

都可能实现一个IPeopleCarrier接口,其中包含容量等属性。


你无法定义一个方法大象或BananaBalloon,但没有其他对象 - 除了

使用界面,例如Sub LoadUp(oCarrier As IPeopleCarrier)。


==== =============================

写完这篇文章,并在VB中玩,我不是'我很高兴我回答你,所以我决定在C#中尝试同样的事情。


||对于作为界面类型的变量,GetType不可用


是的,我很害怕。看看下面的C#代码:


static void Main()

{HmmmInterfaces(new Hashtable()); }


static void HmmmInterfaces(IDictionary d)

{

Hashtable h = new Hashtable();

类型th = h.GetType();

类型td = d.GetType(); //不能用VB编译它说

// GetType不是IDictionary的成员。

}


这是完全可以接受的C#和这段代码,td是HashTable。

在VB中,你不能在d上调用GetType。


现在我想我理解你的问题。 - 你怎么知道你在做什么?


嗯,你对DirectCast说得对。


这将是给HashTable

Dim td As Type = DirectCast(d,Object).GetType

如果td是GetType(Hashtable)那么


和TypeOf也可用。


这对于HashTable只有True

如果TypeOf d是Hashtable那么


这将始终为True(因为参数是IDictionary)

如果TypeOf d是IDictionary那么


||这是否意味着可以使用变量类型

||是不是派生自Object?


不,我只是认为虽然GetType在C#中可用,但它有一些任意的(即我无法理解它:-))原因,已经从VB中的接口中删除了



我希望这对你有用。


问候,

Fergus


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)
If GetType(ISomeInterface).IsAssignableFrom(o.GetType ) Then
''<... some code...>
End If
End Sub

Does this mean that it is possible to have variable types that are not derived
from Object? And if not, why aren''t all variables treated as Objects?

Bob

解决方案

"Bob" <no*****@nospam.net> schrieb

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)
You don''t need Directcast here:
dim o as object = dictionary
If GetType(ISomeInterface).IsAssignableFrom(o.GetType ) Then
''<... some code...>
End If
End Sub

Does this mean that it is possible to have variable types that are
not derived from Object?
No.
And if not, why aren''t all variables treated
as Objects?



The Interface does not have a GetType method.
--
Armin


"Bob" <no*****@nospam.net> schrieb:

GetType isn''t availble for variables decalred as interface
types, I have to DirectCast(somevariable, Object).
''DirectCast'' to ''Object'' doesn''t make sense, because ''Object'' is the more
general type.
Does this mean that it is possible to have variable types that
are not derived from Object?
No.
And if not, why aren''t all variables treated as Objects?



An interface cannot be instantiated, there cannot be objects with the type
of an interface. Classes can implement interfaces and classes can get
instantiated.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Hi Bob,

Pardon me if this is irrelevant Bob, I''m not sure I understand the question properly so a bit of a ramble about Interfaces...

|| Sub SomeSub(ByVal Dictionary as IDictionary)

You can''t just call this with any old variable - it has to be one whose class implements the given interface. In this case, for
example, a HashTable.

One of the purposes of using an interface for a method parameter is to restrict what can be done with the variable. For example,
the HashTable provides Clone, ContainsKey and ContainsValue, amongst others. These are not available to the Dictionary parameter as
they are HashTable specific.

It''s perhaps clearer if you have two disparate objects, say an Elephant and a BananaBalloon (as at the seaside). Both of these
might implement an IPeopleCarrier interface with such properties as Capacity, etc.

There''s no way that you could define a method which takes either an Elephant or a BananaBalloon but no other object - except by
using the interface, eg Sub LoadUp (oCarrier As IPeopleCarrier).

=================================
Having written this, and played around in VB, I wasn''t happy that I was answering you, so I decided to try the same in C#.

|| GetType isn''t availble for variables decalred as interface types

True, I''m afraid. Have a look at the following C# code:

static void Main()
{ HmmmInterfaces (new Hashtable()); }

static void HmmmInterfaces (IDictionary d)
{
Hashtable h = new Hashtable();
Type th = h.GetType();
Type td = d.GetType(); // Won''t compile in VB. It says that
// GetType is not a member of IDictionary.
}

This is perfectly acceptable C# and, with this code, td is HashTable.
In VB you can''t get call GetType on d.

So now I think I understand your question. - How are you supposed to know what you are dealing with?

Well, you''re right about the DirectCast.

This will give HashTable
Dim td As Type = DirectCast(d, Object).GetType
If td Is GetType(Hashtable) Then

And TypeOf is available too.

This will be True only for HashTable
If TypeOf d Is Hashtable Then

This will be always be True (as the parameter is an IDictionary)
If TypeOf d Is IDictionary Then

|| Does this mean that it is possible to have variable types that
|| are not derived from Object?

No, I just think that while GetType is available in C#, it has, for some arbitrary (ie I can''t fathom it :-)) reason, has been
removed from Interfaces in VB.
I hope this is of some use to you.

Regards,
Fergus


这篇关于将GetType和变量声明为接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆