I have a Wrapper<T> struct type. I'd like to write an implicit operator, which can convert between two Wrapper<T> instances, in which the generic type T is different, for each instance:
public struct A
{
public int value;
}
public struct B : IConvertable<A>
{
public float value;
public void From(A source)
{
value = (float)source.value;
}
public A To()
{
return new A
{
value = (int)value
};
}
}
public class Wrapper<T>
{
public T target;
// Does not compile:
public implicit operator Wrapper<Q> <Q>(Wrapper<T> source) where Q : IConvertable<T>
{
Wrapper<Q> dest = new();
dest.From(source);
return dest;
}
}
public interface IConvertable<T>
{
void From(T source);
T To();
}
As you can see, the implicit operator above won't compile. I'm guessing C# just doesn't allow for implicit operators with extra generic type parameters.
Is there any way to achieve what I'm trying to do? Thanks for any help or advice.