Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2006

Tip: Looking for answers? Try searching our database.

Dynamic generic type with base generic type definition

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michal Dvorak - 09 Nov 2006 13:06 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.