473,241 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

C# Reflection and 'is' operator

I am trying to pass the type of an object to a method, and then check object
references against it to see if they are of that type, using the C# operator
is.

However, my syntax is wrong - see below.

// Main
{
....
CountByType(typeof(Circle));
CountByType(typeof(IStraightSided));
....
}

static int CountByType(Type type)
{
int count = 0;
foreach(Shape shape in shapes)
{
if(shape is type) // incorrect syntax
count++;
}
return count;
}

I have tried
if(shape.GetType() == type) // compiles OK
but this doesn't give the answer I want when I pass an interface type.

I am looking for an elegant solution to this if possible. Failing that I'll
settle for the least inelegant!

I am now in deep philosophy mode about why the is operator seems only to
work with the explicit type name - maybe it is a compile-time thing. The
..NET help doesn't actually state this, however.

Annoying, eh?

tooc
Jul 21 '05 #1
3 1807
Tooc wrote:
if(shape.GetType() == type) // compiles OK
but this doesn't give the answer I want when I pass an interface type.

Use the Type.IsAssignableFrom method.

if(shape.GetType().IsAssignableFrom(type))

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Jul 21 '05 #2
Thanks for the nudge.

Actually it's the other way round

if(type.IsAssignableFrom(shape.GetType())) count++;

"Anders Norås" <an**********@objectware.no> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Tooc wrote:
if(shape.GetType() == type) // compiles OK
but this doesn't give the answer I want when I pass an interface type.

Use the Type.IsAssignableFrom method.

if(shape.GetType().IsAssignableFrom(type))

Anders Norås
http://dotnetjunkies.com/weblog/anoras/

Jul 21 '05 #3
Tooc wrote:
Actually it's the other way round

if(type.IsAssignableFrom(shape.GetType())) count++;


Sorry. Mozilla Thunderbird is a great newsreader, but its a lousy
development environment. :) I mixed up the order.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
4
by: JMZ | last post by:
I want to look up method names in a database which will be present in the executing object and use the names to find the method address to pass to the addressof operator at runtime. Is that...
2
by: André | last post by:
I try to initialize some Public Variable in a class use for code-behind The idea is to check all uxMsgXXX variables, declare in top of my class and assign the string value from a DB request. Why...
5
by: Digital Fart | last post by:
I have 3 strings containing "10" , "20", ">=" would it be possible via reflection to generate a dynamic if statement that return true or false and testing it like this if ( 10 >= 20 ) so a...
4
by: Johndow | last post by:
I have trouble to use "Is" operator to a type that is from reflection. Basically, here is what I want to do, if Class1 type is not from reflection: class Class1 { } public class Test { ... if...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.