【问题标题】:"Cannot reference a type through an expression" when using an enum value使用枚举值时“无法通过表达式引用类型”
【发布时间】:2015-08-18 16:19:59
【问题描述】:

我在尝试通过我的 html 标记设置我的用户控件初始化时收到错误 cannot reference a type through an expression。我可能正在寻找一些我什至无法在这里实现的东西,但我能找到的唯一帮助是与视频游戏和 MVC 相关的,而这两者都不是。

基本上,我想通过 html 中的标记本身来初始化枚举类型的用户控件值。因此,当您声明用户控件时,它会设置所有需要的值。似乎只是抛出了那个错误,有什么见解吗?

标记

<uc:ucStateDropDownList id="UserControls_ucStateDropDownList" runat="server" StatesDisplayed="All"/>

背后的代码

public partial class UserControls_ucStateDropDownList : System.Web.UI.UserControl
{
    private StatesDisplayedInControl m_statesToDisplay;
    /// <summary>
    /// Existing options are as follows:
    ///     - UnitedStates (US states only)
    ///     - Canada (CDN states only)
    ///     - All (All states available)
    /// Default setting is 'All'.   
    /// </summary>
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public StatesDisplayedInControl StatesDisplayed 
    {
        get { return m_statesToDisplay; }
        set { m_statesToDisplay = value; }
    }
    public enum StatesDisplayedInControl : int
    {
        UnitedStates = 0,
        Canada = 1,
        All = 2
    }
}

【问题讨论】:

  • 看起来正确,即兴发挥。您的枚举是在课堂内还是课堂外声明?这是我从发现的类似搜索结果中看到的唯一区别:forums.codeguru.com/…
  • @MikeGuthrie 在类里面,我更新了代码来展示。
  • @MikeGuthrie Post 作为答案,我会接受!该死的天才我的朋友!我现在可以走了,就是这样。
  • 我犹豫是否要发布答案,因为这似乎很容易解决。然而,这是一个很容易遇到的问题,而且消息很混乱。真的很惊讶我在这里或所有谷歌都找不到任何类似的问题。然而,现在这个问题确实出现了,并且为遇到相同问题的其他人提供了答案。

标签: c# asp.net enums webforms


【解决方案1】:

当定义为类的一部分时,ASP.NET 似乎无法识别枚举。只需在自己的文件中定义它,或重新排列如下,即可使其正常工作:

public partial class UserControls_ucStateDropDownList : System.Web.UI.UserControl
{
    private StatesDisplayedInControl m_statesToDisplay;
    /// <summary>
    /// Existing options are as follows:
    ///     - UnitedStates (US states only)
    ///     - Canada (CDN states only)
    ///     - All (All states available)
    /// Default setting is 'All'.   
    /// </summary>
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public StatesDisplayedInControl StatesDisplayed 
    {
        get { return m_statesToDisplay; }
        set { m_statesToDisplay = value; }
    }
}

public enum StatesDisplayedInControl : int
{
    UnitedStates = 0,
    Canada = 1,
    All = 2
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多