C# 属性对声明类型无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1624533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attribute not valid on declaration type
提问by Rob
I have the following code but I am getting the following compile errors:
我有以下代码,但出现以下编译错误:
Attribute 'WebPartStorage' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.
特性“WebPartStorage”在此声明类型上无效。它仅对“属性,索引器”声明有效。
AND
和
Attribute 'FriendlyName' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.
属性“FriendlyName”在此声明类型上无效。它仅对“属性,索引器”声明有效。
I have modified my code from the MSDN article: https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint2003/dd584174(v=office.11). Does anyone have any idea what I am doing wrong that is causing this error?
我修改了 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;
}
}
采纳答案by Marc Gravell
You seem to have attached the attribute to the field; attributes always adhere to the nextthing (in this case, the field). You should re-order so that they adhere to the property instead of the field.
您似乎已将属性附加到字段;属性始终坚持下一个事物(在本例中为字段)。您应该重新排序,以便他们坚持财产而不是领域。
BTW; protected fields are rarely a good idea (they should be private); but especially if the property is public: what is the point?
顺便提一句; 受保护的字段很少是一个好主意(它们应该是私有的);但特别是如果财产是公共的:有什么意义?
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; }
}
回答by Maximilian Mayerl
The message tells you, doesn't it? You are trying to set the attribute to a field, but it is only valid on indexers and properties.
消息告诉你,不是吗?您正在尝试将属性设置为字段,但它仅对索引器和属性有效。
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;
}
}
回答by Janis Veinbergs
Hopefully you have using Microsoft.SharePoint.WebPartPages;
, have you?
希望你有using Microsoft.SharePoint.WebPartPages;
,是吗?