繁体   English   中英

为什么会出现这个错误? “找不到类型或名称空间名称'c'(您是否缺少using指令或程序集引用?)”

[英]Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)”

找不到类型或名称空间名称“ c”(您是否缺少using指令或程序集引用?)

我确实将此名称空间添加为“使用System.Web.UI.WebControls;”。 为什么这个错误?

 protected void DoSomething(Control control)(
    {

        foreach (Control c in control.Controls)
        { 
            if(typeof(c).Equals(Telerik.Web.UI.RadEditor))
            {
               Telerik.Web.UI.RadEditor rad = c as Telerik.Web.UI.RadEditor;
               rad.CssClass = "MyStyle";
                  label1.Visible = true; label1.Text = "dhchk";
               // control.CssFiles.Add("~/styles/myStyle.css"); 
            }
            else
            {
                  DoSomething(c);
            }

        }

    }

您正在使用c是如果它是类型,而不是变量。

更改此:

if (typeof(c).Equals(Telerik.Web.UI.RadEditor))

变成:

if (c.GetType().Equals(typeof(Telerik.Web.UI.RadEditor)))

或者简单地:

if (c is Telerik.Web.UI.RadEditor)

typeof(x)期望x是Type,而不是Object。

改用这个

if(c is Telerik.Web.UI.RadEditor)

正确使用typeof是

if (c.GetType().Equals(typeof(Telerik.Web.UI.RadEditor))

需要给typeof(...)运算符一个类型名称,该名称在编译时就已知。 我想你实际上意味着:

if (c.GetType().Equals(typeof(Telerik.Web.UI.RadEditor)))

但是,最好写:

RadEditor editor = c as Telerik.Web.UI.RadEditor;
if (editor != null)
{
    rad.CssClass = "MyStyle";
    label1.Visible = true;
    label1.Text = "dhchk";
}

然后,这也可以应对cRadEditor子类的实例的RadEditor ,我认为应该沿着相同的路径。

通过使用as曾经的替代is那么as或转换,你只需要做的动态类型检查一次-这通常是整洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM