インターフェイスから派生したいくつかのクラスがあり、コードをチェックインして、渡されたオブジェクトがそのインターフェイスから派生したものかどうかを確認できるようにしたいのですが、メソッド呼び出しが正確にわからない...
interface IFile
{
}
class CreateFile : IFile
{
string filename;
}
class DeleteFile : IFile
{
string filename;
}
// Input here can be a string or a file
void OperateOnFileString( object obj )
{
Type theType = obj.GetType();
// Trying to avoid this ...
// if(theType is CreateFile || theType is DeleteFile)
// I dont know exactly what to check for here
if( theType is IFile ) // its not, its 'CreateFile', or 'DeleteFile'
print("Its an IFile interface");
else
print("Error: Its NOT a IFile interface");
}
実際には、そのインターフェイスから何百もの派生クラスがあり、各タイプをチェックする必要がないようにし、そのタイプから別のクラスを作成するときにチェックを追加する必要がないようにしています。