Search code examples
c#genericsthis

C# Generics type when using this keyword


The objects I am putting together are to allow an object to expose itself as something that can be transformed by another object of the same type. Think along the lines of merge, but the operations are not generic/straightforward such that you could use reflection to simply get all the properties and perform some predetermined set of operations. I have therefore decided to try and implement it as objects that expose themselves as Transformable and attach to them an object that knows how to perform the transformation (which can be interchanged). The compilable code I have so far is:

public interface ITransformer<TType> where TType : class
{
    void Transform(TType source, TType destination);
}

public interface ITransformable<TType> where TType : class
{
    bool Locked { get; set; }
    ITransformer<TType> Transformer { get; set; }

    void TransformFrom(TType source);
}

public class MyTransformer : ITransformer<MyTransformable>
{
    public void Transform(MyTransformable source, MyTransformable destination)
    {
        // Perform some logic for the transform
    }
}

public class MyTransformable : ITransformable<MyTransformable>
{
    public bool Locked { get; set; }
    public ITransformer<MyTransformable> Transformer { get; set; }

    public string Value1 { get; set; }
    public uint Value2 { get; set; }
    public bool Value3 { get; set; }

    public void TransformFrom(MyTransformable source)
    {
        Transformer.Transform(source, this);
    }
}

As you can see, the type MyTransformable contains some data (Value1 - Value3) as well as a Locked state and the Transformer object will know how to perform the transform for this item (generics ensuring only transformers capable of acting on the MyTransformer type are allowed).

My problem is that I don't want all new objects of type Transformable to have to repeat the call to

public void TransformFrom(MyTransformable source)
{
    Transformer.Transform(source, this);
}

So was hoping I could change MyTransformable to

public abstract class Transformable<TType> : ITransformable<TType> where TType : class
{
    public bool Locked { get; set; }
    public ITransformer<TType> Transformer { get; set; }

    public void TransformFrom(TType source)
    {
        Transformer.Transform(source, this);  // ERROR! this is type Transformable<TType> not TType
    }
}

public class MyTransformable : Transformable<MyTransformable>
{

}

But of course this won't compile due to the error I highlight. I feel like I have missed the point somewhere along the line. Could anyone point me in the right direction on this?


Solution

  • What you need to do is add a hook for the derived type to expose the final, real TType implementation (or simply this). This is best done through an abstract property.

    public abstract class Transformable<TType> : ITransformable<TType>
    {
        public bool Locked { get; set; }
        public ITransformer<TType> Transformer { get; set; }
    
        protected abstract TType Value { get; }
    
        public void TransformFrom(TType source)
        {
            Transformer.Transform(source, Value); 
        }
    }
    
    public class MyOtherTransformable : Transformable<MyTransformable>
    {
        protected override MyTransformable Value
        {
            get { return this; }
        }
    }