自定义了一个类,里面包含了很多字段(也是自定义的类),如何将类中的字段全部添加到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;
}
}