7

i want to define a custom configuration section and have a property not yield a string but a system.type (or null if the user types in a load of rubbish)

e.g.:

<myCustomConfig myAnnoyingType="System.String" />

in the C# (in the current real world)

[ConfigurationProperty("myAnnoyingType")]
public string MyAnnoyingType
{
   get { return (string)this["myAnnoyingType"]; }
}

// else where in the app
var stringType = thatConfig.MyAnnoyingType
var actualType = Type.GetType(stringType);
// wow that was boring.

in the C# (in an ideal world)

[ConfigurationProperty("myAnnoyingType")]
public Type MyAnnoyingType
{
    get { return (Type)this["myAnnoyingType"]; }
}

What I want is NOT to have to keep the item as a string in the C# and then convert that into a Type in the application; i'd like this to be done automatically as part of the responsibility of the ConfigurationManager.

Is this possible? I'm OK to use a TypeConverter if I have to be, it just seems so weak to keep it as a string and then do the type lookup in the application. It's not hard to do, just seems pointless when I know i'm looking for a type, what is the value of having to explicitly do it.

peteisace
  • 2,754
  • 1
  • 19
  • 19

2 Answers2

7

Try using a TypeConverterAttribute that will do the conversion for you. This worked for me.

    [ConfigurationProperty("type")]
    [TypeConverter(typeof(TypeNameConverter))]
    public Type Type
    {
        get
        {
            return (System.Type)this["type"];
        }
        set
        {
            this["type"] = value;
        }
    }
davidlbaileysr
  • 644
  • 6
  • 6
  • This works very well indeed in case your intended behavior is in fact that the whole configuration section fails to load when user of this code makes a mistake with the type name. Not saying that it shouldn't be the intended behavior, but maybe not always. – Yuri Makassiouk Jan 05 '21 at 17:57
3

First, its better to define a type in your settings using the fully qualified name. This way you get less problems with resolving the Type from the string.

Second, you need to find the Type by it's string name as was already answered in Convert String to Type in C# because it's not possible just cast string to Type.

In your case it would be:

[ConfigurationProperty("myAnnoyingType")]
public Type MyAnnoyingType
{
    get { return Type.GetType(this["myAnnoyingType"]); }
}
Community
  • 1
  • 1
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
  • i think you misunderstand my question. i'm well aware of how to convert from string to type, having used .net for longer than 20 minutes. my question is about how to ensure this is automagically done when using configuration section files, and not having to do it in the application at all. of course i can keep it as a string in the config then convert that string into System.Type in the app. i don't want to; it should do it automatically ideally. that is the question. – peteisace Jul 02 '15 at 23:22
  • 4
    actually i'm being unfair. although it wasn't what i was looking for, this does reasonably answer the question @kirmir sorry, i was too hasty and too harsh. this is fair, altho not quite what i was after. – peteisace Jul 02 '15 at 23:30