日期:2014-05-17  浏览次数:20810 次

100分请教牛人:如何控制类属性的顺序
比如:我class 定义的属性顺序是
  int ID
  string Name
  bty[] Image

结果在 PropertyInfo[] ps = typeof(T).GetProperties()时
发现顺序变了bty[] Image 排在int ID
前面了,我想让它保持原来的顺序
 

------解决方案--------------------
貌似他是按照字母顺序 排列的吧 呵呵
------解决方案--------------------
如果是Winform的话:
using System.Runtime.CompilerServices;

C# code

    [StructLayout(LayoutKind.Sequential)]              //<---
    class A
    {
      int ID,
      string Name,
      bty[] Image
    }

------解决方案--------------------
Net/C#交流区〓 [7729746]
C# / .Net 交流社团 聊技术,项目合作。

[7729746] 〓 .Net/C#交流区〓 绝对 VS平台 牛群....

欢迎有项目经验的朋友入群交流。。.

附:招管理员(通过 beming 考核)
Blogs http://zhoufleru.cnblogs.com
------解决方案--------------------
这么做有什么用啊?
------解决方案--------------------
我的怎么就可以呢?

C# code

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

[StructLayout(LayoutKind.Sequential)]
class Program
{
    static void Main()
    {
        PropertyInfo[] property = typeof(Program).GetProperties( 
            BindingFlags.Instance |
            BindingFlags.Public | 
            BindingFlags.NonPublic );
    }

    int ID { set { } }
    string Name { set { } }
    byte[] Image { set { } }
}

------解决方案--------------------
你打印出来看看和实体中的有什么关系,然后如果是反过来的话,你就反一下,如果是按照什么排序的就换个思路吧
给你一个思路,给属性一个标识,标识它在实体类中的顺序,然后按照顺序显示啊,
------解决方案--------------------
添加一个数组类型属性
用来排序

------解决方案--------------------
靠编译器来调整顺序是个不好的做法。你可以试试给属性加上一个自定义的attribute,这个attribute就指明属性的顺序。怎么写attribute,参见 msdn
------解决方案--------------------
探讨
靠编译器来调整顺序是个不好的做法。你可以试试给属性加上一个自定义的attribute,这个attribute就指明属性的顺序。怎么写attribute,参见 msdn

------解决方案--------------------
探讨
靠编译器来调整顺序是个不好的做法。你可以试试给属性加上一个自定义的attribute,这个attribute就指明属性的顺序。怎么写attribute,参见 msdn

------解决方案--------------------
...
------解决方案--------------------
处理bype[]类型的数据是不是System.byte[],判断值为不为空.