有关定义从 Attribute.GetCustomAttributes 返回的数组顺序的任何建议
好吧......所以标题准确地定义了我正在寻找的内容。
现在,我正在使用这个解决方案来控制订单:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class DefaultSortCriterionAttribute : Attribute
{
private int _priority = 0;
public int Priority
{
get
{
return _priority;
}
}
private string _parameterName;
public string ParameterName
{
get
{
return _parameterName;
}
}
private SortDirection _direction;
public SortDirection Direction
{
get
{
return _direction;
}
}
public DefaultSortCriterionAttribute(string parameterName, SortDirection direction)
{
_parameterName = parameterName;
_direction = direction;
}
public DefaultSortCriterionAttribute(int priority, string parameterName, SortDirection direction)
{
_priority = priority;
_parameterName = parameterName;
_direction = direction;
}
}
有什么建议如何使其更有效吗?
我的泛型类中的用法示例:
Type type = typeof(T);
if (Attribute.IsDefined(type, typeof(DefaultSortCriterionAttribute)))
{
DefaultSortCriterionAttribute[] attribs = (DefaultSortCriterionAttribute[])Attribute.GetCustomAttributes(type, typeof(DefaultSortCriterionAttribute));
List<SortCriterion> list = new List<SortCriterion>();
foreach (DefaultSortCriterionAttribute attrib in attribs)
{
SortCriterion sc = new SortCriterion(attrib.ParameterName, attrib.Direction);
list.Add(sc);
}
return this.CompareTo(other, list);
}
Ok... so the title defines exactly what I'm looking for.
Right now, I am using this solution for controlling the order:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class DefaultSortCriterionAttribute : Attribute
{
private int _priority = 0;
public int Priority
{
get
{
return _priority;
}
}
private string _parameterName;
public string ParameterName
{
get
{
return _parameterName;
}
}
private SortDirection _direction;
public SortDirection Direction
{
get
{
return _direction;
}
}
public DefaultSortCriterionAttribute(string parameterName, SortDirection direction)
{
_parameterName = parameterName;
_direction = direction;
}
public DefaultSortCriterionAttribute(int priority, string parameterName, SortDirection direction)
{
_priority = priority;
_parameterName = parameterName;
_direction = direction;
}
}
Any suggestion how to make it more efficient?
Example of the usage in my generic class:
Type type = typeof(T);
if (Attribute.IsDefined(type, typeof(DefaultSortCriterionAttribute)))
{
DefaultSortCriterionAttribute[] attribs = (DefaultSortCriterionAttribute[])Attribute.GetCustomAttributes(type, typeof(DefaultSortCriterionAttribute));
List<SortCriterion> list = new List<SortCriterion>();
foreach (DefaultSortCriterionAttribute attrib in attribs)
{
SortCriterion sc = new SortCriterion(attrib.ParameterName, attrib.Direction);
list.Add(sc);
}
return this.CompareTo(other, list);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法想象更好的方法。这很好。
I can't imagine a better approach. This is good.