If you do not want to (or cannot) use dynamic types or generate code at run-time, there is another approach I borrowed from the EqualityComparer
implementation. It is a little lengthy, but another possibility utilizing "classic" .NET tools.
First, you define a generic interface which acts like a "trait", defining the addition methods you want to have:
/// <summary>
/// Represents an interface defining the addition calculation for generic types.
/// </summary>
public interface IAddition<T>
{
/// <summary>
/// Calculates the addition result of <paramref name="left"/> and <paramref name="right"/>.
/// </summary>
/// <param name="left">The left operand.</param>
/// <param name="right">The right operand.</param>
/// <returns>The calculation result.</returns>
T Add(T left, T right);
}
Now, you implement this trait for the types you are interested in, in this example I do it for float
:
internal class SingleAddition : IAddition<Single>
{
Single IAddition<Single>.Add(Single left, Single right) => left + right;
}
Then you create a generic static class which will be the one you'll actually interact with. It automatically creates and caches an implementation of the IAddition<T>
interface fitting its generic type:
/// <summary>
/// Represents an implementation of addition calculations for generic types.
/// </summary>
/// <typeparam name="T">The type to support the addition calculation.</typeparam>
public static class Addition<T>
{
private static readonly IAddition<T> _implementation = Implement();
/// <summary>
/// Calculates the addition result of <paramref name="left"/> and <paramref name="right"/>.
/// </summary>
/// <param name="left">The left operand.</param>
/// <param name="right">The right operand.</param>
/// <returns>The calculation result.</returns>
public static T Add(T left, T right) => _implementation.Add(left, right);
private static IAddition<T> Implement()
{
Type type = typeof(T);
// If T implements IAddition<T> return a GenericAddition<T>.
if (typeof(IAddition<T>).IsAssignableFrom(type))
return (IAddition<T>)Activator.CreateInstance(typeof(GenericAddition<>).MakeGenericType(type));
// Otherwise use a default implementation for primitive types.
switch (type.IsEnum ? Type.GetTypeCode(Enum.GetUnderlyingType(type)) : Type.GetTypeCode(type))
{
case TypeCode.Single:
return (IAddition<T>)new SingleAddition();
default:
throw new NotSupportedException($"Type {type.Name} does not support addition.");
}
}
}
internal sealed class GenericAddition<T> : IAddition<T> where T : IAddition<T>
{
T IAddition<T>.Add(T left, T right) => left.Add(left, right);
}
Of course, you'd extend the switch on type codes to support all types you're interested in. Those would be all primitive types and those you cannot modify, since you can simply implement the IAddition<T>
interface for types you can modify - the GenericAddition
implementation will take care of using it then.
Note that Enum
values are already supported due to the check of the underlying type I added.
As you can see in the static Addition<T>
class, it exposes an Add
method which you'll eventually call from outside like this:
public class SomeStuff<T>
{
public T TestAddition(T left, T right) => Addition<T>.Add(left, right);
}
// Testing in C# interactive:
> SomeStuff<float> floatStuff = new SomeStuff<float>();
> floatStuff.TestAddition(12, 24.34f)
36.34
School math has never been so easy! Maybe. Not really. It's classified.