Google Answers Logo
View Question
 
Q: type inheriting of class - C# ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: type inheriting of class - C#
Category: Computers > Programming
Asked by: geraint-ga
List Price: $5.00
Posted: 23 Apr 2004 04:57 PDT
Expires: 23 May 2004 04:57 PDT
Question ID: 334786
How to tell the type of the class that inherits from a base class from
within the base class? (C#) For example, I create a class called
LogFile, in it's constructor I want to open a file with the name of
the class that inherits from LogFile.

Request for Question Clarification by answerguru-ga on 23 Apr 2004 07:03 PDT
Hi there,

When you say "the type of the class that inherits from a base class"
do you mean a subclass of the base class? The object-oriented
structure of C# means that a single base class have have multiple
classes that inherit directly from it. The only thing you can really
determine given a class definition is where it is inherited from (i.e.
what its superclass is).

answerguru-ga

Clarification of Question by geraint-ga on 23 Apr 2004 07:28 PDT
How to tell the subclass, or hierarchy of the subclasses - from inside
the base class: for example:

public class base{
    public base(){
          (here I need to know that the class "subclass" has been instantiated)
    }
}

public class subclass : base{
}


now when I instantiate an object of type "subclass", this will result
in the constructor of "base" being called?  Inside that constructor I
wish to know that the subclass "subclass" has been instantiated.

Request for Question Clarification by answerguru-ga on 23 Apr 2004 08:17 PDT
That's not quite the way it works...the idea is that the author of a
base class doesn't care about what subclasses are created. The person
writing the actual subclass would be responsible for creating a
constructor within the subclass. Expanding on your example slightly:

public class base{
    public base(){
          // instantiate only this class
    }
}

public class subclass : base{
    public subclass(){
         // instantiate the subclass and/or the base class here
    }
}

What you're asking, therefore, is not possible and not really useful
either since in reality a certain base class might be used by
thousands of people to create subclasses. It doesn't really make sense
to expect the author of the base class to keep contact with all these
people and update the base class accordingly. That would defeat the
whole purpose of object oriented programming!

answerguru-ga
Answer  
Subject: Re: type inheriting of class - C#
Answered By: eiffel-ga on 23 Apr 2004 13:39 PDT
Rated:5 out of 5 stars
 
Hi geraint,

It is possible for the constructor of a base class to use the
introspection library (also known as reflection) to discover the
actual type that it is being created as, although I agree with
answerguru-ga that this "goes against the grain" of object oriented
programming.

Here's how I would do it:

   using System;
   using System.Reflection;
   
   class Test
   {
      public static void Main()
      {
         BaseClass b;
         b = new BaseClass();
         b = new SubClass();
      }
   }
   
   public class BaseClass
   {
       public BaseClass(){
         Console.WriteLine("Hello from BaseClass Constructor");
         /* here I need to know that the class "subclass" has been instantiated */
         Console.WriteLine(GetType());
       }
   }
   
   public class SubClass : BaseClass
   {
   }

I compiled and ran this program, and got the following output:

   Hello from BaseClass Constructor
   BaseClass
   Hello from BaseClass Constructor
   SubClass

As you can see, it is printing the runtime type returned by the call
to GetType(), and so we can see which type is being polymorphically
created at runtime. In your case, you could use the value returned by
the call to GetType() as the name of the file that you wish to open.

I have tested this solution using the Mono C# compiler under Linux
www.go-mono.com
but I doubt you will find any differences if you are using Microsoft tools.

Please request clarification if this answer does not yet meet your needs.


Google Search Strategy:

C# introspection type system
://www.google.com/search?q=C%23+introspection+type+system

reflection gettype
://www.google.com/search?q=reflection+gettype

system.reflection.gettype
://www.google.com/search?q=system.reflection.gettype


Regards,
eiffel-ga
geraint-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy