Generic and Inheritance of "Type"

A

Aquila Deus

Hi all!

I just thought of that the "Type" should be made as a generic class:
Type<T>, and a typeof(DerivedClass) should inherit typeof(BaseClass)
and typeof(SomeInterface), so that you may do this:

interface CarFactory {
Type<ICar> GetCarType();
}


and enforce this method to return a type that implements ICar :)
 
A

Anders Norås [MCAD]

I just thought of that the "Type" should be made as a generic class:
Type<T>, and a typeof(DerivedClass) should inherit typeof(BaseClass)
and typeof(SomeInterface), so that you may do this:

interface CarFactory {
Type<ICar> GetCarType();
}


and enforce this method to return a type that implements ICar :)

You can create this behavior today without generics. Just make the
GetCarType method on your ICarFactory interface return an object that
implements the ICar interface.

public interface ICar {
void Drive();
}
public interface ICarFactory {
ICar GetCar();
}
public class MyCar : ICar {
public void Drive() {
Console.WriteLine("Wroom!");
}
}
public class MyCarFactory : ICarFactory {
public ICar GetCar() {
return new MyCar();
}
}

An example of how to use this code:
MyCarFactory factory=new MyCarFactory();
ICar car=factory.GetCar();
car.Drive();

If you want the "car" instance to be a MyCar just change the second line to
this:
MyCar car=factory.GetCar() as MyCar;

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
M

Mattias Sjögren

Type<T>, and a typeof(DerivedClass) should inherit typeof(BaseClass)
and typeof(SomeInterface),

That would require multiple inheritance, which isn't supported by
..NET.

interface CarFactory {
Type<ICar> GetCarType();
}


and enforce this method to return a type that implements ICar :)

In a situation like this you're not allowed to return an instance of
Type<SomethingImplementingICar> anyway, so that's another reason this
wouldn't work.



Mattias
 
A

Aquila Deus

Mattias said:
That would require multiple inheritance, which isn't supported by
.NET.

Hmmmm... I forgot that.
In a situation like this you're not allowed to return an instance of
Type<SomethingImplementingICar> anyway, so that's another reason this
wouldn't work.

But it could just return typeof(MyCar);
That's exactly what I want it to do :)
 
A

Anders Norås [MCAD]

That would require multiple inheritance, which isn't supported by
.NET.

This is true if BaseClass and SomeInterface both are classes, but I think
SomeInterface is an interface, hence the name. If this is the case
DerivedClass can inherit BaseClass and implement SomeInterface and be of
both types.

public interface ISomeInterface {
}
public class SomeClass {
}
public class DerivedClass : SomeClass, ISomeInterface {
}

public class MyClass
{
public static void Main()
{
DerivedClass dc=new DerivedClass();
Console.WriteLine(dc is ISomeInterface); // <- Prints true
Console.WriteLine(dc is SomeClass); // <- Prints true
}
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
M

Mattias Sjögren

Anders,
This is true if BaseClass and SomeInterface both are classes, but I think
SomeInterface is an interface, hence the name.

Right, but the way I read Aquila's post, s/he wanted the same
inheritance hierarchy to be reflected in the hypothetical Type<T>
Reflection class. So the following assertions would hold

class Type<T> {...}

Type<DerivedClass> tdc = typeof(DerivedClass);
Assert( tdc is typeof(SomeInterface) );
Assert( tdc is typeof(BaseClass) );

and that will certainly not work without MI when Type<T> is a class. I
guess this could sort of work if Type<T> was an interface. But that
leads to other problems.



Mattias
 
A

Anders Norås [MCAD]

Right, but the way I read Aquila's post, s/he wanted the same
inheritance hierarchy to be reflected in the hypothetical Type<T>
Reflection class. So the following assertions would hold
...
and that will certainly not work without MI when Type<T> is a class. I
guess this could sort of work if Type<T> was an interface. But that
leads to other problems.

Sure, I guess we interpreted the question differently.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top