反射类型的默认值(T)

Bil*_*3rd 4 c# generics reflection

在浏览其他答案时,我想出了以下扩展方法:

public static T Convert<T>( this string input )
{
    var converter = TypeDescriptor.GetConverter( typeof( T ) );
    if ( converter != null )
    {
        try
        {
            T result = (T) converter.ConvertFromString( input );
            return result;
        }
        catch
        {
            return default( T );
        }
    }
    return default( T );
}
Run Code Online (Sandbox Code Playgroud)

我可以像以下一样使用它:

string s = "2011-09-21 17:45";
DateTime result = s.ConvertTo( typeof( DateTime ) );
if ( result == DateTime.MinValue )
    doSomethingWithTheBadData();
Run Code Online (Sandbox Code Playgroud)

真棒!效果很好.现在,我想用反射类型做类似的事情.我有:

public static dynamic ConvertTo( this string input, Type type )
{
    var converter = TypeDescriptor.GetConverter( type );
    if ( converter != null )
    {
        try
        {
            dynamic result = converter.ConvertFromString( input );
            return ( result );
        }
        catch
        {
            return default( type );  // bogus
        }
    }

    return default( type );  // bogus
}
Run Code Online (Sandbox Code Playgroud)

我想这样使用它:

Type someType;  // will be DateTime, int, etc., but not known until runtime
DateTime result = s.ConvertTo( sometype );
if ( result == DateTime.MinValue )
    doSomethingWithTheBadData();
Run Code Online (Sandbox Code Playgroud)

当然,编译器会对ConvertTo方法中的'bogus'行进行反对.我需要的东西(好吧,不一定需要,但它很好)是一种获得与第一个例子相同结果的方法,这样如果转换失败,可以返回可以分配给反射对象的东西并且可以以与第一示例中相同的方式识别.

编辑:

我完成了什么:

public static dynamic ConvertTo( this string input, Type type, out bool success )
{
    dynamic result;

    var converter = TypeDescriptor.GetConverter( type );
    if ( converter != null )
    {
        try
        {
            result = converter.ConvertFromString( input );
            success = true;
            return result;
        }
        catch { /* swallow the exception */ }
    }

    result = type.IsValueType ? Activator.CreateInstance( type ) : null;
    success = false;

    return result;
}
Run Code Online (Sandbox Code Playgroud)

和使用:

bool success;
string val = "2011-09-21 17:25";
dateTime = val.ConvertTo( typeof( DateTime ), out success );
if ( success )
    doSomethingGood();

val = "foo";
dateTime = val.ConvertTo( typeof( DateTime ), out success );
if ( !success )
    dealWithBadData();
Run Code Online (Sandbox Code Playgroud)

记住,为了演示,我很难对typeof()位进行编码.在我的应用程序中,所有类型都反映出来.

感谢所有人的快速回答!

Che*_*hen 9

您可以使用

//to get default(T) from an instance of Type
type.IsValueType ? Activator.CreateInstance(type) : null;
Run Code Online (Sandbox Code Playgroud)

这是因为值类型保证具有默认构造函数,并且引用类型的默认值为a null.

  • 只是输入这个.完善. (2认同)