So as we discussed in the comments there are 2 solutions:
Generics with constraints
static void Save<T, U>(T sink, U source) where T : IWriter,
U : ISerializable
{
// do stuff using that fact that U is serialisable and T implements IWriter
}
where IWriter would be:
interface IWriter
{
void Save<U>(U itemToSave) where U : ISerializable;
}
To do the specialisation, you would just overload the method. See this example for how that would work.
dyanamic
type
You can use the dynamic
type to allow you to call arbitary methods, but you loose compile time type checking.
static void Save(dynamic a, dynamic b)
{
a.Save(b);
}
I'm not sure how that would work with overloading to provide the specialisation you require.