Dynamic generic type with base generic type definition - DOTNET

This is a discussion on Dynamic generic type with base generic type definition - DOTNET ; 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): ...

+ Reply to Thread
Results 1 to 3 of 3

Dynamic generic type with base generic type definition

  1. Default Dynamic generic type with base generic type definition

    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

  2. Default Re: Dynamic generic type with base generic type definition


    "Michal Dvorak" <mikee2185> wrote in message
    news:eZPsc$$AHHA.4808@TK2MSFTNGP03.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




  3. Default Re: Dynamic generic type with base generic type definition

    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:
    > "Michal Dvorak" <mikee2185> wrote in message
    > news:eZPsc$$AHHA.4808@TK2MSFTNGP03.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 to Thread

Similar Threads

  1. Replies: 1
    Last Post: 11-10-2007, 02:30 PM
  2. Typecasting of generic type
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 10-15-2007, 11:51 AM
  3. child type cannot be used as generic type parameter
    By Application Development in forum CSharp
    Replies: 4
    Last Post: 08-21-2007, 11:54 AM
  4. Failure to get generic type SortedList in Type.GetType()
    By Application Development in forum DOTNET
    Replies: 3
    Last Post: 06-12-2007, 07:52 PM
  5. Replies: 1
    Last Post: 03-28-2007, 02:05 PM