首页 新闻 赞助 找找看

反射,判断某个接口是否继承了某个接口,如果有父接口递归判断是否有父父接口

0
悬赏园豆:800 [已解决问题] 解决于 2008-12-19 20:10

 [Component(Company="langzhi studio", Id="fc3fce15-cc35-4b60-8d39-0765baa8d6e1", Version="1.0", Author="lexus", Description="正则抽取规则组件")]
    public interface IActivity7Result:IResultBase
    {
        //在各个成员属性上标识说明性的Attribute用于说明该属性是输入还是输出
        //做什么用,在GUI配置环境下进行显示,便于用户组装
        String RegionSource { get; set; }
        DataTable ExtractResult { get; set; }
    }

问题补充: 上面的是一个例子,实现了Component自定义属性的接口,可能继承了某个接口,也可能没有继承,想能过反射来判定这个问题, 如果继承了接口,那么父接口可能还继承了某个接口,是一个递归的过程,对反射这块不是太清楚,请给个示例, string path = @"D:\wwwroot\OICF\eHunter\RegexExtractRule\bin\Debug\Component.RegexExtractRule.dll"; Assembly o = Assembly.LoadFrom(path); Type[] oTypes = o.GetTypes(); ComponentAttribute[] attrs; foreach (Type t in oTypes) { List<Type> list=new List<Type>(); list.Add(t); list.AddRange(t.GetInterfaces()); for (int i = 0; i < list.Count; i++) { attrs = (ComponentAttribute[])list[i].GetCustomAttributes(typeof(ComponentAttribute), false); if (attrs.Length > 0) { for (int j = 0; j < attrs.Length; j++) { Console.WriteLine(attrs[i].Id); Console.WriteLine(attrs[i].Company); } PropertyInfo[] pis = list[i].GetProperties(BindingFlags.Public | BindingFlags.Instance); for(int k=0;k<pis.Length-1;k++) { Console.WriteLine(pis[k].Name); Console.WriteLine(pis[k].PropertyType.ToString()); } //判断是否还父接口,如果有,输出父接口的公有实例属性 } } }
lexus的主页 lexus | 初学一级 | 园豆:0
提问于:2008-12-19 19:31
< >
分享
最佳答案
0

三个接口依次继承

interface IA
{
}

interface IB : IA
{
}

interface IC : IB
{
}

输出函数

static void OutputInterface(Type type)
{
    Console.WriteLine(type.FullName);
    Type[] interfaces = type.GetInterfaces();
    if (interfaces != null)
    {
        foreach (Type inter in interfaces)
        {
            Console.WriteLine(inter.FullName);
        }
    }
}

调用

static void Main(string[] args)
{
    OutputInterface(typeof(IC));

    Console.Read();
}

注意

GetInterfaces方法直接把所有父接口,父父接口之类的都取到了,不用递归

Gray Zhang | 专家六级 |园豆:17610 | 2008-12-19 19:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册