【问题标题】:Attribute not valid on declaration type属性在声明类型上无效
【发布时间】:2010-12-10 02:52:06
【问题描述】:

我有以下代码,但出现以下编译错误:

属性“WebPartStorage”在此声明类型上无效。它仅对“property, indexer”声明有效。

属性“FriendlyName”在此声明类型上无效。它仅对“property, indexer”声明有效。

我修改了 MSDN 文章中的代码:https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint2003/dd584174(v=office.11)。有谁知道我做错了什么导致这个错误?

  [Category("Custom Properties")]
    [DefaultValue(RegionEnum.None)]
    [WebPartStorage(Storage.Shared)]
    [FriendlyName("Region")]
    [Description("Select a value from the dropdown list.")]
    [Browsable(true)]
    protected RegionEnum _Region;
    public RegionEnum Region
    {
        get
        {
            return _Region;
        }
        set
        {
            _Region = value;
        }
    }

【问题讨论】:

    标签: c# sharepoint sharepoint-2007 web-parts


    【解决方案1】:

    您似乎已将属性附加到该字段;属性始终遵循 next 事物(在本例中为字段)。您应该重新排序,以便它们遵守属性而不是字段。

    顺便说一句;受保护的字段很少是一个好主意(它们应该是私有的);但尤其是如果财产是公开的:这有什么意义?

    protected RegionEnum _Region;
    [Category("Custom Properties")]
    [DefaultValue(RegionEnum.None)]
    [WebPartStorage(Storage.Shared)]
    [FriendlyName("Region")]
    [Description("Select a value from the dropdown list.")]
    [Browsable(true)]
    public RegionEnum Region
    {
        get { return _Region; }
        set { _Region = value; }
    }
    

    【讨论】:

      【解决方案2】:

      消息告诉你,不是吗?您正在尝试将属性设置为字段,但它仅对索引器和属性有效。

      protected RegionEnum _Region;
      
      [Category("Custom Properties")]
      [DefaultValue(RegionEnum.None)]
      [Description("Select a value from the dropdown list.")]
      [Browsable(true)]
      [WebPartStorage(Storage.Shared)]
      [FriendlyName("Region")]
      public RegionEnum Region
      {
          get
          {
              return _Region;
          }
          set
          {
              _Region = value;
          }
      }
      

      【讨论】:

        【解决方案3】:

        希望你有using Microsoft.SharePoint.WebPartPages;,是吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-21
          • 1970-01-01
          • 2017-12-31
          相关资源
          最近更新 更多