简体   繁体   中英

Convert.ChangeType throwing Invalid Cast Exception for nullable int

Getting an Invalid Cast Exception if I call the GetClaimValue method below where T is a nullable int.

private static T GetClaimValue<T>(string claimType, IEnumerable<Claim> claims)
{
    var claim = claims.SingleOrDefault(c => c.Type == claimType);

    if (claim != null)
        return (T) Convert.ChangeType(claim.Value, typeof(T));

    return default(T);
}

For example:

 GetClaimValue<int?>(IdentityServer.CustomClaimTypes.SupplierId, claims)

Anyone know of a way to deal with this?

I'm assuming Claim.Value is of type Object and you're dynamically converting here, you can't straight up convert an int to an int? via Convert.ChangeType .

One option is to use Nullable.GetUnderlyingType which will check if this is a nullable struct case, do the conversion via the underlying data type first, then cast to T .

You'll also need to handle the null scenario as well.

if (claim != null)
{
    var conversionType = typeof(T);

    if (Nullable.GetUnderlyingType(conversionType) != null)
    {
        if (claim.Value == null) //check the null case!
            return default(T);

        //use conversion to `int` instead if `int?`
        conversionType = Nullable.GetUnderlyingType(conversionType);
    }

    return (T)Convert.ChangeType(claim.Value, conversionType);
}

I can't explain why it's throwing an exception, however I had a similar situation when I was using Convert.ChangeType .

Try grabbing the converter of the type you pass in first, then use that to convert. I've had better results using this method.

var converter = TypeDescriptor.GetConverter(typeof(T));
return (T)converter.ConvertFrom(claim.Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM