AFAIK currently there is no way to restrict extension method to accept only int
, short
or long
. If you are ok with any type being passed to the method then you can just use generics:
internal static void BasicLog<T>(
this T logData)
=> CreateSystemLog(Type.BasicLog, logData.ToString());
Possibly constraining it to struct
or for .NET 7 one of the generic math types/interfaces if you want to reduce the number of possibilities.
If you are ok with moving from extension methods - then you can use OneOf
:
public static class Helpers
{
public static void BasicLog(OneOf<int, short, long> logData)
=> Console.WriteLine(logData.Match(i => i.ToString(), i => i.ToString(), i => i.ToString()))
}
Which uses implicit conversions for such calls (you can skip Helpers
by adding using static Helpers;
):
Helpers.BasicLog(1L);
Helpers.BasicLog(1);
Helpers.BasicLog((short)1);
Or leverage the existing conversions from int
and short
to long
and remove all methods except the long
(though it will accept everything implicitly convertible to long
, also you can keep this
, but it will work as extension method only for long
's, everything else will require explicit cast):
internal static void BasicLog(long logData)
=> CreateSystemLog(Type.BasicLog, logData.ToString();