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
Ben Voigt - 10 Nov 2006 16:51 GMT
> Hi,
> I'm trying to define dynamic generic type definition, inherited from
[quoted text clipped - 36 lines]
> 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
Michal Dvorak - 11 Nov 2006 13:38 GMT
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..
>> Hi,
>> I'm trying to define dynamic generic type definition, inherited from
[quoted text clipped - 44 lines]
>> Thanks
>> Michal Dvorak