没有好的办法一般是如下的形式:
if (objValue == DbNull.Value)
{
    return null//return ""//return .....
}
else
{
    return ......
}

解决方案 »

  1.   

    给个方法参考一下
    public void SetDefaultValue(object value)
    {
    Type type=value.GetType();
    foreach(PropertyInfo propertyInfo in type.GetProperties())
    {
    try
    {
    if(propertyInfo.PropertyType==typeof(int) || propertyInfo.DeclaringType==typeof(short))
    propertyInfo.SetValue(value,0,null); else if(propertyInfo.PropertyType==typeof(string))
    propertyInfo.SetValue(value,string.Empty,null); else if(propertyInfo.PropertyType==typeof(DateTime))
    propertyInfo.SetValue(value,DateTime.Now,null); else if(propertyInfo.PropertyType==typeof(bool))
    propertyInfo.SetValue(value,false,null); else if(propertyInfo.PropertyType==typeof(Decimal))
    {
    Decimal dValue=0;
    propertyInfo.SetValue(value,dValue,null);
    }
    else if(propertyInfo.PropertyType.IsEnum)
    {
    object enumValue=Activator.CreateInstance(propertyInfo.PropertyType);
    enumValue= System.Enum.GetValues(propertyInfo.PropertyType).GetValue(0);
    propertyInfo.SetValue(value,enumValue,null);
    }
    else
    {
    string s=propertyInfo.Name;
                                                           //暂时就这么多类型
    }
    }
    catch(Exception){}
    }
    }