![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
"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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |