考虑以下typeof运算和一个基类
class Base
{
public int id { get; set; }
}
class Sub1 : Base
{
public int x { get; set; }
public int y { get; set; }
}
class Sub2 : Base
{
public string x { get; set; }
public string y { get; set; }
}
class Wrapper
{
public int x { get; set; }
public Sub1 sub1 { get; set; }
public Sub2 sub2 { get; set; }
}
什么,我试图做的是下面的,我有这样的效用函数,从这个函数得到CLR类型
private static Dictionary<Type, SqlDbType> types;
public static SqlDbType GetSqlDbType(Type type, string propertyName)
{
if (types == null)
{
types = new Dictionary<Type, SqlDbType>();
types.Add(typeof(Int32), SqlDbType.Int);
types.Add(typeof(Int32?), SqlDbType.Int);
types.Add(typeof(decimal), SqlDbType.Decimal);
//etc
//the problem is here i want to return SqlDbType.VarBinary for every class that inherits Base
types.Add(typeof(Base), SqlDbType.VarBinary);
}
return types[type];
}
SQL类型如果类型是从Base类继承的,我想返回SqlDbType.VarBinary,这可能吗?
尝试IsAssignableFrom() – Liath