【问题标题】:How to return an interface type without mentioning the derived class name?如何在不提及派生类名称的情况下返回接口类型?
【发布时间】:2010-10-28 02:51:59
【问题描述】:

我正在寻找一种不引用类名的方法,但仍能达到预期的效果。 我不能做 ITypedList.GetType() 因为它是一个接口。

public class DerivedList : ITypedList  
...  
public PropertyDescriptorCollection GetItemProperties()  
{  
    return TypeDescriptor.GetProperties(typeof(DerivedList));  
}  
...  

我想知道这是否可能。

干杯,
编辑:试图用没有提到它自己的类名的东西来替换它

【问题讨论】:

  • 我觉得这个问题有点不清楚,但可能只是我。您是否尝试替换此行'return TypeDescriptor.GetProperties(typeof(DerivedList));'没有提到它自己的类名的东西?
  • 是的。抱歉,如果不清楚,我是这里的新手。

标签: c# inheritance interface types


【解决方案1】:

绝对...

    public PropertyDescriptorCollection GetItemProperties()
    {
        return TypeDescriptor.GetProperties(this);  
    }

【讨论】:

    【解决方案2】:

    只需在当前实例上调用GetType:

    return TypeDescriptor.GetProperties(this.GetType());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      • 2014-08-29
      • 2011-07-06
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      相关资源
      最近更新 更多