Finding base class of an object
-
Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.
abstract public class MyBaseClass1 : Form { abstract public void MyMethod1(); } abstract public class MyBaseClass2 : Form { abstract public void MyMethod2(); }
I have many forms which are inheriting from these base classes. For example
public partial class Form1 : MyBaseClass1 { public Form1() { InitializeComponent(); } public override void MyMethod1() { MessageBox.Show("mymethod1"); } }
These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form
ActiveMdiChild
. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, Samar -
Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.
abstract public class MyBaseClass1 : Form { abstract public void MyMethod1(); } abstract public class MyBaseClass2 : Form { abstract public void MyMethod2(); }
I have many forms which are inheriting from these base classes. For example
public partial class Form1 : MyBaseClass1 { public Form1() { InitializeComponent(); } public override void MyMethod1() { MessageBox.Show("mymethod1"); } }
These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form
ActiveMdiChild
. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, Samar -
Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.
abstract public class MyBaseClass1 : Form { abstract public void MyMethod1(); } abstract public class MyBaseClass2 : Form { abstract public void MyMethod2(); }
I have many forms which are inheriting from these base classes. For example
public partial class Form1 : MyBaseClass1 { public Form1() { InitializeComponent(); } public override void MyMethod1() { MessageBox.Show("mymethod1"); } }
These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form
ActiveMdiChild
. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, SamarYou have a few options, you can as previously stated use the
is
keyword and see if it's the type you are looking for. An alternative is to actually look at theBaseType
:Type t = ActiveMdiChild.GetType();
//You can then look at t.BaseType, I'd like to point out that if you inherit from
//an interface you will have to look at FindInterfacesType baseClass = t.BaseType;
//You can then create an instance of this type
object something = Activator.CreateInstance(baseClass);However, unless there is a common base class that you can cast to so that you can use it then you are back to checking the actual type and casting to that. So depending on what you have and what you need you can either have a series of if statements checking for the types or the above method where you will be able to create and use your base class without necessarily knowing exactly what it is.
My current favourite quote is: Punch them in the face, see what happens!
-SK Genius
-
You have a few options, you can as previously stated use the
is
keyword and see if it's the type you are looking for. An alternative is to actually look at theBaseType
:Type t = ActiveMdiChild.GetType();
//You can then look at t.BaseType, I'd like to point out that if you inherit from
//an interface you will have to look at FindInterfacesType baseClass = t.BaseType;
//You can then create an instance of this type
object something = Activator.CreateInstance(baseClass);However, unless there is a common base class that you can cast to so that you can use it then you are back to checking the actual type and casting to that. So depending on what you have and what you need you can either have a series of if statements checking for the types or the above method where you will be able to create and use your base class without necessarily knowing exactly what it is.
My current favourite quote is: Punch them in the face, see what happens!
-SK Genius
Hmm.. This looks good but i wont get the methods of the base class object in the intellisense here. Is there a way of getting that too? Maybe i am asking for too much here. :) Regards, Samar
-
Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.
abstract public class MyBaseClass1 : Form { abstract public void MyMethod1(); } abstract public class MyBaseClass2 : Form { abstract public void MyMethod2(); }
I have many forms which are inheriting from these base classes. For example
public partial class Form1 : MyBaseClass1 { public Form1() { InitializeComponent(); } public override void MyMethod1() { MessageBox.Show("mymethod1"); } }
These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form
ActiveMdiChild
. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, SamarCheck out this article: The case for a generic C# converter using operators[^]
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001