Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wonder if we can know the based class of some class using some code (programmatically), of course, we can know that at design time by using Object Browser, but at runtime, can we know that? For example, I have a custom textbox named "QTextBox" derived from the .NET class "TextBox", but anyone else may not know that, and how can they do to check that? I think it is important to know that before any further handlings with the objects of QTextBox concerning polymorphism, type casting...
I ask this question but I think we may not have any ways!
Thank you so much!
Posted

Reflection. for a variable v, call v.GetType(), or for a type T, call typeof(T). Both expression will return an object of the type System.Type. Check the instance property System.Type.BaseType. Repeat until recursively until you learn all the inheritance chain to the very base type, which is always System.Object.

—SA
 
Share this answer
 
Comments
Hemant__Sharma 9-May-11 2:11am    
ohh. i was few seconds late.. :)
Sergey Alexandrovich Kryukov 9-May-11 10:14am    
:-)
[no name] 9-May-11 7:29am    
Wow! That's exactly what I want! Thank you so much!
Sergey Alexandrovich Kryukov 9-May-11 10:14am    
I knew that... :-)
OK, thanks for accepting this answer.
Good luck, call again,
--SA
There is a tip here: Following object inheritance[^] which does just that - it follows the inheritance chain all the way back.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 3:10am    
Yes, my 5, but I described the pseudo-code in a couple of lines.
Please see.
--SA
Good answer by SAKryukov. Want to add some to it.
If you simply want to check if an Object is a derived of another you can do the following:

C#
QTextBox qtb = new QTextBox();
TextBox tb = qtb as TextBox; // Called TryCast in VB. It tries to cast one Object to another.
if (tb == null)
{
   // Conversion failed, QTextBox is not a derived of TextBox.
}
else
{
   // Conversion succeeded, QTextBox is a derived of TextBox.
}


Edit:
I have used the "as" operator[^]. The "as" operator is equal to TryCast in VB, which describes it pretty well I think. Follow the link for an example and short description "The as operator is used to perform certain types of conversions between compatible reference types. The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception.".
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-May-11 3:12am    
Yes of course. Good, this is a valid practical technique, but you should also mention "as".
Would you please do it to justify my 5?
--SA
Sander Rossel 9-May-11 4:37am    
Edited my post. Thanks for the 5 :)
yes you can:
C#
public class A
    {
        public string StringA;
        public A()
        {
            StringA = "StringA";
        }
    }

    public class B : A
    {
        public string StringB;
        public B()
        {
            StringA = "StringA";
            StringB = "StringB";
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            B bObject = new B();
            Type baseType = bObject.GetType().BaseType;
            string baseClassName = baseType.Name;
        }

    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 3:09am    
Not so good.
Working with names is a dirty way. You can get type of the type Type and compare them, the type Type has semantic equivalence operator. If TypeA == TypeB...
--SA
Hemant__Sharma 9-May-11 5:12am    
Well I just used an example on How to get "base type". I didn't use the baseClassName further and I believe @King Boy is smart enough to understand what to do once he has .GetType().BaseType otherwise the intellisense will tell me what to do next.
Thanks,
Hemant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900