Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Created May 28, 2012 06:41
Show Gist options
  • Save pinzolo/2817715 to your computer and use it in GitHub Desktop.
Save pinzolo/2817715 to your computer and use it in GitHub Desktop.
特定のインターフェースを実装しているかどうかを調べる(ジェネリクス込)
// 基底クラス
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