473,396 Members | 1,764 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.

How convert a string to a type for use in reflection

Hi all.

Is possible I coerce a value to a determined type informed by a string? See
the sample:
dim obj as Object

obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")

or

obj = SomeConversionType("System.Windows.Forms.DockStyle .Bottom")

?

I need use that because I'm loading and setting an object via reflection,
and for reflection can find the corret Property, I must to inform the exact
value type.

Its possible do that? Or have other way?

[]s
Cesar
Nov 21 '05 #1
2 2213
I think this is what you want:

Imports System.Windows.Forms
....
Dim obj As Object
Dim ds As DockStyle
obj = [Enum].Parse(GetType(DockStyle), "Bottom")
ds = CType(obj, DockStyle)

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/
"Ronchese" <ro******@smlinfo.com.br> wrote in message
news:u1**************@tk2msftngp13.phx.gbl...
Hi all.

Is possible I coerce a value to a determined type informed by a string?
See
the sample:
dim obj as Object

obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")

or

obj = SomeConversionType("System.Windows.Forms.DockStyle .Bottom")

?

I need use that because I'm loading and setting an object via reflection,
and for reflection can find the corret Property, I must to inform the
exact
value type.

Its possible do that? Or have other way?

[]s
Cesar

Nov 21 '05 #2
Hmm.. no.

More exactly, I need to convert any string informed because I cant wonder
wich type the developer user will put in my xml config file (in my case, I
get from this file to use the reflection). This way, I need to convert the
complete string (for samples, System.String,
System.Windows.Forms.DockingStyle, System.Drawing.Color, etc)

Cesar

"Rob Windsor [MVP]" <ro*****************@gmail.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
I think this is what you want:

Imports System.Windows.Forms
....
Dim obj As Object
Dim ds As DockStyle
obj = [Enum].Parse(GetType(DockStyle), "Bottom")
ds = CType(obj, DockStyle)

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/
"Ronchese" <ro******@smlinfo.com.br> wrote in message
news:u1**************@tk2msftngp13.phx.gbl...
Hi all.

Is possible I coerce a value to a determined type informed by a string?
See
the sample:
dim obj as Object

obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")

or

obj = SomeConversionType("System.Windows.Forms.DockStyle .Bottom")

?

I need use that because I'm loading and setting an object via reflection,
and for reflection can find the corret Property, I must to inform the
exact
value type.

Its possible do that? Or have other way?

[]s
Cesar


Nov 21 '05 #3

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

Similar topics

4
by: muffinman | last post by:
Hello, I try to convert a string (char or string or CString, etc) into an executable function. For example : char sample = "cout << \"sample\""; char setColor = "glColor3f(1.0f, 1.0f,...
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...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
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.