IsSubclassOf

by KodefuGuru 18. March 2009 13:24

It appears that I missed an important method that belongs to Type when making the extension method IsType. Jason Olson pointed out that type.IsSubclassOf would provide the functionality necessary to fulfill my coworker's requirement. The functionality and implementation of IsType is a little different, however. IsType will check the current type then recursively checks each BaseType. IsSubclassOf will return false if the original check indicates that the types are identical. Also, it uses a while loop rather than recursively calling itself.

public virtual bool IsSubclassOf(Type c)
{
    
Type baseType = this;
    
if (baseType != c)
    {
        
while (baseType != null)
        {
            
if (baseType == c)
            {
                
return true;
            }
            baseType = baseType.BaseType;
        }
        
return false;
    }
    
return false;
}

I find this interesting. Leave a comment on whether you prefer recursive methods or loops.

I also discovered that the compiler modifies my code to the following:

public static bool IsType(this Type thisType, Type type)
{
    
if (thisType == null)
    {
        
return false;
    }
    
return ((thisType == type) || thisType.BaseType.IsType(type));
}

I prefer to keep the original style because I feel it is more readable. In addition to the if statement, I had the option to use the boolean trick or the ternary operator to perform the same function. I tend to think that if statements are easier to read, but I'm always looking to improve my coding style. Let me know what you prefer.

Tags: ,

Kodefu

Comments

4/1/2009 1:32:06 PM #

Nate Kohari

Or you could just use Type.IsAssignableTo(Type)... ;)

Nate Kohari United States

4/2/2009 10:03:40 AM #

chris

Hmm, Type.IsAssignableFrom(Type) does check the subclasses as well. Seems there's many ways to skin this cat.

I am concerned about one thing though. Why does typeof(float).IsAssignableFrom(typeof(int)) return false? There is an implicit conversion that allows you to assign an int to a float.

chris United States

6/29/2009 10:34:25 PM #

r4 dsi

thanks for the codes I will try it out.

r4 dsi United States

1/8/2010 2:00:18 AM #

Download Instrumentals

A guy says, "For our Twentieth Anniversary, I'm taking my wife to Australia."

His friend says, "That's going to be tough to beat. What are you going to do for your Twenty-fifth Anniversary?"

The first guy says, "I'm going to go back and get her."


Regards
Reealman







Download Instrumentals United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er


World Map

RecentComments

Comment RSS

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010