I have a method that takes in an interface. I wrote out a custom function for each primitive type (and string). Here's what it looks like:
我有一个接受接口的方法。我为每个基本类型(和字符串)写了一个自定义函数。这是它的样子:
public interface IFoo
{
void DoSomething();
}
static void IntDoSomething(int value)
{
// do something with an int, as if it implemented IFoo
}
static void FloatDoSomething(float value)
{
// do something with a float, as if it implemented IFoo
}
// ... I have an XDoSomething() for all the other primitives and string
public void Execute(IFoo value)
{
value.DoSomething();
}
public void Execute(int value)
{
IntDoSomething();
}
public void Execute(float value)
{
FloatDoSomething();
}
// ... I have an Execute() for all the other primitives and string
While tedious, having an Execute() for each primitive type is workable. The problem is when I have to add stuff like these:
虽然乏味,但每个基元类型都有一个Execute()是可行的。问题是我必须添加这样的东西:
public void Execute(List<IFoo> values)
{
foreach (IFoo foo in values)
{
values.DoSomething();
}
}
public void Execute(Dictionary<IFoo, IFoo> values)
{
foreach (var pair in values)
{
pair.Key.DoSomething();
pair.Value.DoSomething();
}
}
Do I have to write out an Execute() function every time I have a new collection I want to process? For the dictionary one, I'd have to explicitly define versions for every combination of every primitive!
每次有一个我想要处理的新集合时,我是否必须编写一个Execute()函数?对于字典一,我必须为每个原语的每个组合明确定义版本!
I feel like there's a solution involving writing an adapter class that wraps up primitives to be IFoos, but I can't seem to do it without breaking the method signature of Execute(). I don't want the user to have to create an adapter first, I want it to happen implicitly. Is there any way to do that?
我觉得有一个解决方案涉及编写一个将原语包装成IFoos的适配器类,但是如果不破坏Execute()的方法签名,我似乎无法做到这一点。我不希望用户必须首先创建一个适配器,我希望它隐式发生。有没有办法做到这一点?
Thanks!
2 个解决方案
#1
0
I might be misunderstanding, but could you do something like this? Hopefully this isn't what you're trying to avoid... You'd have to write an adapter for each primitive, but at least you'd be able to do your collection executes
我可能会误解,但你可以这样做吗?希望这不是你想要避免的......你必须为每个原语编写一个适配器,但至少你能够执行你的集合执行
public inteface IFoo
{
void DoSomething();
}
public IntFoo : IFoo
{
private int _value;
public IntFoo(int value)
{
_value = value;
}
void DoSomething()
{
IntDoSomething(_value);
}
}
#2
0
Two options come to mind;
想到两个选择;
One would be having DoSomething take an object so that any value would work, and you could use the "is" operator on the parameter to determine the type. This is obviously not very typesafe.
一个人可能会让DoSomething获取一个对象,以便任何值都可以工作,并且您可以在参数上使用“is”运算符来确定类型。这显然不是非常类型安全的。
Also, as you mentioned, you could make DoSomething a generic function. Then, in the implementation you could use:
另外,正如您所提到的,您可以将DoSomething作为通用函数。然后,在实现中您可以使用:
public void Execute<T>( T value)
{
if( value is int )
{
IntDoSomething();
}
else if( value is float)
{
FloatDoSomething();
}
}
etc.
Again, not very typesafe, so you might want to validate the input at the top of the method.
同样,不是非常类型安全,因此您可能希望验证方法顶部的输入。
#1
0
I might be misunderstanding, but could you do something like this? Hopefully this isn't what you're trying to avoid... You'd have to write an adapter for each primitive, but at least you'd be able to do your collection executes
我可能会误解,但你可以这样做吗?希望这不是你想要避免的......你必须为每个原语编写一个适配器,但至少你能够执行你的集合执行
public inteface IFoo
{
void DoSomething();
}
public IntFoo : IFoo
{
private int _value;
public IntFoo(int value)
{
_value = value;
}
void DoSomething()
{
IntDoSomething(_value);
}
}
#2
0
Two options come to mind;
想到两个选择;
One would be having DoSomething take an object so that any value would work, and you could use the "is" operator on the parameter to determine the type. This is obviously not very typesafe.
一个人可能会让DoSomething获取一个对象,以便任何值都可以工作,并且您可以在参数上使用“is”运算符来确定类型。这显然不是非常类型安全的。
Also, as you mentioned, you could make DoSomething a generic function. Then, in the implementation you could use:
另外,正如您所提到的,您可以将DoSomething作为通用函数。然后,在实现中您可以使用:
public void Execute<T>( T value)
{
if( value is int )
{
IntDoSomething();
}
else if( value is float)
{
FloatDoSomething();
}
}
etc.
Again, not very typesafe, so you might want to validate the input at the top of the method.
同样,不是非常类型安全,因此您可能希望验证方法顶部的输入。