通用方法 - 寻求设计建议
我有一个 util 类 (C#),其中有一个静态方法,该方法采用某种类型的对象并启动 Web 服务来获取更多数据。我想支持其他对象类型并且不复制代码,即添加多个类似的方法,我认为通用路线是最好的。
例如,假设我有:
public static void GetData(Building building)
{
var webClient = new WebClient();
var wrapper = new WrapperClass(building);
if (building.Distance.HasValue)
{
structure = new Structure((decimal)building.Length.Value, (decimal)building.Height.Value);
}
... // and so on ...
而不是像这样创建另一个方法:
public static void GetDataForBridge(Bridge bridge)
{
var webClient = new WebClient();
var wrapper = new WrapperClass(bridge);
if (bridge.Distance.HasValue)
{
structure = new Structure((decimal)bridge.Length.Value, (decimal)bridge.Height.Value);
}
// ...
我不知道如何使用泛型来做到这一点。有人可以给我一些提示或建议吗?
I have a util class (C#) where I have a static method that takes a certain type of object and brings up a web service to get further data. I would like to support other object types and to not replicate the code, i.e. add multiple similar methods, I am thinking the Generic route would be best.
For example, let's say I have:
public static void GetData(Building building)
{
var webClient = new WebClient();
var wrapper = new WrapperClass(building);
if (building.Distance.HasValue)
{
structure = new Structure((decimal)building.Length.Value, (decimal)building.Height.Value);
}
... // and so on ...
instead of creating another method(s) like so:
public static void GetDataForBridge(Bridge bridge)
{
var webClient = new WebClient();
var wrapper = new WrapperClass(bridge);
if (bridge.Distance.HasValue)
{
structure = new Structure((decimal)bridge.Length.Value, (decimal)bridge.Height.Value);
}
// ...
I am not sure how to do this using Generics. Can anyone please give me some tips or advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,Bridge 和 Building 必须实现相同的接口,例如 IObjectWithHeightAndWidth。然后,您可以将此接口指定为对泛型方法的类型参数的约束。
(或者,这些类可以共享一个公共基类,而不是公共接口。)
不过,正如其他发帖者所指出的,您可能根本不需要泛型。仅当您随后需要将对象作为
Bridge
或Building
进行强类型引用时,才需要泛型——例如,如果您需要调用另一个泛型方法在我们正在讨论的方法中。In this case, Bridge and Building would have to implement the same interface, say, IObjectWithHeightAndWidth. You'd then specify this interface as a constraint on the type parameter of your generic method.
(Or, instead of a common interface, the classes could share a common base class.)
As other posters have pointed out, though, you might not need generics at all. You only would need generics if you subsequently need to have a strongly-typed reference to the object as a
Bridge
orBuilding
-- for example if you need to call another generic method in the method we're discussing.在您的情况下,为什么不直接用更简单的
IHaveDistanceLengthAndHeight
接口替换函数声明中的Building
呢?这样你就根本不需要泛型。In your case, why not just replace
Building
in function declaration with a simplerIHaveDistanceLengthAndHeight
interface? This way you don't need generics at all.听起来在这种情况下您可能应该使用共享接口而不是泛型。定义一个接口,其中包含距离属性和长度、高度等内容;并让您的桥和 Building 实现它,然后定义一个接受共享接口实例的 GetData() 方法。
Sounds like you should probably be using a shared interface rather than generics in this case. Define an Interface which contains things like the Distance property and Length, Height, etc; and have your bridge and Building implement it, then define a GetData() method that takes in an instance of the shared interface.