[英]Cast with variable assigned of some type
What i'm trying to do is do cast needed to add my controls to Ext.Net.Panel
.我正在尝试做的是将我的控件添加到Ext.Net.Panel
所需的转换。
This is my code example:这是我的代码示例:
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.
got error in line panel.Items.....
cannot resolve this symbol typeToCast行panel.Items.....
无法解析此符号 typeToCast
Anyone did something similar before?以前有人做过类似的事情吗?
Thanks for advance:)感谢您的提前:)
Resolved:解决:
What I did was Cast my ready control for Component
type.我所做的是为Component
类型投射我准备好的控件。 panel.Items.Add((Component)control);
Well the error is because the type part of a casting expression has to be the name of a type (or type parameter) - not the value of an expression.嗯,这个错误是因为转换表达式的类型部分必须是类型的名称(或类型参数)——而不是表达式的值。
Why do you feel you need to cast at all?为什么你觉得你需要施法? Why not just:为什么不只是:
panel.Items.Add(new Ext.Net.NumericField());
Why do you have the cast to Control
at all?为什么你有演员来Control
? Does NumericField
not already derive from Control
? NumericField
是否已经从Control
派生? Why are you using typeof
?你为什么使用typeof
? Basically I can't see any need for any casts in the code you've presented.基本上,我看不到您提供的代码中需要任何强制转换。 If you think there's a good reason for them, please add more context to the question.如果您认为他们有充分的理由,请为问题添加更多上下文。
With a little refactoring, you should be able to build the same result without use of much Casting.通过一些重构,您应该能够在不使用太多 Casting 的情况下构建相同的结果。 The following sample demonstrates a very simplified approach.以下示例演示了一种非常简化的方法。
Example例子
<%@ Page Language="C#" %>
<%@ Import Namespace="Panel=Ext.Net.Panel" %>
<%@ Import Namespace="Button=Ext.Net.Button" %>
<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
this.Form.Controls.Add(this.BuildForm());
}
}
private Component BuildForm()
{
var panel = new FormPanel {
Title = "Example",
Width = 350,
Height = 215,
Padding = 5,
DefaultAnchor = "100%",
Buttons = { new Button { Text = "Submit" }}
};
panel.Items.Add(this.BuildWidget(new Widget {
Name = "text",
ID = "TextField1",
Label = "My TextField"
}));
panel.Items.Add(this.BuildWidget(new Widget {
Name = "date",
ID = "DateField1",
Label = "My DateField"
}));
return panel;
}
private Field BuildWidget(Widget widget)
{
Field field = null;
switch(widget.Name)
{
case "text":
field = new TextField();
break;
case "date":
field = new DateField();
break;
}
field.ID = widget.ID;
field.FieldLabel = widget.Label;
return field;
}
public class Widget
{
public string Name { get; set; }
public string ID { get; set; }
public string Label { get; set; }
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.