转换为可空< T>从使用字符串思考转换为、字符串、LT、GT

2023-09-03 13:54:50 作者:亡心少女

使用反射我如何可以转换为可空的字符串?

我有以下的code转换到几乎放弃几乎所有值的值类型。有相当多的code以上这种使用IsAssignableFrom等,所以这是最后的手段。

  MethodInfo的解析= t.GetMethod(解析,新类型[] {typeof运算(字符串)});

如果(解析!= NULL)
{
    对象解析= parse.Invoke(空,新的对象[] {value.ToString()});
    返程(T)解析;
}
其他
{
    抛出新的InvalidOperationException异常(您指定的值不是有​​效的+ typeof运算(T)的ToString());
}
 

在问题出现时,我想转换为可空类型一样长?

显然,长时间?类没有一个分析方法。

我如何从提取解析方法可空的模板的类型?

编辑:

下面是测试我试图传递一个短暂的电池:

  [测试]
公共无效ConverterTNullable()
{
    Assert.That((int)的1,Is.EqualTo(转换器和所述;?int的> .Convert(1)));
    Assert.That((int)的2,Is.EqualTo(转换器和所述;?int的> .Convert(2.0D)));
    Assert.That(3,Is.EqualTo(转换器<长> .Convert(3)));

    Assert.That((对象)空,Is.EqualTo(转换器<长> .Convert()));
    Assert.That((对象)空,Is.EqualTo(转换器<长> .Convert(空)));
    Assert.That((对象)空,Is.EqualTo(转换器<长> .Convert(的DBNull.Value)));

    Assert.That((长)1,Is.EqualTo(转换器<长> .Convert(1)));
    Assert.That((长)2,Is.EqualTo(转换器<长> .Convert(2.0)));
    Assert.That((长)3,Is.EqualTo(转换器<长> .Convert(3)));
}
 
网奇cms sql版的安装与转换

和全功能:

  ///<总结>
///任何兼容的对象转换为T的一个实例
///< /总结>
///<参数名称=值>要转换的值小于/参数>
///<返回>转换后的值小于/回报>
公共静态牛逼转换(对象的值)
{
    如果(值T)
    {
        返回(T)值;
    }

    类型t = typeof运算(T);

    如果(T == typeof运算(字符串))
    {
        如果(值是为DBNull ||值== NULL)
        {
            返回(T)(对象)空;
        }
        其他
        {
            返回(T)的(对象)(value.ToString());
        }
    }
    其他
    {
        如果(值是为DBNull ||值== NULL)
        {
            返回默认(T);
        }

        如果(值是字符串和放大器;&安培; string.IsNullOrEmpty((字符串)值))
        {
            返回默认(T);
        }

        尝试
        {
            返回(T)值;
        }
        赶上(InvalidCastException的)
        {
        }

        如果(Nullable.GetUnderlyingType(T)!= NULL)
        {
            T = Nullable.GetUnderlyingType(T);
        }

        MethodInfo的解析= t.GetMethod(解析,新类型[] {typeof运算(字符串)});

        如果(解析!= NULL)
        {
            对象解析= parse.Invoke(空,新的对象[] {value.ToString()});
            返程(T)解析;
        }
        其他
        {
            抛出新的InvalidOperationException异常(您指定的值不是有​​效的+ typeof运算(T)的ToString());
        }
    }
}
 

解决方案

我加这一点点:

 如果(Nullable.GetUnderlyingType(T)!= NULL)
{
    T = Nullable.GetUnderlyingType(T);
}

MethodInfo的解析= t.GetMethod(解析,新类型[] {typeof运算(字符串)});
 

谢谢大家!

How can I convert TO a Nullable from a String using reflection?

I have the following code to convert TO almost any value type given almost any value. There is quite a bit of code above this to use the IsAssignableFrom, etc. so this is the last resort.

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());
}

The problem comes when I want to to convert to a nullable type like long?.

Obviously, the long? class does not have a parse method.

How do I extract the parse method from the nullable's template type?

EDIT:

Here is a short battery of tests I'm trying to pass:

[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)));
}

And the whole function:

/// <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());
        }
    }
}

解决方案

I added this little bit:

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

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

Thanks everyone!