PC Review


Reply
Thread Tools Rate Thread

Convert, e.g., string "System.Drawing.Colors.Red" into true datatype?

 
 
Ken Overton
Guest
Posts: n/a
 
      30th Jan 2005
Obviously the system does this for deserialization. So how can I do it
for arbitrary classes (not just enums)? Does this require generating a
temporary assembly and compiling it?

-- kov
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      30th Jan 2005
Ken,

>Obviously the system does this for deserialization. So how can I do it
>for arbitrary classes (not just enums)?


Type.GetType() or Assembly.GetType(). Since there can be multiple
types with the same name in different assemblies, you need to specify
which asembly to look in one way or another.


>Does this require generating a temporary assembly and compiling it?


No



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Ken Overton
Guest
Posts: n/a
 
      30th Jan 2005
Mattias Sjögren wrote:
>
>>Obviously the system does this for deserialization. So how can I do it
>>for arbitrary classes (not just enums)?

>
> Type.GetType() or Assembly.GetType(). Since there can be multiple
> types with the same name in different assemblies, you need to specify
> which asembly to look in one way or another.


Sorry, I guess I'm a bit dense. I understand that
Type.GetType("System.Drawing.Color") will give me a Type object with all
the info about that class, but how can I use a Type object to actually
*create an instance* of that class? In my example above I suppose I
could get the PropertyInfo for the static property
System.Drawing.Colors.Red, but I'm not sure that is a generic solution
for all constant expressions of all types. I'm looking for a generic
solution to produce an instance of a constant given that constant
expression as a string and the type, like for example:

Class String Constant expression string
============ ======
"System.Drawing.Color" "Red"
"System.Int32" "0x0f"
"MyOwnClass.PiDouble" "3.14"
etc.

As far as I can see, if you want to call a method/property on an
instance via reflection, the instance has to already be created. I
suppose I could make my own factory to parse expressions and figure out
how to create them, it just seems like .NET already has that somewhere.
Either that or I have a poor understanding of the problem, which could
very well be.

-- kov
 
Reply With Quote
 
Ken Overton
Guest
Posts: n/a
 
      31st Jan 2005
Nathan Baulch wrote:
>
> Your best bet is to use a TypeConverter.
> You'll need to apply the TypeConverterAttribute to your custom classes if
> you want them to work with this approach.


Thank you, Nathan, that looks like it's exactly what I was missing.

Cheers,

-- kov
 
Reply With Quote
 
Nathan Baulch
Guest
Posts: n/a
 
      31st Jan 2005
> Sorry, I guess I'm a bit dense. I understand that
> Type.GetType("System.Drawing.Color") will give me a Type object with
> all the info about that class, but how can I use a Type object to
> actually *create an instance* of that class?



Your best bet is to use a TypeConverter.
You'll need to apply the TypeConverterAttribute to your custom classes if
you want them to work with this approach.
The following code demonstrates a few different examples.


Type[] types = new Type[] {
typeof(Int32),
typeof(Color),
typeof(DayOfWeek),
typeof(DateTime),
typeof(MyClass)
};
string[] values = new string[] {
"13",
"Red",
"Saturday",
"2005-05-05",
"abc123"
};
for(int i = 0; i < types.Length; i++) {
object o = TypeDescriptor.GetConverter(
types[i]).ConvertFrom(values[i]);
Console.WriteLine("{0}\t{1}", o.GetType(), o);
}


Nathan


 
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
convert streamreader string to system.drawing.point? Dave Bootsma Microsoft VB .NET 5 8th Sep 2008 11:46 PM
Convert HD file system from "RAW" to "NTFS" on slave drive? (Win X =?Utf-8?B?RVhGRlBN?= Windows XP Help 1 6th Nov 2007 04:49 PM
Convert.Int32("0")causes{"Input string was not in a correct format =?Utf-8?B?SmFtZXMgUG9zZQ==?= Microsoft Dot NET 1 23rd Feb 2006 03:01 AM
err: Cannot implicitly convert type 'string' to 'System.Drawing.Co =?Utf-8?B?b3JpdA==?= Microsoft Dot NET Framework Forms 4 11th May 2005 05:49 AM
Convert "char" into "string" in java privetv7 Microsoft VC .NET 2 27th Dec 2004 09:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:36 PM.