HighTechTalks DotNet Forums  

Dynamic generic type with base generic type definition

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Dynamic generic type with base generic type definition in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Michal Dvorak
 
Posts: n/a

Default Dynamic generic type with base generic type definition - 11-09-2006 , 08:06 AM






Hi,
I'm trying to define dynamic generic type definition, inherited from
another generic type definition (something like class B<T> : A<T>).
I'm using similar code to set base type (if this is wrong, all other
problems will be probably gone):

void SetBaseType(TypeBuilder builder, Type sourceType)
{
Type[] ga = sourceType.GetGenericArguments();
string[] gaNames = new string[ga.Length];

for (int i = 0; i < ga.Length; i++)
gaNames[i] = ga[i].Name;

GenericTypeParameterBuilder[] gaBuilders =
typeBuilder.DefineGenericParameters(gaNames);
// Setting constraints is here

Type baseType = sourceType.MakeGenericType(gaBuilders);
typeBuilder.SetParent(baseType);
}

Now, this works until i want to override any method. When I'm using
sourceType to get method that will be overridden, builder.CreateType()
throws this exception "System.TypeLoadException":
Type 'Foo.ListEx`1' from assembly 'Boo, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' tried to override method
'GetObjectData' but does not implement or inherit that method.

(Note: I'm sure that IL code generation is fine)
I think that problem is that sourceType isn't actually new type parent,
however when any GetMethod (or similar) method on builder.BaseType fails
with exception "System.NotSupportedException: Specified method is not
supported." until type is created (thats probably because i used
non-built types as generic arguments).

Problem is that i'm stuck in the loop.. i can't get methods that should
be overriden until type is created, and can't override methods after
type is created
Any ideas?

Thanks
Michal Dvorak

Reply With Quote
  #2  
Old   
Ben Voigt
 
Posts: n/a

Default Re: Dynamic generic type with base generic type definition - 11-10-2006 , 11:51 AM







"Michal Dvorak" <mikee2185 (AT) gmail (DOT) com> wrote

Quote:
Hi,
I'm trying to define dynamic generic type definition, inherited from
another generic type definition (something like class B<T> : A<T>).
I'm using similar code to set base type (if this is wrong, all other
problems will be probably gone):

void SetBaseType(TypeBuilder builder, Type sourceType)
{
Type[] ga = sourceType.GetGenericArguments();
string[] gaNames = new string[ga.Length];

for (int i = 0; i < ga.Length; i++)
gaNames[i] = ga[i].Name;

GenericTypeParameterBuilder[] gaBuilders =
typeBuilder.DefineGenericParameters(gaNames);
// Setting constraints is here

Type baseType = sourceType.MakeGenericType(gaBuilders);
typeBuilder.SetParent(baseType);
}

Now, this works until i want to override any method. When I'm using
sourceType to get method that will be overridden, builder.CreateType()
throws this exception "System.TypeLoadException":
Type 'Foo.ListEx`1' from assembly 'Boo, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' tried to override method 'GetObjectData' but does not
implement or inherit that method.

(Note: I'm sure that IL code generation is fine)
I think that problem is that sourceType isn't actually new type parent,
however when any GetMethod (or similar) method on builder.BaseType fails
with exception "System.NotSupportedException: Specified method is not
supported." until type is created (thats probably because i used non-built
types as generic arguments).

Problem is that i'm stuck in the loop.. i can't get methods that should be
overriden until type is created, and can't override methods after type is
created
Any ideas?
There are some static member functions TypeBuilder.GetConstructor,
TypeBuilder.GetMethod, etc. that are described as "Returns the method of the
specified constructed generic type that corresponds to the specified method
of the generic type definition."

Quote:
Thanks
Michal Dvorak



Reply With Quote
  #3  
Old   
Michal Dvorak
 
Posts: n/a

Default Re: Dynamic generic type with base generic type definition - 11-11-2006 , 08:38 AM



Thanks for help! This will probably solved my problems.

However i would choose different names for that methods because they
are created for generics only..

Ben Voigt wrote:
Quote:
"Michal Dvorak" <mikee2185 (AT) gmail (DOT) com> wrote in message
news:eZPsc$$AHHA.4808 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
Hi,
I'm trying to define dynamic generic type definition, inherited from
another generic type definition (something like class B<T> : A<T>).
I'm using similar code to set base type (if this is wrong, all other
problems will be probably gone):

void SetBaseType(TypeBuilder builder, Type sourceType)
{
Type[] ga = sourceType.GetGenericArguments();
string[] gaNames = new string[ga.Length];

for (int i = 0; i < ga.Length; i++)
gaNames[i] = ga[i].Name;

GenericTypeParameterBuilder[] gaBuilders =
typeBuilder.DefineGenericParameters(gaNames);
// Setting constraints is here

Type baseType = sourceType.MakeGenericType(gaBuilders);
typeBuilder.SetParent(baseType);
}

Now, this works until i want to override any method. When I'm using
sourceType to get method that will be overridden, builder.CreateType()
throws this exception "System.TypeLoadException":
Type 'Foo.ListEx`1' from assembly 'Boo, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' tried to override method 'GetObjectData' but does not
implement or inherit that method.

(Note: I'm sure that IL code generation is fine)
I think that problem is that sourceType isn't actually new type parent,
however when any GetMethod (or similar) method on builder.BaseType fails
with exception "System.NotSupportedException: Specified method is not
supported." until type is created (thats probably because i used non-built
types as generic arguments).

Problem is that i'm stuck in the loop.. i can't get methods that should be
overriden until type is created, and can't override methods after type is
created
Any ideas?

There are some static member functions TypeBuilder.GetConstructor,
TypeBuilder.GetMethod, etc. that are described as "Returns the method of the
specified constructed generic type that corresponds to the specified method
of the generic type definition."

Thanks
Michal Dvorak



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.