我有一个结构,我想让它更容易填充
public struct DBParameter
{
public string parameterName;
public object value;
public DbType dbType;
public DBParameter(string paramName, object val, DbType type)
{
parameterName = paramName;
value = val;
dbType = type;
}
public SqlParameter ToSqlParameter()
{
if (parameterName == string.Empty || parameterName == null)
throw new ArgumentException("No parameter, fieldname is mandatory", "parameterName");
SqlParameter me = new SqlParameter(parameterName, value);
me.DbType = dbType;
return me;
}
我在想这样的事情......
public void LoadDBParameter<T>(string paramName, T val)
{
parameterName = paramName;
value = val;
if (val is Int32)
{
dbType = DbType.Int32;
}
if (val is String)
{
dbType = DbType.String;
}
if (val is Int64)
{
dbType = DbType.Int64;
}
if (true)
{
//(...)
}
}
}
但是我一直看到名称几乎相同,有没有比编写每个可能的常见数据结构更简单的方法?