使用.NET泛型可变参数改进方法 [英] Improving methods with variable parameters using .NET Generics

查看:143
本文介绍了使用.NET泛型可变参数改进方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多这是目前超负荷运作的 INT 字符串功能:

I have a lot of functions which are currently overloaded to operate on int and string:

bool foo(int);
bool foo(string);
bool bar(int);
bool bar(string);
void baz(int p);
void baz(string p);

然后我有很多的功能以1,2,3或4任 INT 字符串,它调用上述功能:

I then have a lot of functions taking 1, 2, 3, or 4 arguments of either int or string, which call the aforementioned functions:

void g(int p1)    { if(foo(p1)) baz(p1); }
void g(string p1) { if(foo(p1)) baz(p1); }

void g(int    p2, int    p2) { if(foo(p1)) baz(p1); if(bar(p2)) baz(p2); }
void g(int    p2, string p2) { if(foo(p1)) baz(p1); if(bar(p2)) baz(p2); }
void g(string p2, int    p2) { if(foo(p1)) baz(p1); if(bar(p2)) baz(p2); }
void g(string p2, string p2) { if(foo(p1)) baz(p1); if(bar(p2)) baz(p2); }

// etc.

注意:在 G()系列的实施只是一个例子

Note: The implementation of the g() family is just an example

更多种类比目前的 INT 字符串可能会随时出台。这同样适用于与更多的参数比4的相同功能的当前数量几乎没有管理的职能。添加在任何尺寸和combinatoric爆炸多一个变种会如此之大,它可能会吹走应用。

More types than the current int or string might be introduced at any time. The same goes for functions with more arguments than 4. The current number of identical functions is barely manageable. Add one more variant in either dimension and the combinatoric explosion will be so huge, it might blow away the application.

在C ++中,我想模板化 G()和完成。

In C++, I'd templatize g() and be done.

据我所知,.NET泛型是不同的。我一直在与他们战斗了两个小时,现在试图拿出不涉及复制和粘贴code,但没有成功的解决方案。

I understand that .NET generics are different. I have been fighting them for two hours now trying to come up with a solution that doesn't involve copy and pasting code, to no avail.

C#泛型不会要求我输入了相同的code为家庭的功能或者服用三类五个参数?

C# generics won't require me to type out identical code for a family of functions taking five arguments of either of three types?

我在想什么?

修改 这些功能用于解析了一堆参数(目前为 INT 或<$ C $一些源代码,>字符串)。想象一下,酒吧()巴兹()能够同时读取 INT 字符串 G()系列指定参数的类型和数量来解析(隐式,通过他们的论点'类型)。

These functions are used to parse a bunch of arguments (currently either int or string) from some source. Imagine bar() and baz() being able to read both int or string, and the g() family specifying the type and number of arguments to parse (implicitly, by their arguments' types).

推荐答案

不作为最佳的,因为我想......但如果​​酒吧巴兹有仿制药呢?

Not as optimal as I would like... but what if foo, bar, and baz had generic versions as well?

static bool foo(int input)
{
    return input > 5;
}
static bool foo(string input)
{
    return input.Length > 5;
}
static void baz(int input)
{
    Console.WriteLine(input);
}
static void baz(string input)
{
    Console.WriteLine(input);
}
static bool foo<T>(T input)
{
    if (input is int) return foo((int)(object)input);
    if (input is string) return foo((string)(object)input);
    return false;
}
static void baz<T>(T input)
{
    if (input is int) baz((int)(object)input);
    else if (input is string) baz((string)(object)input);
    else throw new NotImplementedException();
}
static void g<T>(T input)
{
    if (foo(input))
        baz(input);
}
static void g<T, U>(T input, U inputU)
{
    g(input);
    g(inputU);
}

这篇关于使用.NET泛型可变参数改进方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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