检查对象是否继承了类型

时间:2016-06-14 01:32:50

标签: c#

有人可以请一些代码帮助检查一个对象是否继承了特定的类型。

这是我的代码:

public class Class1
{

}

public class Class2 : Class1
{

}

private void TestType()
{
    var collection = new List<Class1>();
    collection.Add(new Class1());
    collection.Add(new Class2());

    var results = new List<Class1>();

    foreach (var item in collection)
    {
        if (item.GetType().IsAssignableFrom(typeof(Class1)))
        {
            results.Add(item);
        }
    }
}

在上面的代码中,结果集只有Class1个对象,而不是两个对象。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

vowel = "aeiou"
vcount = 0
index = 0

sen = input("Enter sentence (If you enter nothing the program will terminate)\n")
senLen = len(sen)

while senLen > 0:
    sen = sen.lower
    while index < senLen:
        if "test"[index] in vowel:
            vcount = vcount + 1
            index = index + 1
    print("You latest sentance was", sen, "\nIt was", senLen, "Characters long\n", "And had", vcount, "Vowels")
    input("Enter sentence (If you enter nothing the program will terminate)\n")