Reputation: 447
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?
Upvotes: 2
Views: 3852
Reputation: 18965
It'll result in more casting than you are probably comfortable with and may even defeat the purpose of what you're trying to do, but this compiles:
public interface ITransformable { }
public interface ITransformer<TType>
where TType: ITransformable
{
void Transform(ITransformable source, ITransformable destination);
}
public interface ITransformable<TType> : ITransformable
where TType: ITransformable
{
bool Locked { get; set; }
ITransformer<TType> Transformer { get; set; }
void TransformFrom(TType source);
}
public class MyTransformer : ITransformer<MyTransformable>
{
public void Transform(ITransformable source, ITransformable destination)
{
if (source.GetType() != destination.GetType())
throw new InvalidOperationException();
// Perform some logic for the transform
}
}
public abstract class Transformable<TType> : ITransformable<TType> where TType: ITransformable
{
public bool Locked { get; set; }
public ITransformer<TType> Transformer { get; set; }
public void TransformFrom(TType source)
{
Transformer.Transform(source, this);
}
}
public class MyTransformable : Transformable<MyTransformable>
{
}
Upvotes: 0
Reputation: 71563
If you're using .NET 4, you can use a cast to dynamic
:
public void TransformFrom(TType source)
{
Transformer.Transform(source, (dynamic)this);
}
This allows the CLR to do the parameter type-matching at runtime based on the real type (closed generic) of the object at the time of execution, preventing the compile-time error. At runtime, the CLR can easily tell that source
and this
are the same type and will perform the call.
The upside is it solves your problem. The downside is that use of dynamic
is an inherently reflective process, which is first slower and second can hide bugs that will cause runtime exceptions.
Upvotes: 1
Reputation: 754665
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; }
}
}
Upvotes: 1