Type Hierarchies

Do you program directly PostSharp.CodeModel or another low-level API and need help? Put your questions here and we will try to answer.

Type Hierarchies

Postby tmilker on Sat Feb 23, 2008 4:59 pm

Does PostSharp model any type of hierarchy for types? For instance, if I have a class inheritance hierarchy of:

System.Object -> GrandParent -> Parent -> Child

Is there a way to get the TypeDefDeclaration of the Parent, assuming I have the Child's TypeDefDeclaration, from there get the GrandParent's, etc. back up the tree?

I know I can do this recursively with GetDeclarationEnumerator but I am just wondering if there is another way I am not seeing.
tmilker
 
Posts: 2
Joined: Sat Feb 23, 2008 4:45 pm
Full Name: Ted Milker

Re: Type Hierarchies

Postby gfraiteur on Sat Feb 23, 2008 5:40 pm

Navigating from children to the parent is easy: it is the property child.BaseType.

To navigate from parents to children, have a look at the class TypeHierarchyTask. This task indexes all parent-child relationships one for all.

The following code enumerates all types (in the current module) derived (even indirectly) from MulticastAttribute:

Code: Select all
            TypeHierarchyTask typeHierarchy = TypeHierarchyTask.GetTask( this.Project );

            IEnumerator<Type> typeEnumerator =
                typeHierarchy.GetDerivedTypesEnumerator( typeof(MulticastAttribute), true, false );

            while ( typeEnumerator.MoveNext() )
            {
            }


The advantage of using this task is of course that the whole indexing is done only once for all tasks.

Be sure to add this task as a prerequisite of your.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 651
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Type Hierarchies

Postby tmilker on Sat Feb 23, 2008 6:25 pm

*INCORRECT*But BaseType just returns IType. You cannot go any further backwards and get the BaseType of that type.

*edit I see, I had a bug in the checks I was doing to get this. Thanks a lot.
tmilker
 
Posts: 2
Joined: Sat Feb 23, 2008 4:45 pm
Full Name: Ted Milker

Re: Type Hierarchies

Postby gfraiteur on Sat Feb 23, 2008 6:37 pm

Sure, BaseType returns an IType. Because the base type can be a TypeSpec, a TypeRef or a TypeDef. If you want the TypeDef, you have to use ITypeSignature.GetTypeDefinition(). The TypeDef can be in another module, of course.
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 651
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org


Return to PostSharp Core