silly question.

the machine where this does not work....is framework 2.0 installed?


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Brad Wilson
Sent: Monday, July 23, 2007 12:43 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] What's wrong with typeof(GenericClass<>)
for Reflection Emit?


FWIW, using typeof(System.Collections.Generic.List<>) yields the same error.

On 7/23/07, Brad Wilson <[EMAIL PROTECTED]> wrote:
>
> I'm using AssemblyBuilder and friends to emit a wrapper class that derives
> from a given type. When provided with typeof(NonGenericClass), it works
> great. However, when it is provided with typeof(GenericClass<>), it fails to
> find that type at the final stages of the emit:
>
> System.TypeLoadException was unhandled
>   Message="Could not load type 'GenericClass`1' from assembly 'Example,
> Version= 0.0.0.0, Culture=neutral, PublicKeyToken=null'."
>   Source="mscorlib"
>   TypeName="GenericClass`1"
>   StackTrace:
>        at System.Reflection.Emit.TypeBuilder.TermCreateClass(Int32 handle,
> Module module)
>        at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
>        at System.Reflection.Emit.TypeBuilder.CreateType()
>        at Program.WrapType(Type typeToWrap) in Program.cs:line 46
>        at Program.Main() in Program.cs:line 15
>
> The problem is, "Example" is the name of the dynamic assembly module, not
> the name of the EXE where GenericClass<> is found. I believe it's looking in
> the wrong place to find the generic type, even though we pass a Type object
> to DefineType().
>
> The code is pretty straight-forward Reflection Emit code, which I have
> narrowed down to the smallest amount of code necessary to repro (attached
> below).
>
> Thoughts? Why does this work for non-generics and not for generics?
>
> - Brad
>
> P.S. Just to complicate matters, this code works on the machine we
> developed it on, but no others. We can't figure out exactly what the
> difference between that machine and any others is.
>
> -------- Repro Code --------
>
> using System;
> using System.Reflection ;
> using System.Reflection.Emit;
> using System.Threading;
>
> public class NonGenericClass {}
>
> public class GenericClass<T> {}
>
> public class Program
> {
>     public static void Main()
>     {
>         WrapType(typeof(NonGenericClass));    // This works fine
>         WrapType(typeof(GenericClass<>));     // This throws the above
> exception
>     }
>
>     public static Type WrapType(Type typeToWrap)
>     {
>         // Create the wrapper type
>         AppDomain appDomain = Thread.GetDomain();
>         AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(new
> AssemblyName("Example"), AssemblyBuilderAccess.Run);
>         ModuleBuilder module = assemblyBuilder.DefineDynamicModule("
> Example.dll");
>         TypeBuilder typeBuilder = module.DefineType("Wrappers." +
> typeToWrap.Name, typeToWrap.Attributes, typeToWrap);
>
>         // Setup generic arguments
>         Type[] genericParameterTypes = typeToWrap.GetGenericArguments();
>
>         if (genericParameterTypes.Length > 0)
>         {
>             string[] genericParameterNames = new string[
> genericParameterTypes.Length];
>             for (int idx = 0; idx < genericParameterTypes.Length; idx++)
>                 genericParameterNames[idx] =
> genericParameterTypes[idx].Name;
>
>             GenericTypeParameterBuilder[] genericBuilders =
> typeBuilder.DefineGenericParameters (genericParameterNames);
>
>             for (int idx = 0; idx < genericBuilders.Length ; idx++)
>             {
>
> genericBuilders[idx].SetGenericParameterAttributes(genericParameterTypes[idx].GenericParameterAttributes);
>                 foreach (Type type in
> genericParameterTypes[idx].GetGenericParameterConstraints())
>                     genericBuilders[idx].SetBaseTypeConstraint(type);
>             }
>         }
>
>         // Return wrapper type
>         return typeBuilder.CreateType();
>     }
> }




--
http://www.agileprogrammer.com/dotnetguy/
http://www.flickr.com/photos/dotnetguy/
http://www.last.fm/user/dotnetguy/

(\__/)
(='.'=) This is Bunny. Copy and paste bunny into your
(")_(") signature to help him gain world domination.

===================================
This list is hosted by DevelopMentor  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to