您的位置:首页 > 其它

反射检查类型是否实现某个接口

2009-09-06 13:18 274 查看
public interface IBookRetailer : IDisposable
{
void Purchage();
void ApplyDiscount();
}

public interface IMusicRetailer
{
void Purchase();
}

public class MyRetailer : IMusicRetailer,IBookRetailer
{
public void Purchase() { }
public void Dispose() { }
void IBookRetailer.Purchage() { }
public void ApplyDiscount() { }
void IMusicRetailer.Purchase() { }
}

class Program
{
static void Main(string[] args)
{
Type t = typeof(MyRetailer);

Type[] interfaces = t.FindInterfaces(new TypeFilter(ConsoleApplication22.Program.TypeFilter), Assembly.GetCallingAssembly().GetName());

Console.WriteLine("MyRetailer implements the following"+"interfaces defined in this assembly");

foreach (Type i in interfaces)
{
Console.WriteLine("/nInterface:" + i);

InterfaceMapping map = t.GetInterfaceMap(i);
for (int m = 0; m < map.InterfaceMethods.Length; m++)
{
Console.WriteLine("{0} is implemented by {1}", map.InterfaceMethods[m], map.TargetMethods[m]);
}

}

Console.ReadKey();

}

static bool TypeFilter(Type t, object filtercriteria)
{
return true;
return t.Assembly.GetName().ToString() == filtercriteria.ToString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: