creating a value type by reflection

  • Thread starter Thread starter farseer
  • Start date Start date
F

farseer

How can i do this?

i have the following in a config file:

name="id"
type="int32"

can i create a variable called "id" as type int32?
 
farseer said:
How can i do this?

i have the following in a config file:

name="id"
type="int32"

can i create a variable called "id" as type int32?

You (normally, without using CodeDom/Emit) cannot create values at runtime.
What exactly do you want to archieve?
 
it's a bit difficult to explain..but i'll try...

I an XML file which contains elements with either an "id" or "code
attribute. these correspond to keys in a hashtable that contains
ILists. so in that hashtable might be entires like
123, List1
"AOL", List2

so you see, the hashtable can be keyed by either Ints or String values.


I have config file which tells my app what elements in the XML file to
look for and what "Type" their keys are.

elementName: "Company"
keyName: "id"
keyType: "int32"

elementName: "Issuer"
keyName: "code"
keyType: "string"

so with this config file, i know what elements i need to look for and
what attribute i need to fetch to extract any item from my Hashtable.
but because the keys in the HashTable can be either of Int32 or String,
i need to be able to generate that Type by reflection before being able
to retreive lists from the a hashtable.

this is a bit confusing, and may not make sense..but it's simplest way
i can think of explaining what i want to do...
 
Farseer,
In addition to the other comments, have you considered using
Convert.ChangeType?

Something like:
Dim table As New Hashtable

Dim name As String = "id"
Dim value As String = "100"
Dim typeName As String = "System.Int32"

Dim conversionType As Type = Type.GetType(typeName)
table.Add(name, Convert.ChangeType(value, conversionType))

name = "code"
value = "Something"
typeName = "System.String"

conversionType = Type.GetType(typeName)
table.Add(name, Convert.ChangeType(value, conversionType))


For Each de As DictionaryEntry In table
Debug.WriteLine(de.Value, de.Key.ToString())
Next



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| How can i do this?
|
| i have the following in a config file:
|
| name="id"
| type="int32"
|
| can i create a variable called "id" as type int32?
|
 
Jay,

Is this a new one from you or did I missed it in past, it looks interesting

Although I try forever to avoid the hashtable, don't tell me that it is
nice, just some inbuild behaviour of me.

:-)

Cor
 
farseer said:
it's a bit difficult to explain..but i'll try...

I an XML file which contains elements with either an "id" or "code
attribute. these correspond to keys in a hashtable that contains
ILists. so in that hashtable might be entires like
123, List1
"AOL", List2

so you see, the hashtable can be keyed by either Ints or String
values.

Do the things you're representing as numbers really have to be numbers? As
in, do you perform calculations with them? If not, regard them as strings
and you will have eliminated the problem.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top