2009-07-23 84 views
2

试图在.Net 3.0中使用泛型设置扩展方法,并且收到错误消息,上面的详细信息该行:无法找到名称空间名称'Control'的类型(是否缺少使用指令或程序集引用?)

foreach(Control childControl in parent.Controls) 

我是否缺少使用指令或程序集引用?

感谢

我所试图做的是设置此(下同)作为扩展功能:

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 

namespace System.Runtime.CompilerServices 
{ 
    public static class ControlHelper 
    { 
    public static T FindControl<T>(this Control parent, string controlName) where T : Control 
    { 
     T found = parent.FindControl(controlName) as T; 
     if (found != null) 
      return found; 
     foreach (Control childControl in parent.Controls) 
     { 
      found = childControl.FindControl(controlName) as T; 
      if (found != null) 
       break; 
     } 
     return found; 
    } 
} 
} 

我缺少一个参考system.core.dll ...它使我抓狂!

回答

9

选择一个适合你正在使用的技术:

Windows窗体:

它位于System.Windows.Forms.dllSystem.Windows.Forms命名空间。

WPF:(可能不是这一个,因为没有相关的Controls属性在WPF类)

它位于PresentationFramework.dllSystem.Windows.Controls命名空间。

Web控件:

它位于System.Web.dllSystem.Web.UI命名空间。

+1

是的,我的意思是确保你有DLL的引用,你有使用指令或通过它的完全合格的名称访问它。 – 2009-07-23 00:50:42

0
  1. 添加System.web.dll以在右侧的解决方案资源管理器中引用。
  2. 添加命名空间 - System.Web.UI.WebControls;在您网页的顶部。
  3. 然后你可以通过编码本身来创建网页控件。
相关问题