PC Review


Reply
Thread Tools Rate Thread

creating a value type by reflection

 
 
farseer
Guest
Posts: n/a
 
      19th Oct 2005
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?

 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      19th Oct 2005
"farseer" <(E-Mail Removed)> schrieb:
> 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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

 
Reply With Quote
 
farseer
Guest
Posts: n/a
 
      19th Oct 2005
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...

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      19th Oct 2005
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


"farseer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| 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?
|


 
Reply With Quote
 
farseer
Guest
Posts: n/a
 
      19th Oct 2005
i will try this. thank you

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      19th Oct 2005
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


 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      20th Oct 2005
farseer wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a type from a generic argument using Reflection? Ympostor Microsoft C# .NET 8 21st Nov 2006 11:10 AM
Reflection: Creating an array of a type? Axel Dahmen Microsoft Dot NET Framework 2 9th Mar 2006 10:47 AM
creating a value type by reflection farseer Microsoft C# .NET 1 19th Oct 2005 08:00 AM
Detect Type with Reflection? xenophon Microsoft Dot NET Framework 1 22nd Aug 2005 07:42 PM
Reflection/Type Question =?Utf-8?B?RWRC?= Microsoft VB .NET 5 6th Feb 2005 03:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 PM.