2011-02-09 26 views
11

我有一個自定義用戶控件DatePicker.cs。在另一段代碼中,我有一組控件,我正在檢查控件的類型並根據類型執行一些邏輯。我的問題是這樣的:如何獲得自定義用戶控件的「typeof」

typeof(DatePicker) 

演算值爲:

{Name = "DatePicker" FullName = "cusitecore.cedarsc.UserControls.DatePicker"} 

但是當我運行調試器,看看那是我的網絡上的控件的類型構成是:

{Name = "cedarsc_usercontrols_datepicker_ascx" FullName = "ASP.cedarsc_usercontrols_datepicker_ascx"} 

這兩件事情並不相同,所以正確的邏輯沒有得到評估。我試過使用Type.GetType(「ASP.cedarsc_usercontrols_datepicker_ascx」),但是這返回null。

編輯

這裏就是我想要做的事:

private readonly Dictionary<Type, ControlType?> _controlTypes = new Dictionary<Type, ControlType?> 
    { 
     {typeof(CheckBox), ControlType.CheckBox}, 
     {typeof(CheckBoxList), ControlType.CheckBoxList}, 
     {typeof(DropDownList), ControlType.DropDownList}, 
     {typeof(HiddenField), ControlType.HiddenField}, 
     {typeof(ListBox), ControlType.ListBox}, 
     {typeof(RadioButton), ControlType.RadioButton}, 
     {typeof(RadioButtonList), ControlType.RadioButtonList}, 
     {typeof(TextBox), ControlType.TextBox}, 
     {typeof(Label), ControlType.Label}, 
     {typeof(DatePicker), ControlType.DatePicker}, 
     {typeof(CustomSelect), ControlType.CustomSelect} 
    }; 

private void PopulateFields(Control control) 
{ 
    ControlType? controlType; 
    _controlTypes.TryGetValue(control.GetType(), out controlType); 

    // recurse over the children 
    if (control.Controls.Count > 0 && controlType == null) // don't want to recurse into children of controls we are reading values of 
    { 
     foreach(Control childControl in control.Controls) 
     { 
      PopulateFields(childControl); 
     } 
    } 

    if (controlType != null) 
    { 
     switch (controlType) 
     { 
      case ControlType.CheckBox: 
      case ControlType.RadioButton: 
       CheckBox checkBox = control as CheckBox; 
        if (checkBox != null) 
         _fields.AddFieldValue(checkBox.ID, checkBox.Checked ? "Checked" : "Not Checked"); 
        break; 
      case ControlType.CheckBoxList: 
      case ControlType.ListBox: 
      case ControlType.RadioButtonList: 
       ListControl listControl = control as ListControl; 
       if (listControl != null) 
        _fields.AddFieldValue(listControl.ID, String.Join(", ", listControl.Items.Cast<ListItem>().Where(item => item.Selected).Select(item => item.Value).ToArray())); 
       break; 
      case ControlType.DropDownList: 
       DropDownList dropDownList = control as DropDownList; 
       if (dropDownList != null) 
        _fields.AddFieldValue(dropDownList.ID, dropDownList.SelectedValue); 
       break; 
      case ControlType.HiddenField: 
       HiddenField hiddenField = control as HiddenField; 
       if (hiddenField != null) 
        _fields.AddFieldValue(hiddenField.ID, hiddenField.Value); 
       break; 
      case ControlType.TextBox: 
       TextBox textBox = control as TextBox; 
       if (textBox != null) 
        _fields.AddFieldValue(textBox.ID, textBox.Text); 
       break; 
      case ControlType.DatePicker: 
       DatePicker datePicker = control as DatePicker; 
       if (datePicker != null) 
        _fields.AddFieldValue(datePicker.ID, datePicker.Text); 
       break; 
      case ControlType.CustomSelect: 
       CustomSelect customSelect = control as CustomSelect; 
       if(customSelect != null) 
        _fields.AddFieldValue(customSelect.ID, customSelect.SelectedValue); 
       break; 
      case ControlType.Label: 
       Label label = control as Label; 
       if(label != null) 
        _fields.AddFieldLabel(label.AssociatedControlID, label.Text); 
       break; 
      default: 
       throw new Exception("Unhandled Control"); 
     } 
    } 
} 
+0

你能提供一些更多的上下文嗎?你的代碼是什麼樣子,試圖在兩種類型之間進行比較? – 2011-02-09 18:53:49

+4

ASP.NET創建它自己的類型從用戶控件繼承。使用`is`關鍵字進行比較,並嘗試`control.GetType()。BaseType`進行提取。 – 2011-02-09 18:57:54

+0

@Jaroslav這是答案。一巴掌在底部,所以我們可以給你信用:) – Kyle 2011-02-09 19:04:44

回答

11

ASP.NET創建它自己的類型來自用戶控件。

對於比較使用is操作。
提取用control.GetType().BaseType

1

你可以嘗試使用關鍵字is。這不是完全相同的事情,但如果你所要做的只是確定一個對象是否屬於某種類型(或者擴展/實現了一個類/接口),那麼它就應該做到這一點。

當然,根據你的代碼,這可能沒有幫助。

+0

我今天剛剛遇到了這個問題,(myControl是TypeICareAbout)工作。 「is」關鍵字檢查變量是否可以轉換爲所需類型而不會引發錯誤。請參閱:http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.80).aspx – William 2011-02-09 18:57:55

2

您的文章並沒有完全進入你打算如何使用這一點,但我從來沒有用typeof任何問題()我在事件中使用。例如,我有以下的if語句在處理懸停事件:

if (sender.GetType() == typeof(Transparent_Panel)) 

其中Transparent_Panel是一個自定義用戶控件。我從來沒有在Tansparent_Panel做過任何特別的工作。

1

ASP.NET 2.0和更高版本將UserControl編譯爲Temporary ASP.NET Files目錄隨需應變,因此您查看調試器中控件類型時檢查的類型是由ASP.NET編譯引擎。好消息是,這種從DatePicker類型繼承,所以下面的代碼應該工作測試給定UserControl實際上是否是DatePicker

typeof(DatePicker).IsAssignableFrom(userControl.GetType().BaseType) 

或者,你總是可以在創建DatePickerUserControl的一個實例運行時間,並通過檢查類型等價:

LoadControl(typeof(DatePicker)).GetType() == userControl.GetType() 

來源:Compilation and Deployment in ASP.NET 2.0