Reflection On Structure Differs From Class?

Jan 15, 2009

Code snippet:

Dim target As Object
' target gets properly set to something of the desired type
Dim field As FieldInfo = target.GetType.GetField("fieldName", _
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic)
field.SetValue(target,newValue)

This snippet works perfectly IF target is set to an instance of a CLASS.However, if target is set to an instance of a STRUCTURE, the code does not actually change the value of the field. No error, but the value remains unchanged.And, oddly, if I'm stepping through code, watch the SetValue fail to do anything, and immediately go to the Immediate window and type exactly the same SetValue operation, that works.

Edit:

Per request from Jon Skeet, actual code:
Private Shared Function XmlDeserializeObject(ByVal objectType As Type, _
ByVal deserializedID As String) As Object[code].....

View 2 Replies


ADVERTISEMENT

Class Structure - Large Structure That Has Lots Of Properties ?

Jan 5, 2010

I'm new to VB 2008 after having spent a long time with VB6, so I apologize if this is a stupid question. But I'd really like to have this straightened out.

Let's say I have a pretty large structure that has lots of properties.

Code:

Now say that I want an internal database with about 10 instances of this structure total, describing, say, 10 different products that a store sells. When these values are loaded from a database, they remain totally static. (However, they can be different each time a program loads)

Now say that I have a class. Each instance of this class is a type of that BaseProduct structure. Meaning, each instance of the class pertains to one of the 10 types of products that the store sells. However, this class has additional properties that pertain specifically to each instance, which are not static.

Code:

Now, the problem here is... If I have 200 different transactions, each one contains an instance of BaseProduct. BaseProduct is HUGE, and is largely redundant (only 10 types possible), so I think it's a little silly to include a whole copy of it with EVERY transaction. However, the Transaction class really needs information regarding the base product it pertains to. Is there a way to, instead of declaring a New BaseProduct in the Transaction class, to simply make one of the properties of the Transaction class a pointer to a BaseProduct variable?

In VB6, I would accomplish this by making a BaseProduct(10) array, and then giving each Transaction an ID number referring to an entry in that array. But in VB 2008, using class structure, this is impossible. I can't define the BaseProduct(10) array outside of a class in a namespace, and if I define it in the actual application's form, then the class loses modularity since it relies on the application that's using it.

View 11 Replies

VS 2008 Convert The XML Structure Into A Class Structure?

Apr 25, 2010

I'm having a problem that's driving me crazy; I can't understand how to convert the XML structure into a class structure (that I want to use to hydrate a XML document).

The XML document looks like this:

xml
<?xml version="1.0" encoding="utf-8"?>
<artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1">

[code]....

View 2 Replies

Reflection - Getting A Reference To A Class By Its Name

Sep 6, 2010

I'm trying to use reflection to get the instance of a class in vb.net. I have a class 'A' in my web project and to test it, i create a new aspx page and try to write the following:

Dim t as Type = Type.GetType("A")

This returns "Nothing". But if i do this:

Dim inst as A = new A()
Dim t as Type = inst.GetType()
t's type is "A"

So how come i can't get the type with GetType even if the name is exactly the same? It does works for things like System.Math though

View 1 Replies

Reflection - Set Value Of Array Within Class?

Feb 19, 2009

I'm trying to use reflection to populate the properties of an array of a child property... not sure that's clear, so it's probably best explained in code:

Parent Class:
Public Class parent
Private _child As childObject()
Public Property child As childObject()
Get
Return _child
[Code] .....

The issue is now trying to get that array assigned to the parent object (using reflection). It shouldn't be difficult, but I think the problem comes because I don't necessarily know the parent/child types. I'm using reflection to determine which parent/child is being passed in. The parent always has only one property, which is an array of the child object. When I try assigning the child array to the parent object, I get a invalid cast exception saying it can't convert Object[] to.

Basically, what I have now is:
Dim PropChildInfo As PropertyInfo() = ResponseObject.GetType().GetProperties()
For Each PropItem As PropertyInfo In PropChildInfo
PropItem.SetValue(ResponseObject, ResponseChildren, Nothing)
Next
ResponseObject is an instance of the parent Class, and ResponseChildren is an array of the childObject Class.

This fails with:
Object of type 'System.Object[]' cannot be converted to type 'childObject[]'.

View 2 Replies

Asp.net - .Net Reflection To Get Description Of Class / Property?

Apr 14, 2010

I know its unlikely but I was wondering if there is any way to get the comments (i.e. the bits after the ''') of a class or property..? I have managed to get a list of properties of a class using the PropertyInfo class but I cant find a way to get the comments / description.. I need it for a guide I am writing for the administrators of my site - it would be great if it could automatically update if new properties are added, so there is no need to worry about updating it in the future too much.

View 5 Replies

C# - .NET Reflection Create Class Properties

Apr 28, 2009

how to create an instance of a class then add properties to the class, set those properties, then read them later. I don't have any code as i don't even know how to start going about this. C# or VB is fine.

My system has a dynamic form creator. one of my associates requires that the form data be accessible via web service. My idea was to create a class (based on the dynamic form) add properties to the class (based on the forms fields) set those properties (based on the values input for those fields) then return the class in the web service.

additionally, the web service will be able to set the properties in the class and eventually commit those changes to the db.

View 4 Replies

C# - Usage Of FieldInfo Class In .NET REFLECTION?

Mar 11, 2011

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As
System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(_Class))

[code]....

How can I reach that sqlparameter collection via fieldinfo or other related classes in >NET Reflection?

View 1 Replies

Reflection , Need To Get A Type From A Class Name String?

Dec 30, 2011

What I wanted to do was, given the table name (as string), use reflection to instantiate the get method for specific fields (defined as properties with Get and Set methods.

Public Property VC60() As String
Get
Return _VC60

[code].....

View 2 Replies

Passing A Structure From One Class To Another Class In A Applications (VS2008)

Apr 7, 2010

I'm sure there are several ways of achieving my goal but I am after opinions on what you think is the best option. I'm writing a vb.net application which (amongst other thinks) interfaces with media players in the home. Because there are different types of media playersI'm trying to structure it so each type of media player has it's own class which interfaces to a management class which sits on top of all the different devices types.

The device classes are responsible for discovering the phsyical devices and returning that information up to the management class. A device class may be responsible for monitoring 1 or more physical devices so..

[Code]...

View 1 Replies

.net - Reflection Retrieve Private Methods Of A Class?

Sep 13, 2010

I want to retrieve private (implementation and other) methods of a class which implements an interface and also is derived from (inherits) a base class.How can I achieve this using reflection?This is wat m tryin to do. I need to view these private methods and their contents, I don't want to invoke them.

Dim assembly As System.Reflection.Assembly
Dim assemblyName As String assemblyName = System.IO.Path.GetFullPath("xyz.dll")
assembly = System.Reflection.Assembly.LoadFile(assemblyName)
assembly.GetType("myClass").Getmethods(Bindings.NonPublic)
assembly.GetType("myClass").GetMethods(BindingFlags.NonPublic) isn't working

View 2 Replies

Invoking A Method From A Class Dynamically With Reflection?

Oct 15, 2010

I'm making a little program in where i'm trying to invoke a method from a class dynamically with so called Reflection.The class I'm trying to call is called ContactList and i try to invoke the method in this class called count. The assembly itself is called Contact.ExeNow I have the following code:

Public Function InvokeContacts() As Integer
Dim ContactsAssembly As Assembly = Assembly.LoadFrom("Contacts.exe")
Dim Mycontactlist As Type = ContactsAssembly.GetType("ContactList")

[code]....

View 10 Replies

Using Reflection To Find A Properties Definition Class?

May 18, 2008

I am currently using reflection to retrieve all properties of a class. I return a collection of PropertyInfo objects and all is working fine.

My problem is, I want to check if a property has been defined in a base class implementation and is therefore inherited in the current class.

View 2 Replies

C# - Retrieve Internal Member Of Nested Class By Reflection

Jun 1, 2012

I know that the MenuItem class contains an internal class named MenuItemData, which contains itself an internal member named onDrawItem. Given a MenuItem, I want to retrieve the object corresponding to the member onDrawItem. But all I manage to do is to get the FieldInfo, not the object itself.

Here is my code:
Dim obj As Object
Dim fi As FieldInfo
Dim item as System.Windows.Forms.MenuItem
Dim mType As System.Type
[Code] .....

When reaching the last line, I get an error saying something like that (it's traduced):
The field 'onDrawItem' défined in type 'System.Windows.Forms.MenuItem+MenuItemData' is not a field of the target object of type 'System.Windows.Forms.MenuItem. I don't know what object to pass to the GetValue function on the last line. My goal is to remove the base eventHandler of the menuItem, named DrawItem. See this post and the function RemoveClickEventin the accepted answer for a better understanding.

View 2 Replies

Reflection - How To Get Variable Names Types / Values In Current Class

Feb 1, 2011

I am working on a project where my class has to execute VB code provided by the user, to make it simple I am trying to recreate my own eval function, I am using the following code I found on the web to do this task.

Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
[Code] .....

The problem with code is that it can't access any variables or there values, so I have decided to get the variable names, there values types and there types dynamically and recreate them in the class that is being created dynamically. Any way to get the variable names there types and values in the current class or method, so that I can recreate them, and execute the user passed code, the user knows what variables are in the current class or method and there datatypes but he don't know there values as they may have changed, so he can't initialize them. Is there a way to do this, this code will be called in an asp.net page on page_load event, the code passed by the user is stored in the variable vbCode that is passed as a parameter.

View 2 Replies

VS 2010 Reflection, Create A Class With Dynamically Named Properties?

May 11, 2010

I'm trying to make a very simple audio tag editor. The idea is that the user can select as many audio files as he likes from a ListView, and a PropertyGrid will then display their tags + their values.When multiple files are selected, I want the property grid to display the values of the tags that all files have in common, and display empty values for tags that the files do not have in common.As an example, look at the properties list in your Visual Studio IDE when you select multiple controls (of the same type for this matter). Select a few Button controls, for example, and notice that the properties whose values are the same (often things like Font, ForeColor, etc) are displayed. Also notice that the values for properties that are not the same (Text, Name, etc) are empty.

View 17 Replies

Reflection - Function Witch Return Type Is The Class Itself In Inherited Classes?

Dec 10, 2011

I have a parent class that is also a factory. For example:

Public Class Factory
Public Function clone() as Factory
' Some logic here

[code].....

View 2 Replies

Use Reflection To Unit-test An Internal Class Within An Assembly When The InternalsVisibleToAttribute Is Not An Option?

Aug 26, 2010

I have a solution with two projects within:

Company.Project.vbproj
Company.Project.Tests.vbproj

Within the Company.Project.vbproj assembly, I have a class FriendClass.vb which scope is Friend (internal in C#).Now I wish to test this FriendClass.vb from within the Company.Project.Tests.vbproj assembly. I know about the InternalsVisibleToAttribute, but that is not an option in Visual Basic .NET 2.0, as it is only available with C#, in .NET 2.0 (see here).I would like to create myself a proxy class using this internal FriendClass from within my testing assembly, so that I could instantiate it and do the testings accordingly.

View 1 Replies

Using Reflection To Attach / Detach Handler Of Base Event In Derived Class

Dec 13, 2009

I am just curious, if there is some way to attach handler to my derived classes base event, if I know EventInfo and I have Delegate to handler function. I have tried MyEvent.AddHandler(CType(Me, BaseClassType), MyDelegate) already, but no positive result at all. Consider being in need of attaching handler to custom event at runtime, while derived class possibly shadows this event.

View 2 Replies

Asp.net - Structure A Class Inside A Class?

Jun 3, 2011

Currently, if I have different classes containing functions etc, I would structure them all in the 1 VB file titled whatever the main Class is. For example:

[Code]...

However, the problem with this is the file can become 1000s of lines long making hard to find portions of code. Is there a way I can store SubClass in a file such as MainClass.SubClass.vb so that its easier to locate classes? Or is there a better, more standard, way of doing this?

View 3 Replies

C# - Use A Structure Rather Than A Class?

Apr 10, 2009

In a recent project I was working I created a structure in my class to solve a problem I was having, as a colleague was looking over my shoulder he looked derisively at the structure and said "move it into a class". I didn't have any argument for not moving it into a class other than I only need it in this class but this kind of falls down because couldn't I make it a nested class?

View 5 Replies

.net - Where To Keep A Structure: Class, Module Or None

Apr 18, 2011

This is purely a coding practice question concerning VB.NET structures. Where do you keep one?As an example, I've created a structure for the simple purpose of keeping information organized, I'd like this structure to be stand-alone, not belonging to any class so I wouldn't have to access it through SomeClass.MyStructure but rather simply MyStructure. So, do I keep it in a separate class and import it into my namespace? Do I keep it in a module (Static class, essentially)? Do I just make a separate VB.NET code file and place is there (I would like it to live in a separate file). Just to clarify, I need this structure to be accesses by the whole application, not just a single class.

View 1 Replies

Class Structure For Three Outputs?

Feb 20, 2012

Could someone write me a Class structure for passing the data array below.

Function function1(Byval input1 as Double, Byval input2 as Double, Byval input3 as Double)
'code'
data.data1=value1

[code].....

View 4 Replies

Cannot Get Values From A Serializable Class And Structure

Feb 17, 2010

I have a serializable class and structure I created for an ArrayList to be access within a Web Application. The problem I am having is the count comes back as zero after calling New. I debugged the code and the Arrylist is being populated

View 3 Replies

Class/structure To Serialize JSON?

Mar 9, 2011

I am trying to figure out a class/struture to handle the following JSON format:

{
"ReturnData": [
{
"id": "msg2DoesNotExistName",

[Code]......

I may or may not have Data for ReturnData and SetValue (one or both at a minimum). I am trying to let the serializer handle most of the formatting without having to check for empty sections and single-item arrays.

View 1 Replies

Possible To Show Structure Of A Class In A Treeview?

May 31, 2009

is it possible to show the structure of a class in a treeview? if yes how

View 3 Replies

[2008] Size Structure In A Class?

Mar 6, 2009

I am making a class that has a property that I want to be of type System.Drawing.Size. I don't seem to be able to reference system.drawing from within my class, and therefor can't set my property's data type to Size. Anyone know how I might do this?

View 2 Replies

Schema Returned By New Query Differs From Base

Mar 15, 2012

When Creating a query using the sear Criteria Builder. I keep getting the error:
"The schema returned by the new query differs from the base query".
I know this means that the error is due to the query attempting to return too little columns. I have 3 rows in a table but I want to display one. I am currently using a list box what alternative is there.

View 1 Replies

Build A Class Structure To Support Two Threads?

Nov 8, 2009

I have this application that users can make exams that contain exercises of different ... whatever. Anyway when student takes the exam he chooses it from a list and then all exercises are loaded from database. I wanted to make loading process faster by loading only first exercise data and putting others in background thread.

Actually in other parts of this application something like that is done but not by me. Basicaly an api class is built around BackgroudWorker and i can use this class to put certain functions of objects to be executed in background. The idea sounds great but in real life there are some problems with it. For example for historical reasons this application uses global functions and reader and uses global connection to Access database which means if in background something is being fectched from data base and you try to fetch from database in main thread then a „reader is already open" error occurs. To avoid this error if im doing something in main thread and want to fetch some data i have to move my function from main thread into background worker function list. And basically this in my opinion stinks cuz you end up with many delegātes, invoked checking and thre result... I was just thinking that there maybe is another way to allow (maybe with events) not put everything in background in case something is happening in bakcground but rather allow bgworker to finsih current thing, then do my thing and then resume bgWorker and not create havoc in code.

Basically i have to classes - exam and exercise that each implement many interfaces (like IExercise, IExerciseResult, IExerciseYesNo, IExerciseMultipleChoise etc.)And then i have DataBase classes like ExerciseDB, ExerciseResult etc. That have functions that preparē the sql params and call sqlHelper functions.And for example when i load up exam i go throug all exercises and call LoadData function that in turn calls ExerciseDB function selectData and then i put the returned data in Exercise object. And this type of thing of course would producē error if i at the same time would try to register the answer in database for another exercise. Ofcourse i could always check and put this in background... but as i said i'd rather want to work with database from both threads.

View 13 Replies

Class - Internationalization In .Net : Choosing The Right Structure Type?

Jan 22, 2010

I'm about to start translating my vb.net application, and I don't want to use the default methods provided by Visual Studio to do so. I need my application to be very light, and it nearly doubles it size to use the resources option.Therefore, I'm planning to use some thing like a class, of which I would have one instance per language. Since I don't want to distribute language files as separate files (I'd rather have them hard-coded), I would like to find an easy way to check if every field of the class is initialized. I was thinking of something like an Interface, where I would do something like this:

Public Interface Language
Dim HelloMsg As String
Dim GoodbyeMsg As String
End Interface

[code]....

It's obviously not the right way to do it (although I could use properties instead of vars), but I was wondering whether the was a way to have the compiler check that everything is translated and warn about it if not.

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved