2

Let's say I have type Point2D (this is example), how can I use this type with ConfigurationSection -- in other words, what methods I have to implement in order value of my type be created from string.

One way would be to provide TypeConverter, is there another way? Without introducing extra types, solely within my custom type (in this example Point2D)?

Another way, but it is more a trick, would be for example having properties x and y of know type int, and then provide creator property to get Point2D -- I don't want to go that path.

Update: Example as requested:

public sealed class MyConfig : ConfigurationSection
{
    [ConfigurationProperty("LeftPoint", IsRequired = true),
        TypeConverter(typeof(Point2DTypeConverter))]
    public Point2D LeftPoint
    {
        get { return (Point2D )this["LeftPoint"]; }
        set { this["LeftPoint"] = value; }
    }

    ...
}

as you can see I use type converter for Point2D and it works. No problem. However I wonder if it would be possible to do conversion (parsing really from string) inside my type Point2D so I could remove entire converter (not as attribute tag, but completely).

astrowalker
  • 3,123
  • 3
  • 21
  • 40
  • use some serialization to local files is enough, why must put in config files? – Lei Yang Jul 12 '17 at 12:07
  • Why would you want to put your point in configuration section? – Vlad Stryapko Jul 12 '17 at 12:09
  • @LeiYang, I have to put in configuration file, this is requirement. – astrowalker Jul 12 '17 at 12:11
  • this is bad requirement – Lei Yang Jul 12 '17 at 12:12
  • Guys, I am not asking about how/why to ditch config files, my question is about `ConfigurationSection` type. And I ask how it converts strings into given type -- is this conversion doable without `TypeConverter` or not. That's it. Please put your judgments how software should be written on other day, thank you. – astrowalker Jul 12 '17 at 12:20
  • in config file there' only string. – Lei Yang Jul 12 '17 at 12:21
  • @LeiYang, in config files there are strings, in your/my config **type** there are given types, like `Point2D`. So `ConfigurationSection` somehow converts strings to your/my type. One way of this conversion is `TypeConverter`, is any other mechanism of conversion? – astrowalker Jul 12 '17 at 12:30
  • 1
    Would `implicit conversion operator` be of help here? – Ofir Winegarten Jul 12 '17 at 12:38
  • @OfirWinegarten, thank you, just checked, it is ignored. – astrowalker Jul 12 '17 at 12:58
  • Would be good to provide your code for configuration section so that we know exactly what you mean. – Evk Jul 12 '17 at 13:46
  • @Evk, I updated the question. – astrowalker Jul 12 '17 at 14:00
  • 1
    I think there is no way to do that without TypeConverter, because of how it works internally. When ConfigurationSection is parsed - it will always look for type converter for given property type (if it does not inherit from ConfigurationElement) and fail it none can be found. The whole config files system in .NET is unintuitive, inextensible and hard to use, that's why people trying to avoid it at all costs. I don't remember to ever creating custom config sections in at least last 6 years. – Evk Jul 12 '17 at 14:21
  • @Evk, my personal decision would be the same, it is a tragic system, but as I wrote, this is a requirement (and I don't make those). Anyway, could you post your comment as answer, then I would accept it as solution. – astrowalker Jul 13 '17 at 06:01

2 Answers2

2

Since it's a solid requirement, I would suggest using Newtonsoft.Json to convert to and from a string.

string MyPoint = Json.Serialize(MyPointObject)

Point2d MyPoint = Json.Deserialize(ConfigurationSection..)

You also may abstract your serialization logic by creating custom ConfigurationSections as explained here: How:To Create Custom ConfigurationSections

Adam Vincent
  • 3,281
  • 14
  • 38
  • Thank you, I am not marking it as solution, since it as external mechanism as using `TypeConverter`. – astrowalker Jul 12 '17 at 13:01
  • You said "Without introducing extra types" not without extra mechanisms. Update to answer incoming. – Adam Vincent Jul 12 '17 at 13:06
  • actually, I need about a couple hours to get an update to this answer written. Basically, it would be creating the custom config section to handle a `Section` of Point, with `Elements` of `X` and `Y`. – Adam Vincent Jul 12 '17 at 13:22
  • Oh no, I know how to split into config elements, I am thinking about single value which is parsed -- think of TimeSpan for example. – astrowalker Jul 12 '17 at 13:55
2

TypeConverter is required in one form or another for ConfigurationProperty. If you don't provide one - it will look for it with TypeDescriptor.GetConverter if necessary. For that to work your type itself should be decorated with TypeConverter attribute, so you cannot get rid of it this way (though you can move responsibility of conversion to the type itself). You can also use generic TypeConverter, but for that you need to pass target type to it which is not possible using ConfigurationProperty attribute (but is possible if you configure your properties manually, without using attributes). All in all - there is always TypeConverter involved.

Evk
  • 98,527
  • 8
  • 141
  • 191