Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find a converter that supports conversion to/from string for the property of type 'Type'

Tags:

I'm writing a custom config class in C# and .NET 3.5. One of the properties should be of type System.Type. When I run the code I get the error mentioned in the title.

[ConfigurationProperty("alertType", IsRequired = true)] public Type AlertType {     get { return (Type)this["alertType"]; }     set { this["alertType"] = value; } } 

The config file looks like this:

<add name="Name" pollingInterval="60" alertType="Namespace.ClassName, Company.Project" /> 

The .net framework is able to cast a string into System.Type, because the configSections of the config file has a type attribute. The question is how do they do it.

like image 691
Jonas Stawski Avatar asked Feb 23 '10 21:02

Jonas Stawski


2 Answers

I know this is old, but I think this is actually the correct answer:

[TypeConverter(typeof(TypeNameConverter))] [ConfigurationProperty("alertType", IsRequired=true)] public Type AlertType {     get { return this[ "alertType" ] as Type; }     set { this[ "alertType" ] = value; } } 

Adding the TypeNameConverter makes the transformation from String to Type happen without using Type.GetType().

like image 109
Shawn Hall Avatar answered Jun 23 '23 04:06

Shawn Hall


I think you're looking for Type.GetType Method (String)

like image 28
Preet Sangha Avatar answered Jun 23 '23 03:06

Preet Sangha