[英]c# Error 1 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
[英]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";
}
然后,這也可以應對c
是RadEditor
子類的實例的RadEditor
,我認為應該沿着相同的路徑。
通過使用as
曾經的替代is
那么as
或轉換,你只需要做的動態類型檢查一次-這通常是整潔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.