473,386 Members | 1,846 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,386 software developers and data experts.

Reflection and property enumeration

balabaster
797 Expert 512MB
okay, I'm trying to reference a property within a class using a string "class.property", I've got a test class, imaginitively called "Test", hold your applause...it's got a property called value.

I've got a function called GetValue that accepts an array of objects and a string for the property reference I want to get the value of.

Now I can use reflection to create a type and use the GetProperty method to reference the "value" property:

This gets a dynamic object of type "Test":
Dim MyClass As Type = GetType(Test)
at which point, I can use MyClass.GetProperty("Value") to grab the property's value and return it...seems simple enough.

Okay, lets add some complexity. Given that I can pass an array of objects into my function, I want to be able to give a fully qualified property name to reference:

Lets say there's two classes, I want to specify which class to pass the value back from:
Dim Args(1) As Object
Args(0) = New Test1(123.45)
Args(1) = New Test2(987.65)
Dim Value = GetValue(Args, "Test1.Value")
I think I could do this readily enough if I could pass a string value into GetType (which obviously I can't)... rather than code a switch/case to pass the correct type given a string, is there an easy way to evaluate a string as the type it refers to rather than as a string?

(Apologies for not using code tags, the new code windows are a touch excessive for small code blocks)
Sep 19 '08 #1
2 1477
Plater
7,872 Expert 4TB
Wait, why can't you use:
Type.GetType(string name)
?
Granted you would need to do a .Split('.') of some sort to get your type?

Expand|Select|Wrap|Line Numbers
  1. Type tt = Type.GetType("WMI.WebDownload");
  2. object o = tt.InvokeMember("WebDownload", BindingFlags.CreateInstance, null, null, new object[] { "http://www.google.com", @"c:\ggg.html" });
  3. tt.InvokeMember("GetWebObject", BindingFlags.Default|BindingFlags.InvokeMethod, null, o, null);
  4.  
I have a namespace called "WMI" and a class called "WebDownload" in it. For my purpose I needed to create an instance of the class (The first InvokeMember() call, the object[] is the constructors arguments) and assigned it to object o. I then used the second InvokeMethod to invoke the method "GetWebObject", which has no arguments, but I told it to invoke that method on object o. Since o is of type WebDownload, it executed my code. I check with breakpoints and it really does enter into that function.

Somewhere in all of that should be an idea to help you out? You could apply the same logic as the BindingFlags.InvokeMethod call, only change the flags to be like "return property" or whatever they are
Sep 22 '08 #2
balabaster
797 Expert 512MB
hehe, I'd forgotten I'd posted this. So, here was my solution... for the moment. I'm going to design something recursive in the long run, but this was my prototype:

Expand|Select|Wrap|Line Numbers
  1. Function GetValue(ByVal MyInstance As IMyInterface, ByVal ValuePath As String) As Double
  2.  
  3.     Dim AssemblyName As String = Reflection.Assembly.GetExecutingAssembly().GetName.Name
  4.     Dim PathNodes As List(Of String) = ValuePath.Split(".").ToList
  5.  
  6.     If PathNodes(0) <> AssemblyName Then PathNodes.Insert(0, AssemblyName)
  7.  
  8.     Dim CrntInstance As Object = MyInstance
  9.  
  10.     For i As Integer = 2 To PathNodes.Count - 1
  11.  
  12.         Dim CrntType As Type = CrntInstance.GetType()
  13.         Dim CrntProperty As String = PathNodes(i)
  14.         Dim dynProperty As Reflection.PropertyInfo = CrntType.GetProperty(CrntProperty)
  15.         Dim CrntValue = dynProperty.GetValue(CrntInstance, _
  16.                                        System.Reflection.BindingFlags.GetProperty, _
  17.                                        Nothing, _
  18.                                        Nothing, _
  19.                                        Nothing)
  20.         CrntInstance = CrntValue
  21.  
  22.     Next
  23.  
  24.     Return CrntInstance
  25.  
  26. End Function
I'm not sure I like the use of Split() to do it... but I can't think of anything more appropriate.
Sep 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos...
4
by: Alicia | last post by:
Hi all, I have a problem with an Enum and Reflection. I am using an Xml and Reflection to create some controls, and to set their properties. All goes well until I encounter one property which is...
8
by: Rachel Suddeth | last post by:
I'm trying to use reflection to create an object at runtime where the type is given by a string. I thought we should be able to do that, but so far it's not working. I'm trying to work on...
3
by: Brett Kelly | last post by:
Hello all, I'm in a situation where I need to retrieve a member from the System.Data.SqlDbType enumeration knowing only the type name. At this point, I'm just trying to get reflection to...
6
by: ECathell | last post by:
I have a routine that compares 2 intances of the same object to see if there are any changes. The problem is that even though there are no changes, it tells me there are. I have even tried running a...
2
by: Lee | last post by:
Is it possible to use Reflection to determine if a class's member is an Enumerated Type? Thank you. -- Warm Regards, Lee "Upon further investigation it appears that your software is...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
1
by: Mordachai | last post by:
I'm trying to simply generate the list of enumeration values for a given enumeration class. e.g., for the following: enum struct Colors { red, green, blue }; I would like to have a...
6
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am slightly familiar with reflection but have never done the following I know how to find a class and call but I haven't done the following The Method return a List of Another Class And...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.