In C#, I have the enum data type:
public enum DebugLevel
{
    NONE = 0,
    ERROR = 1,
    ...
}
inside an object:
public class DataOptions
{
    #region Fields
    public DebugLevel debug = DebugLevel.NONE;
    ...
and the data is provided via POST in a FormData as an int:
Form Data:
    debug: 0
and is then parsed in a WebAPI like this:
[HttpPost]
public AjaxAnswer<SomeDataType[]> MyEndpoint(HttpRequestMessage req) {
    DataOptions o = null;
    try
    {
        o = req.DecodeJSON<DataOptions>();
    }
    catch (Newtonsoft.Json.JsonReaderException) // Not JSON - old frontend versions are sending as form data
    {
        try { 
            o = req.DecodeFormData<DataOptions>();
        } catch(Exception e) {
            return new AjaxAnswer<SomeDataType[]>() {success:false, data:new SomeDataType[0], Error: "Error in MyEndpoint", ErrorMessage: "Could not decode request payload: " + e.Message
        }
}
DecodeFormData is a custom Extension Method which looks like this:
public static T DecodeFormData<T>(this System.Net.Http.HttpRequestMessage req) where T : new()
{
    string postdata = req.Content.ReadAsStringAsync().Result;
    Dictionary<string, string> s = postdata.Split('&').Where(x=>x.Contains('=')).Select(x => x.Split('=')).ToDictionary(x => x[0], x => HttpContext.Current.Server.UrlDecode(x[1]));
    return s.ToType<T>();
}
For some reason, this code throws the error:
Invalid cast from 'System.String' to 'MyNamespace.DebugLevel'
Conversions without that enum run through fine. What is happening there (behind the s.ToType<T>() line) and why?
 
     
    