如何实现可返回class或nullable结构的泛型集合查找方法?

时间:2012-05-02 19:28:21

标签: c# generics

我想为异构集合创建一个类型化的查找辅助函数:它应该返回一个结构或类,如果找不到该项,则返回null。 下面是一个使用普通集合查找的示例,但它可以是数据库调用或其他任何内容。

有没有办法通过单一方法签名实现这一目标?

    public T GetClass<T>(string key)  where T : class
    {
        object o;
        if (Contents.TryGetValue(key, out o))
        {
            return o as T;
        }
        return null;
    }

    public T? GetStruct<T>(string key) where T : struct
    {
        object o;
        if (Contents.TryGetValue(key, out o))
        {
            if (o is T)
            {
                return (T?) o;
            }
        }
        return null;
    }

我已经尝试过:

  • 我知道通用限制不能用于消除重载的歧义。所以我不能简单地给这两个方法赋予相同的名称。
  • 返回(Default) T不是一个选项,因为0是一个有效的int值。
  • 我尝试使用<int ?>作为类型进行调用,但正如所讨论的,Nullable<T>不是引用类型。

有没有办法表明我要返回一个盒装的int?

3 个答案:

答案 0 :(得分:6)

  

有没有办法通过单一方法签名实现这一目标?

使用可选参数有一种horrible(真正可怕的)方式,这样在两种情况下调用代码看起来都一样。但它很蹩脚。

选项:

  • 返回Tuple<T, bool>而不是使用无效
  • 使用out参数(例如int.TryParse等)
  • 使用不同的方法名称

请注意,通过单独表示缺少值,可以使null成为有效的“找到”结果,这有时非常有用。或者你可能想要保证它永远不会被退回。

如果你真的想使用nullity,我会选择最后一个选项。我相信它会让你的代码更清晰。 IMO,当方法完全使用不同的参数表示相同的东西时,实际上只应该使用重载 - 而在一个案例中返回Nullable<T>作为返回类型,而T作为在另一种情况下,返回类型不能真正被看到。

答案 1 :(得分:3)

以下方法适用于类和可空结构:

public static T GetValue<T>(string key)
{
    object o;
    if (Contents.TryGetValue(key, out o))
    {
        if (o is T)
        {
            return (T)o;
        }
    }
    return default(T);
}

用法:

int?   result1 = GetValue<int?>("someInt");
string result2 = GetValue<string>("someString");

注意?如何是泛型类型参数的一部分,而不是由返回类型的方法定义。

答案 2 :(得分:1)

这应该完全符合你的需要。如果请求的类型是可空类型,请在转换之前检查基础类型。

public static T GetValue<T>(string key)
{
    object o;
    if (Contents.TryGetValue(key, out o))
    {
        if (o is T || Nullable.GetUnderlyingType(typeof(T)) == o.GetType())
        {
            return (T)o;
        }
    }

    return default(T);
}

我的测试代码:

Contents.Add("a string", "string value");
Contents.Add("an integer", 1);
Contents.Add("a nullable integer", new Nullable<int>(2));

// Get objects as the type we originally used.
Debug.WriteLine(string.Format("GetValue<string>(\"a string\") = {0}", GetValue<string>("a string")));
Debug.WriteLine(string.Format("GetValue<int>(\"an integer\") = {0}", GetValue<int>("an integer")));
Debug.WriteLine(string.Format("GetValue<int?>(\"a nullable integer\") = {0}", GetValue<int?>("a nullable integer")));

// Get objects as base class object.
Debug.WriteLine(string.Format("GetValue<object>(\"a string\") = {0}", GetValue<object>("a string")));
Debug.WriteLine(string.Format("GetValue<object>(\"an integer\") = {0}", GetValue<object>("an integer")));
Debug.WriteLine(string.Format("GetValue<object>(\"a nullable integer\") = {0}", GetValue<object>("a nullable integer")));

// Get the ints as the other type.
Debug.WriteLine(string.Format("GetValue<int?>(\"an integer\") = {0}", GetValue<int?>("an integer")));
Debug.WriteLine(string.Format("GetValue<int>(\"a nullable integer\") = {0}", GetValue<int>("a nullable integer")));

// Attempt to get as a struct that it's not, should return default value.
Debug.WriteLine(string.Format("GetValue<double>(\"a string\") = {0}", GetValue<double>("a string")));

// Attempt to get as a nullable struct that it's not, or as a class that it's not, should return null.
Debug.WriteLine(string.Format("GetValue<double?>(\"a string\") = {0}", GetValue<double?>("a string")));
Debug.WriteLine(string.Format("GetValue<StringBuilder>(\"a string\") = {0}", GetValue<StringBuilder>("a string")));

结果:

GetValue<string>("a string") = string value
GetValue<int>("an integer") = 1
GetValue<int?>("a nullable integer") = 2

GetValue<object>("a string") = string value
GetValue<object>("an integer") = 1
GetValue<object>("a nullable integer") = 2

GetValue<int?>("an integer") = 1
GetValue<int>("a nullable integer") = 2

GetValue<double>("a string") = 0

GetValue<double?>("a string") = 
GetValue<StringBuilder>("a string") =