Pergunta

Como posso converter para um anulável a partir de uma string usando reflexão?

Eu tenho o seguinte código para converter para quase qualquer tipo de valor, com quase qualquer valor. Há um pouco de código acima disso para usar o isassignable de, etc. Portanto, este é o último recurso.

MethodInfo parse = t.GetMethod("Parse", new Type[] { typeof(string) });

if (parse != null)
{
    object parsed = parse.Invoke(null, new object[] { value.ToString() });
    return (T)parsed;
}
else
{
    throw new InvalidOperationException("The value you specified is not a valid " + typeof(T).ToString());
}

O problema surge quando eu quero converter para um tipo anulável como Long?.

Obviamente, o longo? A classe não possui um método parse.

Como faço para extrair o método parse do Nullable's modelo modelo?

EDITAR:

Aqui está uma curta bateria de testes que estou tentando passar:

[Test]
public void ConverterTNullable()
{
    Assert.That((int?)1, Is.EqualTo(Converter<int?>.Convert(1)));
    Assert.That((int?)2, Is.EqualTo(Converter<int?>.Convert(2.0d)));
    Assert.That(3, Is.EqualTo(Converter<long>.Convert(3)));

    Assert.That((object)null, Is.EqualTo(Converter<long?>.Convert("")));
    Assert.That((object)null, Is.EqualTo(Converter<long?>.Convert(null)));
    Assert.That((object)null, Is.EqualTo(Converter<long?>.Convert(DBNull.Value)));

    Assert.That((long)1, Is.EqualTo(Converter<long?>.Convert("1")));
    Assert.That((long)2, Is.EqualTo(Converter<long?>.Convert(2.0)));
    Assert.That((long?)3, Is.EqualTo(Converter<long>.Convert(3)));
}

E toda a função:

/// <summary>
/// Converts any compatible object to an instance of T.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>The converted value.</returns>
public static T Convert(object value)
{
    if (value is T)
    {
        return (T)value;
    }

    Type t = typeof(T);

    if (t == typeof(string))
    {
        if (value is DBNull || value == null)
        {
            return (T)(object)null;
        }
        else
        {
            return (T)(object)(value.ToString());
        }
    }
    else
    {
        if (value is DBNull || value == null)
        {
            return default(T);
        }

        if (value is string && string.IsNullOrEmpty((string)value))
        {
            return default(T);
        }

        try
        {
            return (T)value;
        }
        catch (InvalidCastException)
        {
        }

        if (Nullable.GetUnderlyingType(t) != null)
        {
            t = Nullable.GetUnderlyingType(t);
        }

        MethodInfo parse = t.GetMethod("Parse", new Type[] { typeof(string) });

        if (parse != null)
        {
            object parsed = parse.Invoke(null, new object[] { value.ToString() });
            return (T)parsed;
        }
        else
        {
            throw new InvalidOperationException("The value you specified is not a valid " + typeof(T).ToString());
        }
    }
}
Foi útil?

Solução 3

Eu adicionei esse pouco:

if (Nullable.GetUnderlyingType(t) != null)
{
    t = Nullable.GetUnderlyingType(t);
}

MethodInfo parse = t.GetMethod("Parse", new Type[] { typeof(string) });

Obrigado a todos!

Outras dicas

Eu provavelmente usaria o TypeConverter nesse caso, e Nullable.GetUnderlyingType(); Exemplo no caminho ...

    static void Main()
    {
        long? val1 = Parse<long?>("123");
        long? val2 = Parse<long?>(null);
    }

    static T Parse<T>(string value)
    {
        return (T) TypeDescriptor.GetConverter(typeof(T))
            .ConvertFrom(value);
    }

Isso deve para o trabalho. (O código está incorporado em um método de extensão de simplicidade, embora possa não ser o que você deseja.)

public static T? ParseToNullable<T>(this string value) where T : struct
{
    var parseMethod = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
    if (parseMethod == null)
        return new Nullable<T>();

    try
    {
        var value = parseMethod.Invoke(null, new object[] { value.ToString() });
        return new Nullable<T>((T)value);
    }
    catch
    {
        return new Nullable<T>();
    }
}

Agora, se você deseja o paramter do tipo genérico em si Para ser o tipo anulado e não o subjacente (eu realmente não vejo uma vantagem nisso), você pode usar:

Nullable.GetUnderlyingType(typeof(T))

Como Marc Granell sugeriu, e isso exigiria apenas algumas pequenas modificações no código.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top