Click here to Skip to main content
12,394,971 members (64,355 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

1.5K views
1 bookmarked
Posted

Following object inheritance

, 31 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
namespace System{ public static class SystemExtensionMethods { public static string GetAncestry( this object target ) { return string.Join( " -> ", target.GetTypes().Reverse().Select( t => FormatName( t ) ).ToArray() ); } public...
namespace System
{
   public static class SystemExtensionMethods
   {
      public static string GetAncestry( this object target )
      {
         return string.Join( " -> ",
            target.GetTypes().Reverse().Select( t => FormatName( t ) ).ToArray() );
      }
 
      public static IEnumerable<Type> GetTypes( this object target )
      {
         for( Type t = target.GetType(); t != null; t = t.BaseType )
            yield return t;
      }
 
      // Based on code from bjarneds, in:
      // http://www.codeproject.com/kb/dotnet/objectspy.aspx (ObjectInfo.cs)

      public static string FormatName( this Type type )
      {
         return FormatName( type, TypeNameLanguage.CSharp );
      }
 
      public static string FormatName( this Type type, TypeNameLanguage format )
      {
         if( !type.IsGenericType )
            return type.Name;
         int pos = type.Name.LastIndexOf( '`' );
         string name = ( pos >= 0 ? type.Name.Substring( 0, pos ) : type.Name );
         return name + (format == TypeNameLanguage.CSharp ? "<" : "(Of ")
            + string.Join( ",", type.GetGenericArguments()
                              .Select( t => FormatName( t, format ) ).ToArray() )
            + (format == TypeNameLanguage.CSharp ? ">" : ")");
      }
 
      public enum TypeNameLanguage
      {
         CSharp = 0, VB = 1
      }
   }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tonyt
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 31 Jul 2010
Article Copyright 2010 by tonyt
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid