|
-
May 25th, 2010, 01:35 PM
#1
[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:
private int GetNextFreeNumber(Type t) { if (t derives from Shape) { foreach (Shape s in this) { if (t is the type of s, and not a type deriving from s, nor any other Shape type) { //... } } } }
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!
-
May 25th, 2010, 07:00 PM
#2
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.
-
May 26th, 2010, 02:36 AM
#3
Re: Type equality...?
Ah, I didn't know the IsSubclassOf method, that seems to do what I want. I was looking for an operator instead of a method, that explains why I didn't find it 
Thanks!
-
May 26th, 2010, 04:42 AM
#4
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|