Skip to main content
 首页 » 编程设计

c#之使用某种类型的变量进行转换

2024年02月23日16shihaiming

我想做的是将我的控件添加到 Ext.Net.Panel 中。

这是我的代码示例:

   Ext.Net.Panel panel = new Ext.Net.Panel(); 
   Control control = (Control) null;//here can be any Ext.Net type as well as NumeticField for example Ext.Net.TextArea and so on 
  switch (infoAttribute.Type) 
                                { 
                                        case PropertyTypeAttrib.Text: 
                                            control = (Control)new Ext.Net.TextField(); 
                                            ((TextField)control).FieldLabel = infoAttribute.HeadLine; 
                                            control.ID = propertyHelper.Name; 
                                            //panel.Items.Add((TextField)control); 
                                        typeToCast = typeof (TextField); 
                                        break; 
                                    case PropertyTypeAttrib.FileUrl: 
                                        control = (Control) new Ext.Net.HyperLink(); 
                                        control.ID = propertyHelper.Name; 
                                        ((Ext.Net.Label) control).FieldLabel = infoAttribute.HeadLine; 
 
                                        //panel.Items.Add((Ext.Net.HyperLink) control); 
                                        typeToCast = typeof (Ext.Net.HyperLink); 
                                        break; 
                                    case PropertyTypeAttrib.Enum: 
                                        control = (Control) new MMPControls.Web.ComboBoxEnumExt(); 
                                        ((MMPControls.Web.ComboBoxEnumExt) control).EnumerationTypeName = 
                                            propertyHelper.PropertyType.Name; 
                                        control.ID = propertyHelper.Name; 
                                        ((MMPControls.Web.ComboBoxEnumExt) control).FieldLabel = infoAttribute.HeadLine; 
                                        //panel.Items.Add((MMPControls.Web.ComboBoxEnumExt) control); 
                                        typeToCast = typeof (MMPControls.Web.ComboBoxEnumExt); 
                                        break; 
                                    case PropertyTypeAttrib.Date: 
                                        control = new MMPControls.Web.DateSelect(); 
                                        control.ID = propertyHelper.Name; 
                                        //panel.Items.Add( 
                                            //(MMPControls.Web.DateSelect) control); 
                                        //panel.Items.Add((MMPControls.Web.DateSelect)control); 
                                        typeToCast = typeof (MMPControls.Web.DateSelect); 
                                        break; 
                                    case PropertyTypeAttrib.DateTime: 
                                        control = new MMPControls.Web.DateSelect(); 
                                        control.ID = propertyHelper.Name; 
                                        //panel.Items.Add( 
                                            //(MMPControls.Web.DateSelect)control); 
                                        typeToCast = typeof (MMPControls.Web.DateSelect); 
                                        break; 
                                    case PropertyTypeAttrib.TextInteger: 
                                        control = (Control)new Ext.Net.NumberField(); 
                                        ((NumberField)control).AllowDecimals = false; 
                                        ((NumberField)control).MinValue = 0; 
                                        ((Ext.Net.NumberField)control).FieldLabel = infoAttribute.HeadLine; 
                                        control.ID = propertyHelper.Name; 
                                        //panel.Items.Add( 
                                            //(Ext.Net.NumberField) control); 
                                        typeToCast = typeof (Ext.Net.NumberField); 
                                        break; 
                                    case PropertyTypeAttrib.IList: 
                                        //TODO:  
                                        break; 
                                    case PropertyTypeAttrib.ImageUrl: 
                                        control = (Control)new Ext.Net.Image(); 
                                        control.ID = propertyHelper.Name; 
                                        ((Ext.Net.Image)control).FieldLabel = infoAttribute.HeadLine; 
                                        //panel.Items.Add((Ext.Net.Image) control); 
                                        typeToCast = typeof (Ext.Net.Image); 
                                        break; 
                                    case PropertyTypeAttrib.TextFractional: 
                                        control = (Control)new Ext.Net.NumberField(); 
                                        ((NumberField)control).AllowDecimals = true; 
                                        ((NumberField)control).DecimalPrecision = infoAttribute.Fractional; 
                                        ((NumberField)control).MinValue = 0; 
                                        ((Ext.Net.NumberField)control).FieldLabel = infoAttribute.HeadLine; 
                                        control.ID = propertyHelper.Name; 
                                       //panel.Items.Add( 
                                            //(Ext.Net.NumberField) control); 
                                        typeToCast = typeof (Ext.Net.NumberField); 
                                        break; 
                                    case PropertyTypeAttrib.TextLarge: 
                                        control = (Control)new Ext.Net.TextArea(); 
                                        ((TextArea)control).FieldLabel = infoAttribute.HeadLine; 
                                        control.ID = propertyHelper.Name; 
                                        //panel.Items.Add((TextArea)control); 
                                        typeToCast = typeof (TextArea); 
                                        break; 
   } 
   panel.Items.Add((typeToCast)control);//that's what i need to do. 

panel.Items..... 行中出现错误无法解析此符号 typeToCast

以前有人做过类似的事情吗?

感谢提前:)

已解决:

我所做的是为 Component 类型转换我准备好的控件。 panel.Items.Add((Component)control);

请您参考如下方法:

这个错误是因为转换表达式的类型部分必须是类型的名称(或类型参数)——而不是表达式的值。

为什么你觉得你需要转换?为什么不只是:

panel.Items.Add(new Ext.Net.NumericField()); 

为什么要强制转换为 Control? NumericField 不是已经派生自 Control 了吗?为什么要使用 typeof?基本上,我看不到您提供的代码中需要任何强制转换。如果您认为他们有充分的理由,请为问题添加更多上下文。