飞翔的河南人1949 2019-11-15 11:25 采纳率: 0%
浏览 1189
已采纳

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条)

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题