c# GetInterfaces() 获得不了自己实现的接口

Dispo 2009-02-23 01:33:02
我自己写的类实现了一个IPassengerCarrier()接口,可是在用这个“类.GetInterfaces()”,却获得不了自己的这个类这个接口IPassengerCarrier()
只获得以下的接口:
{Name = "IComparable" FullName = "System.IComparable"}
{Name = "ICloneable" FullName = "System.ICloneable"}
{Name = "IConvertible" FullName = "System.IConvertible"}
{Name = "IComparable`1" FullName = "System.IComparable`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
{Name = "IEnumerable`1" FullName = "System.Collections.Generic.IEnumerable`1[[System.Char, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
{Name = "IEnumerable" FullName = "System.Collections.IEnumerable"}
{Name = "IEquatable`1" FullName = "System.IEquatable`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}


不知道是为什么了?
...全文
393 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dispo 2009-02-23
  • 打赏
  • 举报
回复
Perfect
给分!
Dispo 2009-02-23
  • 打赏
  • 举报
回复
等等 我试试
  • 打赏
  • 举报
回复
Type type=typeof(SUV);
Type[] typeInterface =type.GetType().GetInterfaces();

改成
Type type=typeof(SUV);
Type[] typeInterface =type.GetInterfaces();

你那样写是获取Type的所有接口,而不是你定义的类的所有接口。
Dispo 2009-02-23
  • 打赏
  • 举报
回复

namespace Vehicles
{
public class Vehicle { }
public class Car : Vehicle { }
public class Train : Vehicle { }

public class Compact : Car, IPassengerCarrier {
}
public class SUV : Car, IPassengerCarrier, IHeavyLoadCarrier { }
public class Pickup : Car, IPassengerCarrier { }

public class PassengerTrain : Train, IPassengerCarrier { }
public class FreightTrain : Train, IHeavyLoadCarrier { }
public class D424DoubleBogey : Train { }

public interface IPassengerCarrier {
}
public interface IHeavyLoadCarrier { }
}

在另一个类中 写了

Type type=typeof(SUV);
Type[] typeInterface =type.GetType().GetInterfaces();

这样就出现上面的情况,而没有IPassengerCarrier, IHeavyLoadCarrier 这两个接口。
  • 打赏
  • 举报
回复
把你写的代码贴出来看看,否则还不知道你是否去继承了你写的接口么
北京的雾霾天 2009-02-23
  • 打赏
  • 举报
回复
ia定义如下:

interface ia
{
void somea();
}
北京的雾霾天 2009-02-23
  • 打赏
  • 举报
回复
我有如下的代码是可以获取到的:

class b : ia
{
public void somea()
{

}
public void a()
{
foreach (Type t in this.GetType().GetInterfaces())
{
Console.WriteLine(t);
}
}
}

b _b = new b();
_b.a();

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧