簡體   English   中英

返回不同類型時是否建議使用多重方法?

[英]Are mutiple methods recommended when returning different types?

我正在從Entity對象返回值。 其中有些是String類型的,有些則不是。 現在,我做了如下快速解決方案。

private String GetStringValue(Entity entity, String attribute, String substitute = "")
{
  if(entity.Contains(attribute)
    return entity[attribute] as String;
  return substitute;
}

private String GetIntValue(Entity entity, String attribute, int substitute = 0)
{
  if(entity.Contains(attribute)
    return entity[attribute] as int;
  return substitute;
}

然后我想起了通用類型的語法(類似<TypeX> )。 但是,我的問題是,是否有必要開始更改現有代碼。 我需要在兩個地方(返回類型和替換類型)更改方法的簽名,但是我擔心我也需要在方法內部進行一些復雜的編碼。

另一方面,我有一種很好的方式來處理所有可能的類型(而且我有一種直覺,我們將使用更多的字符串和整數。

您將必須在三個位置更改方法的簽名,因為還必須添加通用參數:

private T GetValue<T>(Entity entity, String attribute, T substitute)

該方法中,並不需要任何復雜的編碼。 T分別替換當前出現的stringint (請注意,僅當將T限制為引用類型時才可以應用as運算符-您可能不想這樣做,因為int是值類型)。

請注意,此方法有兩個問題,您可能會考慮缺點:

  • 這種通用方法將支持“所有可能的類型”,但也將支持所有不可能的類型(用戶可以自由指定T任何類型,並且沒有辦法在仍然支持stringint情況下限制T
  • 您不能為每種類型指定任意的默認替換值。 您可以做的是為substitute聲明一個默認值,即default(T) ,但至少為string聲明一個默認值,它不是一個空字符串,而是null

如果您不想更改方法簽名,則可以編寫一個泛型函數並從所有這些非泛型版本中調用它。

private String GetStringValue(...){
    return GetValue<String>(...);
}

順便說一句,您正在尋找通用方法

對於例如(來自msdn)

static void Swap<T>(ref T lhs, ref T rhs)
{
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;
}

...

Swap<int>(ref a, ref b);

要不就

Swap(ref a, ref b); //type int is infered based on type of arguements and method signature

您是對的,“類似”是通用方法。 在此處查看通用方法。 下一種方法看起來很適合您的目的。

   private  static T GetValue<T>(Entity entity, string attribute, T defaultValue)
        {
            if (!entity.Contains(attribute))
                return defaultValue;

            return  (T)entity[attribute];
        }

編輯:根據w0lf的評論進行更新。

Entity是什么類? 假設它是一個自定義類,使其也具有通用性,那么它將起作用:

private T Get<T>(Entity<T> entity, T attribute, T substitute = default(T))
{
    if (entity.Contains(attribute))
        return entity[attribute];
    return substitute;
}

您可以通過以下方式檢索值:

var entity = new Entity<string>();
string val = Get<string>(entity, "attr", "subst");

您應該定義Entity<T>類:

public class Entity<T>
{
    // TODO: implement
    public T this[string i] { get; set; }

    // TODO: implement
    internal bool Contains(string attribute)
    {
        return true;
    }
    // TODO: implement
    // other properties and methods       
}

您可以使用通用方法:

private T GetStringValue<T>(Entity<T> entity, String attribute, T substitute = default(T))
{
    if (entity.Contains(attribute))
        return entity[attribute];
    return substitute;
}

如果有可能在一個方法中推廣代碼,我絕對建議以通用方式使用它。 它使類更小,更易讀,並且如果需求發生變化,則只需更改一種方法。 您的方法看起來可以輕松實現泛型。

private T GetIntValue<T>(Entity entity, String attribute, T substitute = default(T))
{
    if(entity.Contains(attribute))
        return (T)entity[attribute];
    return substitute;
}

如果還有更多的邏輯要執行,那么您還可以使用具有不同類型函數的字典:

private IDictionary<Type, Func<Entity, string, object>> actions;

private void InitActions()
{
    actions = new Dictionary<Type, Func<Entity, string, object>>
        {
            {
                typeof (string), (entity, attribute) =>
                    {
                        // this could be your custom code for string
                        return entity[attribute];
                    }
            },
            {
                typeof (int), (entity, attribute) =>
                    {
                        // this could be your custom code for int
                        return entity[attribute];
                    }
            }
        };
}

private T GetIntValue<T>(Entity entity, String attribute, T substitute = default(T))
{
    if (entity.Contains(attribute) && actions.ContainsKey(typeof (T)))
    {
        Func<Entity, string, object> action = actions[typeof (T)];
        return (T)action(entity, attribute);
    }
    return substitute;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM