qi_1213
2019-11-15 11:25
采纳率: 50%
浏览 878

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;
            }
        }
    
    

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

    点赞 评论
  • sinat_35053659 2019-11-15 11:36

    强制类型转换为(FileInfoInIni)field后添加到list中

    点赞 评论
  • Json-Huang 2019-11-15 12:03

    Add方法不可以添加吗?AllIniParam.Add(FileInfoInIni实例变量)

    点赞 评论
  • blownewbee 2019-11-15 17:24

    FileInfoInIni在哪里定义的,字符串不能转换为FileInfoInIni
    看看它有没有构造函数或者字段赋值可以接受字符串的。

    点赞 评论

相关推荐 更多相似问题