奇怪的属性错误
我让这个解决方案工作得很愉快,并且添加了这个属性:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
public class metadata : Attribute
{
string mid;
string mdescription;
Dictionary<string, string> mdata;
public string id
{
get
{
return mid;
}
}
public string description
{
get
{
return mdescription;
}
}
public Dictionary<string, string> data
{
get
{
return mdata;
}
}
public metadata(string thisid, string thisdescription, params KeyValuePair<string, string>[] thisdataarray)
{
mid = thisid;
mdescription = thisdescription;
mdata = new Dictionary<string, string>();
if (thisdataarray != null)
{
foreach (KeyValuePair<string, string> thisvaluepair in thisdataarray)
{
mdata.Add(thisvaluepair.Key, thisvaluepair.Value);
}
}
}
}
我添加了这个属性的一些声明,所有 [metadata(null, null)] 没有错误。不知何故,当我编译时,对于每个 [metadata(null, null)] 都会出现“错误 CS0182:属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式”,但是没有行或列或文件,仅项目。出了什么问题?谢谢。
I had this solution working happily and i added this attribute:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
public class metadata : Attribute
{
string mid;
string mdescription;
Dictionary<string, string> mdata;
public string id
{
get
{
return mid;
}
}
public string description
{
get
{
return mdescription;
}
}
public Dictionary<string, string> data
{
get
{
return mdata;
}
}
public metadata(string thisid, string thisdescription, params KeyValuePair<string, string>[] thisdataarray)
{
mid = thisid;
mdescription = thisdescription;
mdata = new Dictionary<string, string>();
if (thisdataarray != null)
{
foreach (KeyValuePair<string, string> thisvaluepair in thisdataarray)
{
mdata.Add(thisvaluepair.Key, thisvaluepair.Value);
}
}
}
}
I added a few declarations of this attribute, all [metadata(null, null)] with no errors. Somehow, when I compile, for each [metadata(null, null)] an "error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" occurs, however with no line or column or file, only the project. What went wrong? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是不支持 KeyValuePair 作为属性参数类型。
根据 C# 语言规范:
属性类的位置参数和命名参数的类型仅限于属性参数类型,它们是:
长、sbyte、短、字符串、uint、ulong、ushort。
它所嵌套的(如果有的话)也具有公共可访问性。
作为解决方法,您可以传递一个字符串数组,其中奇数成员是键,偶数成员是值。然后在属性构造函数中您可以创建字典。
The problem is that KeyValuePair is not supported as attribute parameter type.
According to the C# Language specification:
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
long, sbyte, short, string, uint, ulong, ushort.
which it is nested (if any) also have public accessibility.
As a workaround you can pass an array of strings where the odd members are keys and even members are values. Then inside the attribute constructor you can create your Dictionary.
您声明数组的方式很可疑,我认为您不需要 params 关键字。我认为您不需要 0 个或多个键值对数组,我认为您正在寻找具有 0 个或多个条目的单个数组。删除 params 关键字,并将 thisdataarray 的默认值设置为 null。
The way you declared the array is suspicious, I don't think you need the params keyword. I don't think you want 0 or more keyvaluepair arrays, I think you are looking for a single array with 0 or more entries. Remove the params keyword, and set the default value of thisdataarray to null.