簡體   English   中英

使用分配給某種類型的變量進行轉換

[英]Cast with variable assigned of some type

我正在嘗試做的是將我的控件添加到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 基本上,我看不到您提供的代碼中需要任何強制轉換。 如果您認為他們有充分的理由,請為問題添加更多上下文。

通過一些重構,您應該能夠在不使用太多 Casting 的情況下構建相同的結果。 以下示例演示了一種非常簡化的方法。

例子

<%@ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM