泛型方法返回简单类型 [英] Generic methods returning simple types

查看:103
本文介绍了泛型方法返回简单类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,
我有一些我不太喜欢的代码。鉴于我没有写出它,只是继承了它,我发现我可以清理它一点。基本上,我们有一个用NHibernate持久化的设置对象。我不会让你知道实现的细节,只是说它包含一个字典,它具有我们保存在数据库中的实际值。



我们典型的属性实现看起来像这样:

  public string MyDumbProperty 
{
get {return GetStringValue(MyDumbProperty,string .Empty); }
set {SetValue(MyDumbProperty,value);}
}

现在,从上面的实现中可以明显看出,对于我们返回的每个类型(字符串,整数,长整数,浮点数等),我们都有单独的Getxxxx方法,第二个参数是默认值。我想要的是能够做到如下所示:

  public string MyDumbProperty 
{
得到{返回GetValue(MyDumbProperty,string.Empty); }
set {SetValue(MyDumbProperty,value);}
}

我有几个想要这样做的理由。最大的问题是,我个人不喜欢根据他们所采用的参数类型命名的方法。第二个是我想为这个垃圾做一个resharper模板。然后,我可以填写该属性的名称和类型并完成(假定resharper可以为该类型提供合理的默认值)。



我曾想过制作一个返回T的泛型方法GetValue,但我不确定我需要如何设置泛型约束,因此这是可行的。从这返回的类型将全部是基本类型(字符串,整数等)。有几个返回数组,但我总是可以在这些数组上做些不同的事情。无论如何,这不是一个高度优先的问题,但每当我看到它时都会让我很烦恼。我只是觉得应该有更好的方法来做到这一点。



我想第一步就是重命名各种方法。因为它们在第二个参数的默认值类型上有所不同,所以我至少可以将它们强制为相同的名称。但我能做得比这更好吗?如果我可以摆脱每种方法中重复代码的多个位,这些代码在所使用的数据类型(所有数字类型中的TryParse)方面都不相同,那就太好了。

default(T)来获取某个类型的默认值。

$ b $ =h2_lin>解决方案

  public T Get< T>(string propertyName)
{
object obj;
if(propertyBag.TryGetValue(propertyName,out obj)){
return(T)obj;

if(typeof(T)== typeof(string)){
return String.Empty;
}
return default(T);






UPDATE

作为一种替代方案,您可以有两个重载,一个接受一个显式的默认值。

  public T Get< T>(string propertyName)
{
return Get< T>(propertyName,default(T));
}

public T Get< T>(string propertyName,T defaultValue)
{
object obj;
if(propertyBag.TryGetValue(propertyName,out obj)){
return(T)obj;
}
return defaultValue;
}


Ok, I've got a bit of code that I don't like very much. Given that I didn't write it and merely inherited it, it occurs to me that I could clean it up a little. Basically, we have an object for settings that is persisted with NHibernate. I won't bore you with the implementation details except to say that it contains a dictionary that has the actual values we're keeping in the database.

Our typical property implementation looks like this:

public string MyDumbProperty
{
get { return GetStringValue("MyDumbProperty", string.Empty); }
set { SetValue("MyDumbProperty", value);}
}

Now, it's probably obvious from the implementation above that we have separate Getxxxx methods for each of the types we return (strings, ints, longs, floats, etc.), with the second parameter being a default value. What I want is to be able to do something like the following:

public string MyDumbProperty
{
get { return GetValue("MyDumbProperty", string.Empty); }
set { SetValue("MyDumbProperty", value);}
}

I have a couple of reasons for wanting to do this. The big one is that I have a personal dislike of methods named based on what types of parameters they take. The second is that I'd like to make a resharper template for this crap. Then I could just fill in the name of the property and the type and be done (presuming that resharper could give a reasonable default value for the type).

I had thought of making a generic method GetValue that returned a T, but I'm not sure how I need to set up the generic constraints so this is doable. The types being returned from this will all be basic types (strings, ints, etc). There are a couple that return arrays, but I could always do something different on those. This isn't a high priority issue, by any means, but it annoys me every time I see it. I just think there should be a better way to do this.

I suppose a first step would be to just rename the various methods. Since they vary on what type is being passed for the default in the second parameter, I could at least coerce them to the same name. But I could I do better than that? It would be great if I could get rid of the multiple bits of repetitive code in each method that are only different in terms of the datatype being used (TryParse in all the numeric ones).

解决方案

You can use default(T) to get the default value of a type.

public T Get<T>(string propertyName)
{
    object obj;
    if (propertyBag.TryGetValue(propertyName, out obj)) {
        return (T)obj;
    }
    if(typeof(T) == typeof(string)) {
        return String.Empty;
    }
    return default(T);
}


UPDATE

As an alternative you could have two overloads, one accepting an explicit default value.

public T Get<T>(string propertyName)
{
    return Get<T>(propertyName, default(T));
}

public T Get<T>(string propertyName, T defaultValue)
{
    object obj;
    if (propertyBag.TryGetValue(propertyName, out obj)) {
        return (T)obj;
    }
    return defaultValue;
}

这篇关于泛型方法返回简单类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆