473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to set values on attributes in a structure with reflection

Hi,

I've struggled with this problem for a while now without finding a
solution.
The scenario is as follows:

- I have a flat text file exported from a main frame.
- I use a structure to map data to the data in the file:
Public Structure PartRecord
<VBFixedString(8)> Public PartID As String
<VBFixedString(10)> Public Part As String
End Structure

- Now I want to use Reflection to bind data from the
text file to the structure. Vbfixedstring attribute are used to
determine the record structure in the text file:

Dim obj As PartRecord
dim recordType As Type
obj = Activator.CreateInstance(recordType)

recordType.InvokeMember("PartID", BindingFlags.SetField, Nothing, obj,
New Object() {"12345678"})

- The problem is that no value is set.
What am I doing wrong?
Can't members of a Structure be invoked?

Appreciate all help that can lead me to a solution of this.
Best regards

Gazza
Nov 21 '05 #1
2 1294
>- The problem is that no value is set.
What am I doing wrong?


You're modifying a boxed copy of the structure. Try it like this

Dim tmp As ValueType = obj
recordType.InvokeMember(..., tmp, ...)
obj = DirectCast(tmp, PartRecord)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
>- The problem is that no value is set.
What am I doing wrong?


You're modifying a boxed copy of the structure. Try it like this

Dim tmp As ValueType = obj
recordType.InvokeMember(..., tmp, ...)
obj = DirectCast(tmp, PartRecord)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #3

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

Similar topics

4
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
8
by: critter | last post by:
I am trying to get a value of a property of a class, but not an instance of the class. Using Reflection, I can find the class in my assembly, and I can find the property of my class, however, I...
0
by: Gunnar | last post by:
Hi, I've struggled with this problem for a while now without finding a solution. The scenario is as follows: - I have a flat text file exported from a main frame. - I use a structure to map...
2
by: Prabhudhas Peter | last post by:
Hello, Need an easy way to Display the property values of an object (dynamically) ie I need to show the values of an Object in a rich text box. Background : We have a form, with a rich...
2
by: jim_adams | last post by:
For a nested structure such as: Dim userVariable as one structure one dim a as string dim b() as two end structure structure two
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
16
by: John Salerno | last post by:
Let's say I'm making a game and I have this base class: class Character(object): def __init__(self, name, stats): self.name = name self.strength = stats self.dexterity = stats...
5
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.