在C#中使用`where T:SOMETHING`构造

Eoi*_*ell 2 c# generics

刚刚找到了一些代码,这里有人为了访问一些数据库实体而编写的...

public static OurCustomObject GetOurCustomObject(int primaryKey)
{
    return GetOurCustomObject<int>(primaryKey, "usp_GetOurCustomObjectByID");
}

public static OurCustomObject GetOurCustomObject(Guid uniqueIdent)
{
    return GetOurCustomObject<Guid>(uniqueIdent, "usp_GetOurCustomObjectByGUID");
}

private static OurCustomObject<T>(T identifier, string sproc)
{

    if((T != typeof(int)) && (T == typeof(Guid)))
    {
        throw new ArgumentException("Identifier must be a string or an int");
    }

    //ADO.NET Code to make DB Call with supplied sproc.
}
Run Code Online (Sandbox Code Playgroud)

只是它的东西看起来不是很好generic.将sprocs传递到内部方法的事实感觉很难看.但我能看到的唯一方法就是在私有方法中使用if/else

if(type == int)
    sproc = "GetByID";
else if (type == Guid)
    sproc = "GetByGUID";
Run Code Online (Sandbox Code Playgroud)

此外抛出的异常看起来也很丑......无论如何都要使用where T:子句

例如

private static OurCustomObject<T>(T identifier) where T : int OR Guid
Run Code Online (Sandbox Code Playgroud)

关于如何清理这一点的任何建议.

Jon*_*eet 8

你不能指定一个约束,上面写着"它是这两个中的一个",不是.

可以做的是:

Dictionary<Type, string> StoredProcedureByType = new Dictionary<Type, string>
{
    { typeof(Guid), "GetByGUID" },
    { typeof(int), "GetByID" }
};
Run Code Online (Sandbox Code Playgroud)

然后使用:

string sproc;
if (!StoredProcedureByType.TryGetValue(typeof(T), out sproc))
{
    throw new ArgumentException("Invalid type: " + typeof(T).Name);
}
Run Code Online (Sandbox Code Playgroud)

这对于几种类型来说可能有些过分,但如果你涉及很多类型,它可以很好地扩展.

鉴于这两种类型是值类型,你可以把它一个有点多用的约束稳健:

where T : struct
Run Code Online (Sandbox Code Playgroud)

但这仍然会允许byte