|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using a TypeConverter instead of Type.GetType(String)Does anyone know of a way (without looking through decompiled ASP.Net code)
to convert a String to a Type using the generic TypeConverter stuff? I am writing a deserializer, but I would prefer not to have a custom codepath just for String->Type, but the following code throws a NotSupportedException: TypeDescriptor.GetConverter(typeof(Type)).ConvertFrom("System.String") The problem with the above code is that GetConverter returns the base TypeDescriptor, which always throws an exception on ConvertFrom, but I'm not aware of any gneeric codepaths to do this that don't involve typedescriptors What do you mean by "convert a String to a Type?" Are you referring to
converting a string to the type "System.Type?" You didn't mention any other type, and your example uses "typeof(Type)" which seems to bear this out. If so, you are barking way up the wrong tree. A string can not be converted to the type "System.Type". To convert one type to another, the data must be consistent with the type being converted to. A string can be converted, for example, to an array, because it IS an array. It can be converted to a number if the characters in the string can be read as a number (as in "123.456"). But the System.Type class has nothing in common with any string. Perhaps you could tell us what requirement you're trying to fulfill, and we can help you get there. -- Show quoteHTH, Kevin Spencer Microsoft MVP Software Composer http://unclechutney.blogspot.com A watched clock never boils. "Keith Patrick" <richard_keith_patrick@nospam.hotmail.com> wrote in message news:e%23ZKaYo3GHA.5032@TK2MSFTNGP04.phx.gbl... > Does anyone know of a way (without looking through decompiled ASP.Net > code) to convert a String to a Type using the generic TypeConverter stuff? > I am writing a deserializer, but I would prefer not to have a custom > codepath just for String->Type, but the following code throws a > NotSupportedException: > > > TypeDescriptor.GetConverter(typeof(Type)).ConvertFrom("System.String") > > The problem with the above code is that GetConverter returns the base > TypeDescriptor, which always throws an exception on ConvertFrom, but I'm > not aware of any gneeric codepaths to do this that don't involve > typedescriptors > Yes, what I'm looking to do is convert a string representation of a typename
into an actual System.Type, like Type.GetType(String) does. I'm trying to deserialize something that looks like this: <MyElement OwningType="System.Uri, System"> </MyElement> which would correspond to a type: using System.Net; public class MyElement { private Type _OwningType; public Type OwningType { get { return this._OwningType; } set { this._OwningType = value; } } which after derialization would give me: myDeserialedInstanceOfMyElement.OwningType.FullName.Equals("System.Uri, System") == true I'm trying to avoid having a special case for deserializing XML attributes
if the type is Type, hence the focus on TypeConverter. I need to convert a string-based value to an object's property, and one class I have has a property of type System.Type. I had thought that ASP.Net handles that, and was going to see how it did it, but apparently, that is not the case, and I am going to have to go with it at least for now |
|||||||||||||||||||||||