如何确定基接口??? [英] How to determine base interface???
问题描述
假设如下:
class MyContainer:System.Collections.CollectionBase
{
// ... < br $>
}
(其中CollectionBase实现IList,ICollection)
如何确定某种类型(例如MyContainer)派生自IList?
System.Type type = typeof(MyContainer);
类型是IList - > false
type.IsSubclassOf(typeof(IList)) - > false
type.IsAssignableFrom(typeof(IList)) - > false
等,所有测试都返回false。
所以,我需要的是一种确定特定类型是否来自IList的方法,
无论它的层次结构有多远。
谢谢
Suppose the following:
class MyContainer : System.Collections.CollectionBase
{
//...
}
(where CollectionBase implements IList, ICollection)
How do I determine if a type (such as MyContainer) derives from IList?
System.Type type = typeof(MyContainer);
type is IList -> false
type.IsSubclassOf(typeof(IList)) -> false
type.IsAssignableFrom(typeof(IList)) -> false
etc., all tests return false.
So, what I need is a way to determine if a particular type derives from IList,
no matter how far up the hierarchy it is.
Thanks
推荐答案
嗨!
怎么样?这太过分了吗?
// ***
IList lst =类型为IList;
if(lst!= null)
Console.WriteLine(类型确实实现IList);
// ***
-
最好的问候
Yanick
" J.琼斯" < jj@networld.com> écritdansle message de
news:e2 ************** @ TK2MSFTNGP14.phx.gbl ...
Hi!
how about that ? is that too much overkill ?
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLine("type does implement IList");
//***
--
Best Regards
Yanick
"J. Jones" <jj@networld.com> a écrit dans le message de
news:e2**************@TK2MSFTNGP14.phx.gbl...
假设以下:
类MyContainer:System.Collections.CollectionBase
{
// ...
}
(其中CollectionBase实现IList ,ICollection)
如何确定某个类型(如MyContainer)是否来自IList?
System.Type type = typeof(MyContainer);
类型是IList - > false
type.IsSubclassOf(typeof(IList)) - > false
type.IsAssignableFrom(typeof(IList)) - >错误
等等,所有测试都返回false。
所以,我需要的是一种确定特定类型是否来自
IList的方法,无论如何它的层次结构很远。
谢谢
Suppose the following:
class MyContainer : System.Collections.CollectionBase
{
//...
}
(where CollectionBase implements IList, ICollection)
How do I determine if a type (such as MyContainer) derives from IList?
System.Type type = typeof(MyContainer);
type is IList -> false
type.IsSubclassOf(typeof(IList)) -> false
type.IsAssignableFrom(typeof(IList)) -> false
etc., all tests return false.
So, what I need is a way to determine if a particular type derives from IList, no matter how far up the hierarchy it is.
Thanks
J. Jones写道:
J. Jones wrote:
如何确定某个类型(如MyContainer)是否来自IList?
How do I determine if a type (such as MyContainer) derives from IList?
使用C#is语句:
if(MyContainer是IList){
//处理IList实现
}
Anders Nor?s
http://dotnetjunkies.com/weblog/anoras/
Zoury写道:
嗨!
那怎么样?那太过分了吗?
?!
// ***
IList lst =类型为IList;
if(lst!= null)<控制台.WriteLine(类型确实实现IList);
// ***
Hi!
how about that ? is that too much overkill ?
?!
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLine("type does implement IList");
//***
不行。 IList不是一个类型,而是一个类(接口)。你不能将类型
转换成一个实例,如果你的代码允许会发生什么。
No go. IList isn''t a type, but a class (interface). You can''t convert a type
to an instance, what is would happen if permissible in your code.
这篇关于如何确定基接口???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!