声明类型上的属性无效。

8
我有以下代码,但是我得到了以下编译错误:
属性 'WebPartStorage' 在此声明类型上无效。它只在“属性、索引器”声明上有效。
并且
属性 'FriendlyName' 在此声明类型上无效。它只在“属性、索引器”声明上有效。
我已经从MSDN文章修改了我的代码:https://learn.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;
        }
    }
3个回答

13

看起来你已经将属性附加到了字段上,该属性始终依附于下一个事物(在本例中为字段)。你应该重新排序,使其依附于属性而不是字段。

顺便说一句,受保护的字段很少是一个好主意(它们应该是私有的),但特别是如果属性是公共的:这有什么意义呢?

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; }
}

1

这个信息已经告诉你了,不是吗?你试图将属性设置为字段,但只能在索引器和属性上有效。

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;
    }
}

0
希望你已经使用了 Microsoft.SharePoint.WebPartPages,对吧?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接