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

Set object properties with reflections

I want set different properties of any object at runtime.
With the Function below it works well, but not perfect
Is there a way to set properties of any type with its value

Principle "one-line-code":
Dim propertyValue As Object = myPropertyType.Parse(GetType(myPropertyName),
myPropertyValue)
obj.GetType().GetProperty(myPropertyName).SetValue (obj, propertyValue,
Nothing)

Hardcode sample:
Dim propertyValue As Object = [Enum].Parse(GetType(FlatStyle),
propertyValue)
obj.GetType().GetProperty(propertyName).SetValue(o bj, propertyValue,
Nothing)

I'm looking for a way to replace the Objecttypes "[Enum]" and "FlatStyle" by
an "generalobject"

How is this possible

Best regards Horst

'-----------------------------------------------------
Public Shared Sub SetPropertyValue(ByVal obj As Object, ByVal propertyName
As String, ByVal propertyValue As String)
Dim info As PropertyInfo = obj.GetType().GetProperty(propertyName,
BindingFlags.Instance Or BindingFlags.Public)
If (info Is Nothing) Then
LogFacade.LogWarning("Es gibt kein Property mit Namen " &
propertyName & " für die Klasse " & obj.GetType().Name & ".")
Else
Dim t As Type = info.PropertyType()
Dim o As Object
If (t.Equals(GetType(Boolean))) Then
o = Boolean.Parse(propertyValue.ToString())
ElseIf (t.Equals(GetType(Integer))) Then
o = Integer.Parse(propertyValue.ToString())
Else
o = propertyValue.ToString()
End If
info.SetValue(obj, o, Nothing)
End If
End Sub
'-----------------------------------------------------
Jul 21 '05 #1
1 1135
Found a solution:

Dim info As PropertyInfo = obj.GetType().GetProperty(propertyName,
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.IgnoreCase)
If (info Is Nothing) Then
MessageBox.Show("Es gibt kein Property mit Namen " & propertyName & "
für die Klasse " & obj.GetType().Name & ".")
else
If info.CanRead And info.CanWrite Then
Dim newPropertyValue As Object
Dim t As Type = info.PropertyType()
Dim tc As TypeConverter = TypeDescriptor.GetConverter(t)
If(Not tc Is Nothing AndAlso tc.CanConvertFrom(GetType(String)))
Then
newPropertyValue = tc.ConvertFromInvariantString(propertyValue)
End If
info.SetValue(obj, newPropertyValue, Nothing)
End If
End If
Jul 21 '05 #2

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

Similar topics

1
by: Madhusudhanan Chandrasekaran | last post by:
Hi, Is it possible for me to instantiate an object of the type which is passed in as cmd line argument. I know this can be done in Java/Python having reflections. Is there any c++ way of doing...
9
by: kj | last post by:
Is there any way to programmatically discover and inspect *all* the properties of an object, even those that are not enumerable through a for/in loop? Thanks! kj -- NOTE: In my address...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
1
by: Michael | last post by:
Hi All, I've finally getting into .NET and have a question for you all. I'm creating an app that needs to insert user data into a word doc. One way I thought of doing this was to save the Word...
1
by: Horst Klein | last post by:
I want set different properties of any object at runtime. With the Function below it works well, but not perfect Is there a way to set properties of any type with its value Principle...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
0
by: Robertson1995 | last post by:
I have been using Reflections for a while and I am familiar with macros, but am new to VBA. I need a VBA code that I think is fairly simple, but being new I need help. I have a piece of equipment...
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
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...
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
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,...

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.