Nested Type within a Generic Type

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

Nested Type within a Generic Type

Postby ThoHot00 on Wed Apr 02, 2008 4:21 pm

Hi Gael,

long time no hear. But here I'm back and again I have a question regarding generics. So far everythings works excellent with PostSharp but as people start to use our framework (still only a pre-release), the first problem was encountered.

During the weave process I add a nested type to each weaved type. And within the static constructor of the weaved type, I create an instance of this nested type. This works fine as long as the weaved type isn't a generic. But doesn't work if it is a generic. The code weaves and verifies ok but at runtime a TypeInitialiationException is thrown.

The reason is the missing generic parameter within the newobj statement:

Code: Select all
L_0038: newobj instance void Omicron.Framework.ObjectModel.Test.GenericTypeTest/ValueContainer/~TypeConfiguration::.ctor()


but it should be:
Code: Select all
L_0038: newobj instance void Omicron.Framework.ObjectModel.Test.GenericTypeTest/ValueContainer/~TypeConfiguration<!T>::.ctor()


Yet I couldn't find out how to pass the generic argument to the method. I create the type using:

Code: Select all
typeConfiguration = new TypeDefDeclaration();

// nested private auto ansi beforefieldinit
typeConfiguration.Attributes = TypeAttributes.NestedPrivate | TypeAttributes.AutoClass | TypeAttributes.BeforeFieldInit | TypeAttributes.Class;
typeConfiguration.Name = "~TypeConfiguration";
typeConfiguration.BaseType = (IType)MemberCache.Object;

typeDef.Types.Add(typeConfiguration);
typeConfiguration.CustomAttributes.Add(new CustomAttributeDeclaration(MemberCache.CompilerGeneratedCtor()));

// clone generic parameter
typeConfiguration.GenericParameters.AddCloneRange(typeDef.GenericParameters);

// create default constructor
typeConfigurationConstructor = EmitHelper.CreateDefaultConstructor(typeConfiguration);


Later I use this typeConfigurationConstructor to instantiate the instance. But as mentioned above this call fails because of the missing <!T>.

Sorry to bother you again with Generics but I have no idea if this is bug or if I'm doing something wrong.

Thanks in advance,
Thomas
Thomas Hotz
ThoHot00
 
Posts: 29
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Thomas Hotz

Re: Nested Type within a Generic Type

Postby ThoHot00 on Wed Apr 02, 2008 8:43 pm

Hi Gael,

okay typeConfiguration is a generic type definition, but using MapGenericArguments on typeConfigurationConstructor doesn't change a thing.

Greetings,
Thomas
ThoHot00
 
Posts: 29
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Thomas Hotz

Re: Nested Type within a Generic Type

Postby gfraiteur on Fri Apr 04, 2008 7:48 am

Hi Thomas,

Nice to see you back ;) .

I'm not sure that it will help you a lot, but I think the problem is that you are doing something wrong...

Ensure that:

1. You copy all the generic parameters of the declaring type to the nested type (I guess from your post that you are doing it well).

2. When you invoke the constructor, you should invoke a "MethodRef->TypeSpec->TypeDef", where TypeDef is your nested type and TypeSpec is a generic type instance. You may have a look at the class GenericHelper to get canonical generic type instances easily.

Does that help?

Regards,

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

Re: Nested Type within a Generic Type

Postby ThoHot00 on Fri Apr 04, 2008 4:36 pm

Hi Gael,

yes it helped. If found the bug and the problem is solved.

Thanks ;) ,
Thomas
Thomas Hotz
ThoHot00
 
Posts: 29
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Thomas Hotz


Return to PostSharp Core