ソースコード
using System; public static class Program { public interface ICharacter{} public class Pokemon : ICharacter{} public class Item{} private static void Main() { var interfaceType = typeof( ICharacter ); var pokemonType = typeof( Pokemon ); var itemType = typeof( Item ); // True Console.WriteLine( interfaceType.IsAssignableFrom( pokemonType ) ); // False Console.WriteLine( interfaceType.IsAssignableFrom( itemType ) ); } }
Type.IsAssignableFrom を使用することで
指定したクラスにインターフェイスが実装されているかどうか判定できます
クラス.IsAssignableFrom( インターフェイス )
ではなく
インターフェイス.IsAssignableFrom( クラス )
で記述する