Results 1 to 4 of 4

Thread: [RESOLVED] Type equality...?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] Type equality...?

    Hey,

    I need to check if a certain Type instance:
    1. Is equal to a type, and
    2. Is equal to another type or derives from that type.

    A little hard to explain, so let's bring in an example (actually, the real thing in this case as it's simple enough to understand).


    I have one abstract class Shape, and many deriving classes, such as RectangleShape, EllipseShape, TriangleShape, etc.
    Now, I need to check two different things:

    Suppose I have a Type instance 't'. I need to check if

    1. this type is exactly another type (such as RectangleShape). In english: "Is t a RectangleShape, but not some class deriving RectangleShape".

    2. this type t is either one of the deriving Shapes (in english: "Does t derive from Shape?")


    In code, I have a method that accepts a Type, and I need to check number 2 first (as a check whether the Type is actually a Shape, whatever Shape it is), and number 1 later (to check which kind of shape it actually is).

    csharp Code:
    1. private int GetNextFreeNumber(Type t)
    2. {
    3.     if (t derives from Shape)
    4.     {
    5.         foreach (Shape s in this)
    6.         {
    7.             if (t is the type of s, and not a type deriving from s, nor any other Shape type)
    8.             {
    9.                 //...
    10.             }
    11.         }
    12.     }
    13. }

    How do I do this? I know I can use
    Code:
    if (t == typeof(Shape))
    and I also know of the 'is' operator, something like
    Code:
    if (someInstance is Shape)
    but I can't use the 'is' operator because I haven't got an instance of Shape, I only got a Type (and that Type may be Button or Form or whatever, in which case I don't even need to do the foreach loop).

    So basically I know of only one 'type equality' check in C#, but obviously I can't use it for both... Does my method do check number 1 or number 2, and how can I do the other check?

    Googling for 'type equality' has turned out pretty useless, all I get is 'reference type equality' showing how to implement an IEquatable interface, and stuff like that.

    Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Type equality...?

    E.g.
    Code:
    Type c = typeof (Control);
    Type f = typeof (Form);
    Type b = typeof (Button);
    
    MessageBox.Show(c == typeof (Control) ? "c is Control" : "c is not Control");
    
    MessageBox.Show(f == typeof (Control) ? "f is Control" : "f is not Control");
    
    MessageBox.Show(b == typeof (Control) ? "b is Control" : "b is not Control");
    
    MessageBox.Show(c.IsSubclassOf(typeof (Control))
                        ? "c is derived from Control"
                        : "c is not derived from Control");
    
    MessageBox.Show(f.IsSubclassOf(typeof (Control))
                        ? "f is derived from Control"
                        : "f is not derived from Control");
    
    MessageBox.Show(b.IsSubclassOf(typeof (Control))
                        ? "b is derived from Control"
                        : "b is not derived from Control");
    
    MessageBox.Show((typeof(Control)).IsAssignableFrom(c)
                        ? "Control is assignable from c"
                        : "Control is not assignable from c");
    
    MessageBox.Show((typeof(Control)).IsAssignableFrom(f)
                        ? "Control is assignable from f"
                        : "Control is not assignable from f");
    
    MessageBox.Show((typeof(Control)).IsAssignableFrom(b)
                        ? "Control is assignable from b"
                        : "Control is not assignable from b");
    Last edited by jmcilhinney; May 25th, 2010 at 08:19 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

  4. #4
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] Type equality...?

    hey,
    about the IsSubclassOf, i just wanna share this
    http://msdn.microsoft.com/en-us/libr...ubclassof.aspx
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width