I have a generic class CGeometryCalibration2D<T>
which can be used with numeric types like int
, double
, float
, and my custom type CLocation
.
(less important) question 1: how can I restrict T to one of these types?
Inside this class, there is a function
double InterpolateReverse (T i_Value);
which should be used only with CLocation
as T
.
I don't know any way to apply such a restriction.
I already tried extension methods, which would do exactly this restriction
double InterpolateReverse (this CGeometricCalibration2D<CLocation>, CLocation i_Value);
but they don't allow access to private members. I could work around this limitation by using Reflection, but that's not the nicest way.
What can I do here?
Should I maybe find a completely different approach?
The only remaining idea I have is overloading the generic class by concrete implmentations and adding the function there, like
CGeometricCalibration2D_CLocation : CGeometricCalibration2D<CLocation>
{
double InterpolateReverse (CLocation i_Value);
}
but then I need to have an object of the concrete type CGeometricCalibration2D_CLocation
in order to execute InterpolateReverse ()
Thank you all for your answers and comments!
After thinking about them, I decided to mix generics with inheritance, which hopefully is the cleanest solution to my problem.
The abstract generic base class contains all that stuff that applies to all usable types of T, but it isn't instantiable. The concrete implementations ensure that only those T are usable that make sense to be used.
public abstract class CGeometryCalibration2D<T> : CGeometryCalibration<T>
{
ctor()
public override void Clear ()
public bool Add (double i_dPosition, CGeometryCalibration1D<T> i_oGeoCalib1D)
public bool Remove (double i_dPosition)
public override void CopyTo (ref CGeometryCalibration<T> io_oGeoCalib)
public override T Interpolate (CLocation i_locPosition, bool i_bExtrapolate = false)
public T Interpolate (double i_dPosition, double i_dPositionGC1D, bool i_bExtrapolate = false)
public override CResulT Load (CXmlDocReader i_xmldocreader)
public override CResulT Save (CXmlDocWriter i_xmldocwriter)
}
public class CGeometryCalibration2D_Double : CGeometryCalibration2D<double>
{
ctor()
}
public class CGeometryCalibration2D_CLocation : CGeometryCalibration2D<CLocation>
{
ctor()
public double InterpolateReverse (CLocation i_locValue, out CLocation o_ValueInterpolated, bool i_bExtrapolate = false)
}