당신이 자신을 키워드 "는"사용할 수없는 이유가 있습니까?
foreach(object obj in myObjects)
{
if (obj is type)
{
return obj;
}
}
편집 - 내가 누락 된 부분을 봅니다. Isak의 제안은 올바른 것입니다. 나는 그것을 시험하고 확인했다.
class Level1
{
}
class Level2A : Level1
{
}
class Level2B : Level1
{
}
class Level3A2A : Level2A
{
}
class Program
{
static void Main(string[] args)
{
object[] objects = new object[] {"testing", new Level1(), new Level2A(), new Level2B(), new Level3A2A(), new object() };
ReturnMatch(typeof(Level1), objects);
Console.ReadLine();
}
static void ReturnMatch(Type arbitraryType, object[] objects)
{
foreach (object obj in objects)
{
Type objType = obj.GetType();
Console.Write(arbitraryType.ToString() + " is ");
if (!arbitraryType.IsAssignableFrom(objType))
Console.Write("not ");
Console.WriteLine("assignable from " + objType.ToString());
}
}
}
을하면 그럴 수 없어 use operator 'is'이것은 최고의 솔루션이 될 것입니다 ... – Jonas