FastMember column order preservation

故事扮演 提交于 2019-12-22 18:10:00

问题


When using TypeAccessor.Create FastMember always seems to return a list of the columns in alphabetic sorted order. Is it possible to tell it to preserve the ordering of the columns in the class?

for example:

var testClass = new { B = "1", A = "2" };

will return column A then B from GetMembers, I'd like to get it to preserve the ordering of B then A if possible.


回答1:


Looks like Evk is right and it can't really be done via the standard APIs. In the interest of completeness I'll leave this as the answer.




回答2:


I created a fork of this project with column order option and added a PR to the original repository. OrdinalAttribute was added to specify columns order.

The ordinal attribute can be used as shown below:

        public class ObjectReaderWithDefinedColumnsOrderType
        {
            [Ordinal(1)]
            public byte C { get; set; }
            [Ordinal(0)]
            public int? D { get; set; }
        }
  • IDataReader object returned in ObjectReader.Create() will have columns order according to the defined attributes.
  • If the attribute is not defined in the source class then alphabetical order is used.
  • If two fields have the same Ordinal value then then alphabetical order is used.
  • If the attribute is defined only for some properties then the columns are sorted by the ordinal values and then in alphabetical order (properties with no attributes are considered as -1 ordinal).

You can use my for right away or wait until the PR is merged with the original repository.



来源:https://stackoverflow.com/questions/40628276/fastmember-column-order-preservation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!