Reading Attributes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    #1

    Reading Attributes?

    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?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Reading Attributes?

    Alex,

    First, are you sure you don't want to declare your Wind_speed field as
    ByValArray? If you use LPArray, the field will be a pointer to the array,
    and the array isn't embedded in the struct.

    In order to get the attribute on the field, call the GetCustomAttrib utes
    method on the FieldInfo instance, like so:

    MarshalAsAttrib ute m = (MarshalAsAttri bute)
    pi.GetCustomAtt ributes(typeof( MarshalAsAttrib ute), true));

    // Get the size const field.
    int sizeConst = m.SizeConst;

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Alex" <atelford6@hotm ail.com> wrote in message
    news:e8yXGjuEGH A.4036@TK2MSFTN GP09.phx.gbl...[color=blue]
    >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?
    >
    >[/color]


    Comment

    Working...