All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csharp-functions.typeConverter.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
using System;
using System.ComponentModel;
using System.Globalization;
using Newtonsoft.Json;

namespace {{packageName}}.Converters
{
    /// 
    /// Custom string to enum converter
    /// 
    public class CustomEnumConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var s = value as string;
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }

            return JsonConvert.DeserializeObject(@"""" + value.ToString() + @"""");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy