473,399 Members | 3,656 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,399 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 1295
>- 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.