Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2006

Tip: Looking for answers? Try searching our database.

find out whether there already is an object of the same derived type in a generic collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fabian - 23 Jun 2006 14:36 GMT
Hello,

I have a class hierarchy of "Task Activity" classes for a machine
control system. To manage the activities I have a dictionary class,
derived from Dictionary<string, TaskActivity>.
I have to check whether this dictionary (already) contains a
TaskActivity object of a specific type and - now it becomes difficult -
the mother class hierarchy.

If it was just the same Type my method below would work:

        public bool ContainsObjectOfType(Type ElementType)
        {
            foreach (TaskActivity activity in this.Values)
            {
                if (activity.GetType() == ElementType)
                    return true;
            }
            return false;
        }

I e.g. call it via
bool result =
TaskActivityDictionary.ContainsObjectOfType(typeof(MotherClassOfTaskActivity));

activity.GetType() == ElementType    is only true if the object is of
exactly the same class but I need to know whether it is derived of the
mother class or the mother's mother class... (therefore
activity.GetType().BaseType doesn't work).

Is there another possibility of comparing types at runtime?

Cheers,

Fabian
Markus Kling - 23 Jun 2006 15:04 GMT
The method you are searching for is Type.IsAssignableFrom
http://msdn2.microsoft.com/en-us/library/system.type.isassignablefrom.aspx.

Markus

> Hello,
>
[quoted text clipped - 31 lines]
>
> Fabian

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.