C#
如何将⼀个类中全部字段添加到List 中(字段为⾃定义类,引⽤类
型,不是值类型)?
⾃定义了⼀个类,⾥⾯包含了很多字段(也是⾃定义的类),如何将类中的字段全部添加到list中
做这个功能主要是为了能够通过list来访问FileInfoInIni,调⽤FileInfoInIni的⽅法。
或者哪位⼤神有更好的办法能实现这个功能也⾏
如何写⼀个⽅法,⾃动将 FileInfoInIni 全部添加到 AllIniParam 中
FileInfoInIni 为⾃定义的类,我试过了反射,只能设置值,不知道怎么转换类型,然后添加进去
不能强制转换类型
FileInfoInIni的定义
博客课程⽂库·商城问答社区插件认证开源1
class SystemParamClass 2
{3
public String ModelFileSavePathName = "D:/Data/SystemParam";//保存参数的⽂件夹名称4
public String ModelSystemParamFileName = "/SystemParam.ini";5
6
7
public List <FileInfoInIni> AllIniParam = new List <FileInfoInIni>();8
9
public FileInfoInIni LeftDownCamGrapImageHeight;10
public FileInfoInIni LeftDownCamGrapImagePoseX;11
public FileInfoInIni LeftDownCamGrapImagePoseY;12
13
public FileInfoInIni RightDownCamGrapImageHeight 14
public FileInfoInIni RightDownCamGrapImagePoseX;15 public FileInfoInIni RightDownCamGrapImagePoseY;16writeline方法属于类
17 }
1
public static void AddTolist (object TempClass)2
{3
Type t = TempClass.GetType();4
FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);5
foreach (FieldInfo field in fields)6
{7
if (field.FieldType == typeof (FileInfoInIni))8
{9
Console.WriteLine("Field:" + field);10
Console.WriteLine("Field:" + field.Name );11
}12
//Console.WriteLine("Field:" + field);13
14
}15
16 }1
public class FileInfoInIni 2
{3
private string FilePathAndName;4
private string FileSection;5
private string FileAppName;6
public object Value;7
public string Key { get => FileAppName; }8
9
[DllImport("kernel32", CharSet = CharSet.Ansi, EntryPoint = "GetPrivateProfileStringA", ExactSpelling = true, SetLastError = true)]10
private static extern int GetPrivateProfileString ([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpApplicationName, [MarshalAs (UnmanagedType.VBByRefStr)] ref string lpKeyName, [11
[DllImport("kernel32", CharSet = CharSet.Ansi, EntryPoint = "WritePrivateProfileStringA", ExactSpelling = true, SetLastError = true)]12
private static extern int WritePrivateProfileString ([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpApplicationName, [MarshalAs (UnmanagedType.VBByRefStr)] ref string lpKeyName, [13
public string GetINI (string Section, string AppName, string lpDefault, string FileName)14
{15
string text = Strings.LSet("", 256);16
FileInfoInIni.GetPrivateProfileString(ref Section, ref AppName, ref lpDefault, ref text, Strings.Len(text), ref FileName);17
return Strings.Left(text, checked (Strings.InStr(text, "\0", CompareMethod.Binary) - 1));18
}19
public long WriteINI (string Section, string AppName, string lpDefault, string FileName)20
{21
return (long )FileInfoInIni.WritePrivateProfileString(ref Section, ref AppName, ref lpDefault, ref FileName);22
}23
public bool ReadValue ()24
{25 bool result;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论