> I just thought of that the "Type" should be made as a generic class:
> Type<T>, and a typeof(DerivedClass) should inherit typeof(BaseClass)
[quoted text clipped - 5 lines]
>
> 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/
>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

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aquila Deus - 29 Dec 2004 05:50 GMT
Mattias Sjögren wrote:
> >Type<T>, and a typeof(DerivedClass) should inherit typeof(BaseClass)
> >and typeof(SomeInterface),
>
> That would require multiple inheritance, which isn't supported by
> .NET.
Hmmmm... I forgot that.
> >interface CarFactory {
> >Type<ICar> GetCarType();
[quoted text clipped - 5 lines]
> 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 :)
Anders Nor?s [MCAD] - 29 Dec 2004 10:27 GMT
>But it could just return typeof(MyCar);
>That's exactly what I want it to do :)
Why do you need to roll your own interface to enforce this method? Why can't
you just use GetType inherited from System.Object?
Anders Nor?s
http://dotnetjunkies.com/weblog/anoras/
Anders Nor?s [MCAD] - 29 Dec 2004 11:03 GMT
>>Type<T>, and a typeof(DerivedClass) should inherit typeof(BaseClass)
>>and typeof(SomeInterface),
>
> 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/
Mattias Sj?gren - 29 Dec 2004 12:09 GMT
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

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Anders Nor?s [MCAD] - 29 Dec 2004 12:36 GMT
> Right, but the way I read Aquila's post, s/he wanted the same
> inheritance hierarchy to be reflected in the hypothetical Type<T>
[quoted text clipped - 3 lines]
> 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/