1

I have the (pseudo) code:

public class GlobalClass
{
    public GlobalClass()
    {
        var x = this.GetType().Name // Returns "Channels"
        // WHAT TO DO HERE?
    }
}

public class BaseClass
{
    public string Title { get; set; }
}

And using this code:

public class Channels : GlobalClass
{
    public Channels()
    {

    }

    public class Channel : BaseClass
    {

    }
}

Where the comment is (// WHAT TO DO HERE?), I want to get the runtime type of BaseClass, where in my sample code should return Channel.

I am open to different approaches, but only if it's accompanied with an explanation why I should change the code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
  • 2
    What are you trying to do, actually? Why do you think you need this type? What if `GlobalClass` inherits a different class? Which class type will you need in that case? – vgru Jan 31 '12 at 09:21
  • i have 3-5 class which have the same properties and methods, all i want to do is stop repeating my code in each and every class. i know its a little over my head, but i am willing to learn :) – Rafael Herscovici Jan 31 '12 at 09:45
  • why the Close request? there is no duplicate for this question and i found no information googling it. please explain. – Rafael Herscovici Jan 31 '12 at 09:46
  • *i want to do is stop repeating my code in each and every class*: Yes, inheritance will help you achieve this. But there is rarely a need to access the **type** of the base class when doing this. What do you want to do with this type? – vgru Jan 31 '12 at 09:53
  • @Groo the data for those classes are loaded from files, which reside each in the appropiate folder name ( the folder name is the same as the class ). – Rafael Herscovici Jan 31 '12 at 10:40

4 Answers4

2

I think you need a generic class here, something like:

    public class GlobalClass<T> where T : BaseClass
    {
        public GlobalClass()
        {
            var theType = typeof(T);    //you got it
        }
    }
    public class BaseClass
    {
        public string Title { get; set; }
    }

    public class Channel : BaseClass { }
    public class Channels : GlobalClass<Channel> { }
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
0

is operator is just for that purpose.

getType() method with class Type can also be used.

class Example 
{
    static void ShowTypeInfo (object o) 
    {  
        Console.WriteLine ("type name = {0}, 
                            full type name = {1}", o.GetType(), 
                            o.GetType().FullName ); 
    }

    public static void Main()
    { 
        long longType = 99; 
        Example example= new Example(); 

        ShowTypeInfo (example); 
        ShowTypeInfo (longType); 
    }
}
Azodious
  • 13,752
  • 1
  • 36
  • 71
0

To get the runtime type of anything, you first need an object instance to get the type from. So with your given structure, that's not possible.

There are two possible approaches:

  1. Add a BaseClass parameter to the constructor of your GlobalClass:

    public class GlobalClass
    {
        public GlobalClass(BaseClass data)
        {
            var dataType = data == null ? null : data.GetType();
            // do something with the type
        }
    }
    
    public class Channels : GlobalClass
    {
        public Channels(Channel data) : base(data)
        {
    
        }
    
        public class Channel : BaseClass
        {
    
        }
    }
    
  2. Pass the type to the constructor directly:

    public class GlobalClass
    {
        public GlobalClass(Type actualType)
        {
            Debug.Assert(typeof(BaseClass).IsAssignableFrom(actualType));
        }
    }
    
    public class Channels : GlobalClass
    {
        public Channels() : base(typeof(Channel))
        {
    
        }
    
        public class Channel : BaseClass
        {
    
        }
    }
    

If the structure for some reason doesn't allow generics here (as Danny Chen suggested), I'd personally prefer the second approach, since that doesn't need an actual instance.

Community
  • 1
  • 1
Nuffin
  • 3,882
  • 18
  • 34
0

You can use reflection like this:

using System.Reflection;

...

public class GlobalClass
{
    public GlobalClass()
    {
        Type[] types = Assembly.GetExecutingAssembly ().GetTypes ();
        foreach ( Type t in types )
        {
            if ( t.BaseType == typeof ( BaseClass ) )
            {
                Console.WriteLine ( "I found a class " + t.Name + " that subclass BaseClass" );
            }
        }
    }
}

See also Stack Overflow question List of classes in an assembly.

Community
  • 1
  • 1
Alexis Pautrot
  • 1,128
  • 1
  • 14
  • 18