Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Created May 19, 2012 02:23
Show Gist options
  • Save pinzolo/2728701 to your computer and use it in GitHub Desktop.
Save pinzolo/2728701 to your computer and use it in GitHub Desktop.
Typeクラス拡張
using System;
using System.Reflection;
namespace MktSys.Lib.Extensions
{
/// <summary>
/// Type クラス拡張メソッド定義クラス
/// </summary>
public static class TypeExtension
{
/// <summary>
/// 指定の型が指定のインターフェースを実装しているかどうかを判別する。
/// </summary>
/// <param name="type">型情報</param>
/// <param name="i">インターフェース型情報</param>
/// <returns>指定のインターフェースを実装しているならば true、していない場合 false</returns>
public static bool Implements(this Type type, Type i)
{
if (i == null)
{
throw new ArgumentNullException("i");
}
if (!i.IsInterface)
{
throw new ArgumentException("インターフェース型が指定されていません。", "i");
}
//var filter = new TypeFilter((t, obj) => t == (System.Type)obj);
//return type.FindInterfaces(filter, i).Length > 0;
// こっちのほうが簡単だった
return i.IsAssignableFrom(t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment