2013-07-23 120 views
5

有没有办法获得类实现的接口的类型。为了进一步解释这个问题,我的意思是。例如:返回类实现的接口类型C#

public interface ISomeInterface 
{ 

} 

public class SomeClass : ISomeInterface 
{ 

} 

public class SecondClass 
{ 

    ISomeInterface someclass; 

    public SecondClass() 
    { 
     someclass = new SomeClass(); 
    } 

    public Type GetInterfaceInSomeWay() 
    { 
     //some code 
    } 

} 

我需要填写注释区域。预先感谢您的帮助,社区!

+1

可能重复:http://stackoverflow.com/questions/26733/getting-all-types-that-implement-an-interface-with-c-sharp-3-0 – FSou1

+2

@ FSou1我想这个问题是问这个问题的确切的相反 –

+0

可能的重复[使用反射来查找接口实现](http://stackoverflow.com/questions/1519530/using-reflection-to-find-interfaces-实现) – nothrow

回答

8

typeof(SomeClass).GetInterfaces()

0
foreach (Type tinterface in typeof(SomeClass).GetInterfaces()) 
{ 
    Console.WriteLine(tinterface.ToString()); 
}