我正在尝试使用泛型将一整套遗留方法隐藏在单个扩展方法后面。这些遗留方法都称为 GetValidXXX 并且具有类似的签名(是的,它们真的应该不在ref )。为了向后兼容,需要保留旧的 GetValidXXX。
public static T GetAttributeValue<T>(this DbElement element, DbAttribute attribute, T defaultValue)
{
T result = default(T);
if (typeof(T) == typeof(DbAttribute))
{
if (element.GetValidAttribute(attribute, ref result)) return result;
}
else if (typeof(T) == typeof(bool))
{
if (element.GetValidBool(attribute, ref result)) return result;
}
return defaultValue;
}
这不会编译,因为结果与特定 GetValidXXX 签名中的类型不匹配(返回值为成功/失败)。
bool GetValidAttribute(DbAttribute attribute, ref DbAttribute result)
bool GetValidBool(DbAttribute attribute, ref bool result)
etc
我如何编写它来实现我的目标,即能够编写如下所示的代码:
string description = element.GetAttributeValue(DbAttributeInstance.DESC, "unset");
bool isWritable = !element.GetAttributeValue(DbAttributeInstance.READONLY, true);