473,706 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jan 6 '06 #1
1 2216
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]
- mv*@spam.guard. caspershouse.co m

"Alex" <at*******@hotm ail.com> wrote in message
news:e8******** ******@TK2MSFTN GP09.phx.gbl...
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?

Jan 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9383
by: Carl Lindmark | last post by:
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and outs with datasets and datatables (and navigating through the filled datatable)... Just when I thought I had gotten the hang of it, another problem arose: I can't seem to access the "xsi:type" attribute. That is, the XML file looks
6
10214
by: Hans Kamp | last post by:
My program fails reading XML attributes. A fragment of the code is: private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn) { int i; for (i = 0; i < xnl.Count; i++) { XmlNode xn = xnl; XmlNodeType nodeType = xn.NodeType;
9
1990
by: Xarky | last post by:
Hi, I am writing an XML file in the following way. Now I need to read again that file to retrieve data such as Name and Age. Can someone help me out. Thanks in Advance /*********************************/ static void writeXML() {
1
1299
by: Maziar Aflatoun | last post by:
Hi everyone, I like to use the dataset object for reading the following XML. <?xml version="1.0" encoding="utf-8"?> <ORDERS> <ORDER> <GENERALCONTACT> <FirstName>FirstName1</FirstName> <LastName>LastName1</LastName>
1
2089
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two inconsistencies that Xerces is able to handle that XmlSerializer seems not to be able to deal with. 1) Some of the boolean values that I'm dealing with are "True" and "False" rather than "true" and "false". I'm not quite sure whether "True" and...
4
1972
by: sherifffruitfly | last post by:
Hi, I have an xml file with structured like this: <?xml version="1.0" encoding="UTF-8"?> <Soldiers> <Soldier name="Billy Smith" rank="Private" serial="34" /> (a bunch more soldiers)
1
3672
by: tdpriya1984 | last post by:
Hi, Is it possible to fetch the Elements and Attributes in XML by reading the XSD?? I want to develop a application whose Elements and Attributes changes when i change my Schema. The Elements and Attributes will be displayed in the Form to fetch the user input and the new node will be appended to the existing XML. Any help regarding this would be of great help. Thanks in Advance.
1
2293
by: nitusa | last post by:
Hi All, First time poster, and newbie C# programmer so be patient with my ignorance. :) For my current project I need to store some information (install dir., file names, passwords, ect.) and then have my program access those values. I choose to store the information in an XML file based on a recommendation from a peer. I am also new to XML but my current file setup looks like this: <?xml version="1.0" encoding="utf-8" ?>...
3
1572
by: vb2008 | last post by:
So I am trying to figure out a new part now but I am getting stuck once again and yes I have been reading up on things and I think I know what my problem is I am just not sure how to fix it. Below is the XML I am trying to read. I need to be able to have the user type in a user they are looking for which is assigned to the Role_Search_Value_1.Text box. Then when it finds that user, I want to capture the permissions for that user. So far I have...
1
2756
by: Demon4231 | last post by:
I have a XML file that stores information about an application I am trying to read and it looks like this: <Data> <Login UserName="Username" Password="Password"/> <Application Name="AppName1" Key="Key1" Owner="Owner1"/> <Application Name="AppName2" Key="Key2" Owner="Owner2"/> <Application Name="AppName4" Key="Key3" Owner="Owner3"/> <Owner Name="Owner1"/> <Owner Name="Owner2"/> <Owner Name="Owner3"/>
0
8783
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7912
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2096
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.