保证列表中属性的顺序与它们在代码文件中出现的顺序相匹配
我有一个接口,它定义了返回 IList
的方法:
public interface IWriteable
{
IList<PropertyInfo> WriteableProperties();
}
.
.
它通过以下方式在各种(不同的)类中实现:
public abstract class Foo
{
private IList<PropertyInfo> _props;
protected Foo()
{
this._props = new List<PropertyInfo>();
foreach (PropertyInfo p in this.GetType().GetProperties())
{
if (Attribute.IsDefined(p, typeof(WriteableAttribute)))
this._props.Add(p);
}
}
#region IWriteable Members
public IList<PropertyInfo> WriteableProperties()
{
return this._props;
}
#endregion
}
public class Bar : Foo
{
public string A
{
get { return "A"; }
}
[Writeable()]
public string B
{
get { return "B"; }
}
[Writeable()]
public string C
{
get { return "C"; }
}
// Snip
}
请注意标记几个属性的属性,因为这些是将添加到列表中的属性。然后,在某些文件写入操作期间,此IList
将在其他地方使用。
对我来说很重要的是,它们在列表中的顺序按照它们在代码文件中出现的顺序排列。
但是,MSDN 指出:
GetProperties 方法不返回特定属性 顺序,例如字母顺序或声明顺序。你的代码一定不能 取决于返回属性的顺序,因为 顺序各不相同。
那么,确保每个 PropertyInfo 按照我想要的顺序添加的最佳方法是什么?
(我也在使用.NET2.0,所以我不能使用任何 Linq 的优点,如果有任何可以帮助的东西,尽管它会很有趣。)
I have an interface that defines a method for returning an IList<PropertyInfo>
:
public interface IWriteable
{
IList<PropertyInfo> WriteableProperties();
}
.
.
It is implemented in various (dissimilar) classes in the following manner:
public abstract class Foo
{
private IList<PropertyInfo> _props;
protected Foo()
{
this._props = new List<PropertyInfo>();
foreach (PropertyInfo p in this.GetType().GetProperties())
{
if (Attribute.IsDefined(p, typeof(WriteableAttribute)))
this._props.Add(p);
}
}
#region IWriteable Members
public IList<PropertyInfo> WriteableProperties()
{
return this._props;
}
#endregion
}
public class Bar : Foo
{
public string A
{
get { return "A"; }
}
[Writeable()]
public string B
{
get { return "B"; }
}
[Writeable()]
public string C
{
get { return "C"; }
}
// Snip
}
Please note the attributes marking a couple of the properties, as these are the properties that will get added to the list. This IList
will then be used elsewhere during some file write operations.
It is important to me that they are ordered in the list in the order they appear in the code file.
However, MSDN states:
The GetProperties method does not return properties in a particular
order, such as alphabetical or declaration order. Your code must not
depend on the order in which properties are returned, because that
order varies.
So, what is the best way to ensure each PropertyInfo gets added in the order I would like to to be?
(I am also using .NET2.0, so I can't use any Linq goodness, should there be any that would help, although it would be interesting to see.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将有关排序的信息添加到属性中,然后您可以使用它来确保排序,例如:
对于以下属性:
您可以获得属性的有序选择,如下所示:
NB 对于生产代码我建议缓存属性信息(例如每种类型),因为如果对每个实例都执行此操作,速度会相对较慢。
更新 - 缓存
一些属性查找和排序的缓存示例:
更新 - 无 Linq
您可以使用以下代码替换 Linq 部分,以对属性进行排序并将它们添加到缓存:
更新 - 完整 Linq
并使用完整 Linq 解决方案:
Add information to the attribute about the ordering, you can then use this to ensure the ordering, e.g.:
So for the following attribute:
You can get an ordered selection of the properties as follows:
NB For production code I would recommend caching the property information (per type for example) as this will be relatively slow if carried out for each instance.
Update - Caching
With some example caching of property lookup and ordering:
Update - No Linq
You can replace the Linq section with the following code to order the properties and add them to the cache:
Update - Full Linq
And using a fully Linq solution:
不久前,当我遇到同样的问题时,我编写了一个辅助类来根据属性的
Order
属性对属性进行排序。我使用了内置的DisplayAttribute
,但您只需将Order
属性添加到您编写的任何属性即可。这将为您提供一个
SortedList
,其中键是属性,值是 PropertyInfo。这是因为我仍然需要访问该属性的其他属性。用法示例:
输出:
A while ago when I had the same problem I wrote a helper class to sort the properties based on the
Order
property of the attribute. I used the built-inDisplayAttribute
but you can just add anOrder
property to any attribute you write.This gives you a
SortedList
where the key is the attribute and the value is the PropertyInfo. This was because I still needed to access other properties of the attribute.Example usage:
Output: