-3

我有三种方法。将它们组合成一个的最佳方法是什么。

public App Get(customobj data, string x)
{
    return null;
}

public Con Get(customobj data, string x)
{
    return null;
}

public Env Get(customobj data, string x)
{
    return null;
}
4

3 回答 3

1

您的问题的简单答案是没有最好的方法。除非所有三个中的代码都是相同的(不太可能因为它们返回三种不同的类型),否则将它们保留为三种方法,因为任何将它们组合成一个的尝试都会使代码更混乱,更难阅读,因此更难维护。此外,给他们更好的名字,描述每个人的工作。至少,类似于:

public App GetApp(customobj data, string x)
{
    ...
}

public Con GetCon(customobj data, string x)
{
    ...
}

public Env GetEnv(customobj data, string x)
{
    ...
}
于 2013-11-04T21:38:19.267 回答
1

这可能是一个不错的小通用:

public static TOutput Get<TOutput>(this customobj data, string x)

请记住,在其背后实现一个使用工厂模式的类可能是最好的实现,但这完全取决于您。您可以轻松地打开类型名称并构建正确的类型。

使用它看起来像这样:

myCustomObj.Get<Con>("my x val");
于 2013-11-04T21:24:29.437 回答
0

据我了解你的问题。你可以创建一个类

Public parameter
{
   App app;
   Con con;
   Env env;
}
public parameter Get(customobj data, string x)
{
    parameter _pobj=new parameter();
    //you can add the result here if you want or else return null.
    _pobj="somehting1";
    _pobj="somthing2";
    _pobj="somthing3";

    return _pobj;
}

您可以通过说 _pobj.env 或提到的参数在不同的调用中收集它们

于 2013-11-04T21:42:51.797 回答