y.x1949 2019-11-15 11:25 采纳率: 0%
浏览 1218
已采纳

C# 如何将一个类中全部字段添加到List中(字段为自定义类,引用类型,不是值类型)?

自定义了一个类,里面包含了很多字段(也是自定义的类),如何将类中的字段全部添加到list中
做这个功能主要是为了能够通过list来访问FileInfoInIni,调用FileInfoInIni的方法。
或者哪位大神有更好的办法能实现这个功能也行

class SystemParamClass
    {
         public String ModelFileSavePathName = "D:/Data/SystemParam";//保存参数的文件夹名称
        public String ModelSystemParamFileName = "/SystemParam.ini";


        public List<FileInfoInIni> AllIniParam = new List<FileInfoInIni>();

        public FileInfoInIni LeftDownCamGrapImageHeight;
        public FileInfoInIni LeftDownCamGrapImagePoseX;
        public FileInfoInIni LeftDownCamGrapImagePoseY;

        public FileInfoInIni RightDownCamGrapImageHeight
        public FileInfoInIni RightDownCamGrapImagePoseX;
        public FileInfoInIni RightDownCamGrapImagePoseY;

                }

如何写一个方法,自动将 FileInfoInIni 全部添加到 AllIniParam 中
FileInfoInIni  为自定义的类,我试过了反射,只能设置值,不知道怎么转换类型,然后添加进去

 public static void AddTolist(object TempClass)
        {
            Type t = TempClass.GetType();
            FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType == typeof(FileInfoInIni))
                {
                    Console.WriteLine("Field:" + field);
                    Console.WriteLine("Field:" + field.Name );
                }
                //Console.WriteLine("Field:" + field);

            }

        }

不能强制转换类型
图片说明

FileInfoInIni的定义

public class FileInfoInIni
    {
        private string FilePathAndName;
        private string FileSection;
        private string FileAppName;
        public object Value;
        public string Key { get => FileAppName; }

        [DllImport("kernel32", CharSet = CharSet.Ansi, EntryPoint = "GetPrivateProfileStringA", ExactSpelling = true, SetLastError = true)]
        private static extern int GetPrivateProfileString([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpApplicationName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpKeyName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpDefault, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpReturnedString, int nSize, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpFileName);
        [DllImport("kernel32", CharSet = CharSet.Ansi, EntryPoint = "WritePrivateProfileStringA", ExactSpelling = true, SetLastError = true)]
        private static extern int WritePrivateProfileString([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpApplicationName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpKeyName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpString, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpFileName);
        public string GetINI(string Section, string AppName, string lpDefault, string FileName)
        {
            string text = Strings.LSet("", 256);
            FileInfoInIni.GetPrivateProfileString(ref Section, ref AppName, ref lpDefault, ref text, Strings.Len(text), ref FileName);
            return Strings.Left(text, checked(Strings.InStr(text, "\0", CompareMethod.Binary) - 1));
        }
        public long WriteINI(string Section, string AppName, string lpDefault, string FileName)
        {
            return (long)FileInfoInIni.WritePrivateProfileString(ref Section, ref AppName, ref lpDefault, ref FileName);
        }
        public bool ReadValue()
        {
            bool result;
            try
            {
                this.Value = this.GetINI(this.FileSection, this.FileAppName, "", this.FilePathAndName);
                bool flag = Operators.ConditionalCompareObjectEqual(this.Value, "", false);
                if (flag)
                {
                    result = false;
                    return result;
                }
            }
            catch (Exception expr_41)
            {
                ProjectData.SetProjectError(expr_41);
                result = false;
                ProjectData.ClearProjectError();
                return result;
            }
            result = true;
            return result;
        }
        public bool SaveValue()
        {
            bool result;
            try
            {
                string left = this.Value.ToString();
                this.WriteINI(this.FileSection, this.FileAppName, Conversions.ToString(this.Value), this.FilePathAndName);
                string iNI = this.GetINI(this.FileSection, this.FileAppName, "", this.FilePathAndName);
                bool flag = Operators.CompareString(left, iNI, false) != 0;
                if (flag)
                {
                    result = false;
                    return result;
                }
            }
            catch (Exception expr_71)
            {
                ProjectData.SetProjectError(expr_71);
                result = false;
                ProjectData.ClearProjectError();
                return result;
            }
            result = true;
            return result;
        }
        public FileInfoInIni(string TempFilePathAndName, string TempFileSection, string TempFileAppName)
        {
            this.Value = RuntimeHelpers.GetObjectValue(new object());
            this.FilePathAndName = TempFilePathAndName;
            this.FileSection = TempFileSection;
            this.FileAppName = TempFileAppName;
            this.Value = null;
        }
        public void dispose()
        {
            this.FilePathAndName = "";
            this.FileSection = "";
            this.FileAppName = "";
            this.Value = null;
        }
    }
  • 写回答

4条回答 默认 最新

  • 夫复何求o 2019-11-18 10:56
    关注

    试了下,运行效果如图
    图片说明

    整个代码文件:

        class SystemParamClass
        {
            public String ModelFileSavePathName = "D:/Data/SystemParam";//保存参数的文件夹名称
            public String ModelSystemParamFileName = "/SystemParam.ini";
    
    
            public List<FileInfoInIni> AllIniParam = new List<FileInfoInIni>();
    
            public FileInfoInIni LeftDownCamGrapImageHeight;
            public FileInfoInIni LeftDownCamGrapImagePoseX;
            public FileInfoInIni LeftDownCamGrapImagePoseY;
    
            public FileInfoInIni RightDownCamGrapImageHeight;
            public FileInfoInIni RightDownCamGrapImagePoseX;
            public FileInfoInIni RightDownCamGrapImagePoseY;
        }
        public class FileInfoInIni
        {
            private string FilePathAndName;
            private string FileSection;
            private string FileAppName;
            public object Value;
            public string Key { get => FileAppName; }
    
            [DllImport("kernel32", CharSet = CharSet.Ansi, EntryPoint = "GetPrivateProfileStringA", ExactSpelling = true, SetLastError = true)]
            private static extern int GetPrivateProfileString([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpApplicationName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpKeyName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpDefault, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpReturnedString, int nSize, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpFileName);
            [DllImport("kernel32", CharSet = CharSet.Ansi, EntryPoint = "WritePrivateProfileStringA", ExactSpelling = true, SetLastError = true)]
            private static extern int WritePrivateProfileString([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpApplicationName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpKeyName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpString, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpFileName);
            public string GetINI(string Section, string AppName, string lpDefault, string FileName)
            {
                string text = Strings.LSet("", 256);
                FileInfoInIni.GetPrivateProfileString(ref Section, ref AppName, ref lpDefault, ref text, Strings.Len(text), ref FileName);
                return Strings.Left(text, checked(Strings.InStr(text, "\0", CompareMethod.Binary) - 1));
            }
            public long WriteINI(string Section, string AppName, string lpDefault, string FileName)
            {
                return (long)FileInfoInIni.WritePrivateProfileString(ref Section, ref AppName, ref lpDefault, ref FileName);
            }
            public bool ReadValue()
            {
                bool result;
                try
                {
                    this.Value = this.GetINI(this.FileSection, this.FileAppName, "", this.FilePathAndName);
                    bool flag = Operators.ConditionalCompareObjectEqual(this.Value, "", false);
                    if (flag)
                    {
                        result = false;
                        return result;
                    }
                }
                catch (Exception expr_41)
                {
                    ProjectData.SetProjectError(expr_41);
                    result = false;
                    ProjectData.ClearProjectError();
                    return result;
                }
                result = true;
                return result;
            }
            public bool SaveValue()
            {
                bool result;
                try
                {
                    string left = this.Value.ToString();
                    this.WriteINI(this.FileSection, this.FileAppName, Conversions.ToString(this.Value), this.FilePathAndName);
                    string iNI = this.GetINI(this.FileSection, this.FileAppName, "", this.FilePathAndName);
                    bool flag = Operators.CompareString(left, iNI, false) != 0;
                    if (flag)
                    {
                        result = false;
                        return result;
                    }
                }
                catch (Exception expr_71)
                {
                    ProjectData.SetProjectError(expr_71);
                    result = false;
                    ProjectData.ClearProjectError();
                    return result;
                }
                result = true;
                return result;
            }
            public FileInfoInIni(string TempFilePathAndName, string TempFileSection, string TempFileAppName)
            {
                this.Value = RuntimeHelpers.GetObjectValue(new object());
                this.FilePathAndName = TempFilePathAndName;
                this.FileSection = TempFileSection;
                this.FileAppName = TempFileAppName;
                this.Value = null;
            }
            public void dispose()
            {
                this.FilePathAndName = "";
                this.FileSection = "";
                this.FileAppName = "";
                this.Value = null;
            }
        }
        public class MyTest
        {
            static void Main(string[] args)
            {
                //实例化
                SystemParamClass param = new SystemParamClass
                {
                    LeftDownCamGrapImageHeight = new FileInfoInIni("template1.bmp", "E:/abc", "VisionPro.exe"),
                    LeftDownCamGrapImagePoseX = new FileInfoInIni("template2.bmp", "E:/abc", "VisionPro.exe"),
                    LeftDownCamGrapImagePoseY = new FileInfoInIni("template3.bmp", "E:/abc", "VisionPro.exe"),
    
                    RightDownCamGrapImageHeight = new FileInfoInIni("template4.bmp", "E:/abc", "VisionPro.exe"),
                    RightDownCamGrapImagePoseX = new FileInfoInIni("template5.bmp", "E:/abc", "VisionPro.exe"),
                    RightDownCamGrapImagePoseY = new FileInfoInIni("template6.bmp", "E:/abc", "VisionPro.exe")
                };
    
                var list = new List<FileInfoInIni>();
                //反射加载
                FieldInfo[] fields = param.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance 
                    | BindingFlags.Static | BindingFlags.Public);
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType == typeof(FileInfoInIni))
                    {
                        object value = field.GetValue(param);
                        list.Add(value as FileInfoInIni);
                    }
                }
                param.AllIniParam = list;
            }
        }
    
    

    我也是做视觉集成模块的。。。。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!