我需要按照在类中声明它们的顺序使用反射来获取所有属性.根据MSDN,使用时无法保证订单GetProperties()
GetProperties方法不以特定顺序返回属性,例如按字母顺序或声明顺序.
但我已经读过,通过订购属性有一个解决方法MetadataToken.所以我的问题是,这样安全吗?我似乎无法在MSDN上找到有关它的任何信息.或者有没有其他方法来解决这个问题?
我目前的实施情况如下:
var props = typeof(T)
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.OrderBy(x => x.MetadataToken);
Run Code Online (Sandbox Code Playgroud)