Hello everyone,
I hope you are doing well ?
I would like to share you the following question:
Let's say we start from the following structure used to persist settings already implemented in a previous 32bits application.
public struct struct32bits
{
[MarshalAs(UnmanagedType.I4)]
public int lVersionNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string szValue;
[MarshalAs(UnmanagedType.R4)]
public float fValue;
}
The objective is to be able to still load the file previously generated based on that format but afterwards convert that structure to a 64bits version.
public struct struct64bits
{
public Int64 lVersionNumber;
public string szValue;
public float fValue;
}
Due to alignment issue, I found a way to loop ove the fields of the structure but when I wanted to assign the value into the new structure, I always got convertion exception.
public static bool Migrate32BitsStructTo64BitsStruct(ref struct32bits st32Bits, ref struct64bits st64Bits)
{
System.Reflection.FieldInfo[] OldFields = st32Bits.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
System.Reflection.FieldInfo[] NewFields = st64Bits.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
System.Reflection.FieldInfo OldField = null;
System.Reflection.FieldInfo NewField = null;
for (int i = 0; i < OldFields.Count(); i++)
{
OldField = OldFields[i];
NewField = NewFields[i];
if (NewField != null && OldField != null)
{
object objValue = OldField.GetValue(st32Bits).ToString();
try
{
object objNewValue = Convert.ChangeType(objValue, NewField.FieldType);
NewField.SetValue(st64Bits, objNewValue);
}
catch (Exception ex)
{
}
}
}
return true;
}
I even try with a convertion based on the new type but the assignement exception was still occured.
How would it be possible to apply convertion from 32 to 64 bits structure?
I even tried with the following modification to the structure but this was not solving the issue
public struct struct64bits
{
public Int64 lVersionNumber {get; set;}
public string szValue {get; set;}
public float fValue {get; set;}
}
Does somebody have already tried something like that ?
What implies the 64bits structure manipulation to solve that convertion issue ?
Thank you very much in advance.
Best regards.
MiQi