C# 将类型与实例进行比较

C# 将类型与实例进行比较,c#,C#,如何将类型与实例进行比较 这是我尝试过的,但它无法编译 错误如下: 错误242找不到类型或命名空间名称“type”(是 是否缺少using指令或程序集引用?) 替换 while (p != null && !(p is type)) 与 is运算符只能与类型名称一起使用,而不能与类型的变量一起使用。您需要调用变量以获取其类型,并将其与类型变量进行比较。我必须使用模板 PreBooking pb = GetParent<PreBooking>.Get(this) a

如何将类型与实例进行比较

这是我尝试过的,但它无法编译

错误如下:

错误242找不到类型或命名空间名称“type”(是 是否缺少using指令或程序集引用?)

替换

while (p != null && !(p is type))


is
运算符只能与类型名称一起使用,而不能与
类型的变量一起使用。您需要调用变量以获取其类型,并将其与类型变量进行比较。

我必须使用模板

 PreBooking pb = GetParent<PreBooking>.Get(this) as PreBooking; 



  public class GetParent<T>
    {
        public static object Get(Control c)
        {
            Control p = c.Parent;
            while (p != null && !(p is T))
            {
                p = p.Parent;
            }
            return p;
        }
    }
PreBooking pb=GetParent.Get(this)作为PreBooking;
公共类GetParent
{
公共静态对象Get(控件c)
{
对照组p=c.父母;
而(p!=null&!(p是T))
{
p=p.父母;
}
返回p;
}
}
type==p.GetType()
试着比较这样的类型,看看是否有效“它不编译”,注意编译器通常会告诉您代码的错误。
while (p != null && !(p.GetType() == type))
 PreBooking pb = GetParent<PreBooking>.Get(this) as PreBooking; 



  public class GetParent<T>
    {
        public static object Get(Control c)
        {
            Control p = c.Parent;
            while (p != null && !(p is T))
            {
                p = p.Parent;
            }
            return p;
        }
    }