Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

Generic and Inheritance of "Type"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Aquila Deus - 28 Dec 2004 09:08 GMT
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 :)
Anders Nor?s [MCAD] - 28 Dec 2004 13:06 GMT
> 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/
Mattias Sj?gren - 28 Dec 2004 15:23 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.

>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/

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.