I have a simple enough struct..
[StructLayout(La youtKind.Sequen tial)]
public struct InstallationGen eral_Lock
{
public int Case_name;
public int Run_year;
public int Inst_name;
public int Inst_loc;
public int State;
public int Weather_filenam e;
public int Ground_water_te mp;
[MarshalAs(Unman agedType.LPArra y, SizeConst=FEDSH elper.NUM_MONTH S)]
public int[] Wind_speed;
public int latitude;
[MarshalAs(Unman agedType.LPArra y, SizeConst=FEDSH elper.NUM_BEG_E ND_POINTS)]
public FEDSDate_Lock[] Heat_season;
public int Apart_min_temp;
public int Common_space_te mp;
public int Elevation;
}
Note that I have used the MarshalAsAttrib ute for a couple of fields. This is
because I eventually will be using this to pass to come unmanaged code. That
part I have gotten a good handle on and I have some proof of concept
working. I am writing some general purpose routines to do some various
things, like initialize my struct's of which there are many. I am using
reflection to do this and it's looking very promising. The Wind_Speed field
is an array as you will see and its size is determined by a constant
FEDSHelper.NUM_ MONTHS. In my routine to initialize I want to set the value
to an array of size FEDSHelper.NUM_ MONTHS. But I want to figure out the size
by reading the MarshalAsAttrib ute.SizeConst for the field. For some reason I
cannot figure out how to do it. There is a million example for custom
attributes using GetCustomAttrib utes but this is an intrinsic attribute.
Here is a sample of my code..
/// <summary>
/// Inits the lock struct. Each Int32 field is set to LockState.Never Visited
/// </summary>
/// <param name="structLoc k">The struct lock.</param>
private void InitLockStruct( object structLock)
{
foreach(System. Reflection.Fiel dInfo pi in structLock.GetT ype().GetFields ())
{
if(pi.FieldType .IsArray == false)
{
pi.SetValue(str uctLock,
Convert.ToInt32 (Enum.Format(ty peof(LockStatus ), LockStatus.Neve rVisitd,
"d")));
}
}
}
This works great but how do I init my array?
[StructLayout(La youtKind.Sequen tial)]
public struct InstallationGen eral_Lock
{
public int Case_name;
public int Run_year;
public int Inst_name;
public int Inst_loc;
public int State;
public int Weather_filenam e;
public int Ground_water_te mp;
[MarshalAs(Unman agedType.LPArra y, SizeConst=FEDSH elper.NUM_MONTH S)]
public int[] Wind_speed;
public int latitude;
[MarshalAs(Unman agedType.LPArra y, SizeConst=FEDSH elper.NUM_BEG_E ND_POINTS)]
public FEDSDate_Lock[] Heat_season;
public int Apart_min_temp;
public int Common_space_te mp;
public int Elevation;
}
Note that I have used the MarshalAsAttrib ute for a couple of fields. This is
because I eventually will be using this to pass to come unmanaged code. That
part I have gotten a good handle on and I have some proof of concept
working. I am writing some general purpose routines to do some various
things, like initialize my struct's of which there are many. I am using
reflection to do this and it's looking very promising. The Wind_Speed field
is an array as you will see and its size is determined by a constant
FEDSHelper.NUM_ MONTHS. In my routine to initialize I want to set the value
to an array of size FEDSHelper.NUM_ MONTHS. But I want to figure out the size
by reading the MarshalAsAttrib ute.SizeConst for the field. For some reason I
cannot figure out how to do it. There is a million example for custom
attributes using GetCustomAttrib utes but this is an intrinsic attribute.
Here is a sample of my code..
/// <summary>
/// Inits the lock struct. Each Int32 field is set to LockState.Never Visited
/// </summary>
/// <param name="structLoc k">The struct lock.</param>
private void InitLockStruct( object structLock)
{
foreach(System. Reflection.Fiel dInfo pi in structLock.GetT ype().GetFields ())
{
if(pi.FieldType .IsArray == false)
{
pi.SetValue(str uctLock,
Convert.ToInt32 (Enum.Format(ty peof(LockStatus ), LockStatus.Neve rVisitd,
"d")));
}
}
}
This works great but how do I init my array?
Comment