Created
May 28, 2012 06:41
-
-
Save pinzolo/2817715 to your computer and use it in GitHub Desktop.
特定のインターフェースを実装しているかどうかを調べる(ジェネリクス込)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 基底クラス | |
public class Base {} | |
// サブクラス | |
public class SubClass : Base {} | |
// とあるクラス | |
public class Foo | |
{ | |
// 実体は List だけど IEnumerable として公開 | |
public IEnumerable Classes | |
{ | |
get { return new List<SubClass>(); } | |
} | |
} | |
// ここから | |
var propType = typeof(Foo).GetProperty("Classes").PropertyType; | |
// 代入可能かどうかで判別 | |
typeof(IEnumerable<Base>).IsAssignableFrom(propType) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment