How convert a string to a type for use in reflection

  • Thread starter Thread starter Ronchese
  • Start date Start date
R

Ronchese

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
 
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)
 
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



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)
 
Back
Top