2013-07-23 109 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()); 
}