Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I return the specified type in generic method?

I want to get specified type value from database. Here is my code:

public static T Get<T>(string key)
        {
            Databases.Context Context = new Databases.Context();
            var i = Context.Setting.FirstOrDefault(_ => _.Key == key)?.Value;
            if (i == null) {
                return default;
            }
            switch (typeof(T))
            {
                case Type intType when intType == typeof(int):
                    {
                        if (int.TryParse(i, out int Result))
                        {
                            return Result;
                        }
                        else{
                            return default;
                        }
                    }
            }
        }

As the code above, part of my code is about to return int type value. However, VS reports an error that it cannot convert int to T. How can I solve this? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To solve the error, you can use the Convert class to explicitly convert the int value to the desired type T.

public static T Get<T>(string key)
{
    Databases.Context Context = new Databases.Context();
    var i = Context.Setting.FirstOrDefault(_ => _.Key == key)?.Value;
    if (i == null) {
        return default;
    }
    switch (typeof(T))
    {
        case Type intType when intType == typeof(int):
            {
                if (int.TryParse(i, out int Result))
                {
                    return (T)(object)Result;
                }
                else{
                    return default;
                }
            }
    }
}

Alternatively, you can use the System.ComponentModel.TypeConverter class to perform the conversion. This approach allows you to handle more complex types and may be more flexible in some cases.

You can modify your code to use TypeConverter,

public static T Get<T>(string key)
{
    Databases.Context Context = new Databases.Context();
    var i = Context.Setting.FirstOrDefault(_ => _.Key == key)?.Value;
    if (i == null) {
        return default;
    }
    var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
    if (converter.CanConvertFrom(typeof(string)))
    {
        return (T)converter.ConvertFromString(i);
    }
    else
    {
        return default;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading