There is something wrong in my DefaultValue() function. It ALWAYS returns false
, representing that the the structure is NOT the default value.
Why would this not work?
[StructLayout(LayoutKind.Sequential)]
private struct ArrayItem
{
public long SrcSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)]
public string SrcFile;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)]
public string DestFile;
}
[StructLayout(LayoutKind.Sequential)]
private struct MyInfo
{
public int Count;
public int AppOne;
public int AppTwo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100, ArraySubType = UnmanagedType.Struct)]
public ArrayItem[] Files;
}
private bool DefaultValue<T>(T structure)
{
if (EqualityComparer<T>.Default.Equals(structure, default(T)))
return true;
else
return false;
}
//Success returns 'Value Changed' as expected
MyInfo fileInfoOne = new MyInfo();
fileInfoOne.Count = 3;
fileInfoOne.Files = new ArrayItem[100];
fileInfoOne.Files[0].SrcSize = 100;
Debug.Write("fileInfoOne: ");
if (DefaultValue(fileInfoOne.Files[0])) Debug.WriteLine("Default Value."); else Debug.WriteLine("Value Changed.");
//Fails but has all the default settings, should return 'Default Value'
MyInfo fileInfoTwo = new MyInfo();
fileInfoTwo.Files = new ArrayItem[100];
fileInfoTwo.Files[0].SrcSize = 0;
fileInfoTwo.Files[0].SrcFile = "";
fileInfoTwo.Files[0].DestFile = "";
Debug.Write("fileInfoTwo: ");
if (DefaultValue(fileInfoTwo.Files[0])) Debug.WriteLine("Default Value."); else Debug.WriteLine("Value Changed.");