I have a nongeneric interface to a generic class group. I have a method which uses covariance to return a strongly typed instance in a class derived from an abstract implementation of the non-generic class group.
So basically with this setup, I have a number of classes, e.g. ActualThing1
and ActualThing2
. These classes have a Clone
method which returns a strongly-typed clone of themselves.
Below is how I've structured the interface & classes. I am not sure this is right.
Normally, I wouldn't have the non-generic abstract class at all. But this is necessary because there is a situation where I have to do something to child objects, referencing an internal method _SomethingNeededInternally
. This can't be done using the interface, since it's protected, nor can it be done from inside the generic class, since the children might not be the same type (they might be a different type der开发者_开发技巧ived from the interface). Hence the non-generic base class Something
.
This works, but what doesn't quite make sense to me is the explicit implementation of ISomething.Clone
that is needed in my abstract Something
class. It has to be there, but it shouldn't be implemented, since I want that implementation deferred to the generic class that derives from it. But there's no such syntax as abstract ISomething ISomething.Clone()
.
But that code (where the exception is throw) can't ever execute, can it, since I don't have any non-generic implementations of this object?
I guess I'm wondering if there is a better way to do this, because it seems not right. That is, I am depending on the fact that a non-generic class is never created from the interface, which smells funny.
public interface ISomething
{
// ...
ISomething Clone();
}
public abstract class Something: ISomething
{
ISomething ISomething.Clone()
{
throw new Exception("This should not be happening");
}
protected int _SomethingNeededInternally;
}
public abstract class Something<T>: Something, ISomething where T: ISomething, new()
{
public abstract T Clone();
ISomething ISomething.Clone()
{
return Clone();
}
}
public interface IActualThing {} // this could be any interface
public class ActualThing: Something<ActualThing>, IActualThing
{
public override ActualThing Clone()
{
return new ActualThing(); // do cloning here
}
}
Would adding required method as implementation of Clone work for you? (I'm still not exactly sure what your whole problem is, this will just solve syntax for abstract explicit interface implementation.)
public abstract class Something: ISomething
{
ISomething ISomething.Clone()
{
return CloneYouMust();
}
abstract ISomething CloneYouMust();
}
I think this might work better:
public interface ISomething
{
ISomething Clone();
}
public abstract class Something : ISomething
{
protected abstract ISomething CloneInternal();
ISomething ISomething.Clone()
{
return CloneInternal();
}
}
public abstract class Something<T>: Something, ISomething where T: ISomething, new()
{
protected override ISomething CloneInternal()
{
return Clone();
}
public abstract T Clone();
}
public class ActualThing: Something<ActualThing>, IActualThing
{
public override ActualThing Clone()
{
}
}
Your complaint about abstract ISomething ISomething.Clone
is valid but there's nothing about interfaces that states you must explicitly declare them. The code below shows a working example and removes the requirement of the generic class.
interface Igloo
{
Igloo Clone();
}
abstract class Alpha : Igloo
{
public abstract Igloo Clone();
}
class Bravo : Alpha
{
public override Igloo Clone()
{
/* implement */
return null;
}
}
First of all, I think that having two Clone() methods inside Something<T> is redundant. Since you've already constrained T to both being an ISomething and new(), why not do this:
public abstract class Something<T>: Something, ISomething
where T: ISomething, new()
{
public override ISomething Clone()
{
return new T().Clone();
//alternatively may have to use Activator.CreateInstance() here
}
}
Secondly I'm not entirely clear on whether _SomethingNeededInternally needs to be abstract or virtual, but it isnt marked either. My interpretation is that it should be virtual, but maybe you'll need to tweak that. Anyway here is the overall scheme that seems to work well for me and make the most sense. Note that I was able to make the Clone() method abstract in the abstract class 'Something':
public interface IActualThing
{
}
public interface ISomething
{
ISomething Clone();
}
public abstract class Something: ISomething
{
public abstract ISomething Clone();
protected virtual void _SomethingNeededInternally()
{
throw new NotImplementedException();
}
}
public abstract class Something<T>: Something, ISomething where T: ISomething, new()
{
public override ISomething Clone()
{
return new T().Clone();
}
}
public class ActualThing: Something<ActualThing>, IActualThing
{
public override ISomething Clone()
{
throw new NotImplementedException();
}
}
Also note that you need to mark the return type as ISomething in the final 'ActualThing' class. Of course you would be returning an 'ActualThing' in reality, but the method needs to conform to the interface nevertheless.
I would suggest that you define and implement interfaces ISelf<T>, containing a single Self property of type T, and ICloneable<out T>, deriving from ISelf<T>, with a method Clone() of type T. Then a class which implements ICloneable<ItsOwnType> can implement it with a method returning ItsOwnType; that method should call then a virtual InternalClone method to do the actual work (otherwise, calling Clone on a variable of base type might end up calling the base-type Clone method rather than a derived one).
精彩评论