|
List Info
Thread: What's wrong with typeof(GenericClass<>) for Reflection Emit?
|
|
| What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:36:10 |
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(genericPa
rameterTypes[idx].GenericParameterAttributes);
foreach (Type type in
genericParameterTypes[idx].GetGenericParameterConstraints())
genericBuilders[idx].SetBaseTypeConstraint(type);
}
}
// Return wrapper type
return typeBuilder.CreateType();
}
}
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:43:00 |
FWIW, using typeof(System.Collections.Generic.List<>)
yields the same error.
On 7/23/07, Brad Wilson <dotnetguy gmail.com> 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(genericPa
rameterTypes[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.fl
ickr.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
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:43:00 |
FWIW, using typeof(System.Collections.Generic.List<>)
yields the same error.
On 7/23/07, Brad Wilson <dotnetguy gmail.com> 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(genericPa
rameterTypes[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.fl
ickr.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
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:50:52 |
Your code works fine on my work machine (win2k3, .NET
2.0/3.0) ... I get an
instance of Wrappers.NonGenericClass and
Wrappers.GenericClass`1
Sébastien
On 7/23/07, Brad Wilson <dotnetguy gmail.com> 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(genericPa
rameterTypes[idx].GenericParameterAttributes);
> foreach (Type type in
>
genericParameterTypes[idx].GetGenericParameterConstraints())
>
genericBuilders[idx].SetBaseTypeConstraint(type);
> }
> }
>
> // Return wrapper type
> return typeBuilder.CreateType();
> }
> }
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
--
Sébastien
www.sebastienlorion.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:58:02 |
On 7/23/07, Sébastien Lorion <sebastien.lorion gmail.com> wrote:
>
> Your code works fine on my work machine (win2k3, .NET
2.0/3.0) ... I get
> an
> instance of Wrappers.NonGenericClass and
Wrappers.GenericClass`1
Fails for me. 64-bit Windows XP Pro, .NET 2.0/3.0.
--
Curt Hagenlocher
curt hagenlocher.org
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:50:52 |
Your code works fine on my work machine (win2k3, .NET
2.0/3.0) ... I get an
instance of Wrappers.NonGenericClass and
Wrappers.GenericClass`1
Sébastien
On 7/23/07, Brad Wilson <dotnetguy gmail.com> 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(genericPa
rameterTypes[idx].GenericParameterAttributes);
> foreach (Type type in
>
genericParameterTypes[idx].GetGenericParameterConstraints())
>
genericBuilders[idx].SetBaseTypeConstraint(type);
> }
> }
>
> // Return wrapper type
> return typeBuilder.CreateType();
> }
> }
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
--
Sébastien
www.sebastienlorion.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 11:58:02 |
On 7/23/07, Sébastien Lorion <sebastien.lorion gmail.com> wrote:
>
> Your code works fine on my work machine (win2k3, .NET
2.0/3.0) ... I get
> an
> instance of Wrappers.NonGenericClass and
Wrappers.GenericClass`1
Fails for me. 64-bit Windows XP Pro, .NET 2.0/3.0.
--
Curt Hagenlocher
curt hagenlocher.org
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 12:24:03 |
On 7/23/07, Curt Hagenlocher <curt hagenlocher.org> wrote:
>
> On 7/23/07, Sébastien Lorion <sebastien.lorion gmail.com
> wrote:
> >
> > Your code works fine on my work machine (win2k3,
.NET 2.0/3.0) ... I get
> > an
> > instance of Wrappers.NonGenericClass and
Wrappers.GenericClass`1
>
>
> Fails for me. 64-bit Windows XP Pro, .NET 2.0/3.0.
>
As I'm sure you've already discovered, it fails even if you
reference as
parent a generic type that's been specifically defined
within the
ModuleBuilder. My money's on a weird CLR bug.
Does the box where it works have an Orcas or framework 3.5
beta installed on
it?
--
Curt Hagenlocher
curt hagenlocher.org
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 12:20:58 |
> 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.
Have you, by any chance, a more recent version of .NET 2.0
on your
development machine than on the target machine? There have
been quite
a high number of issues with generics and
Reflection/Reflection.Emit
and some of these have been fixed in e.g. the .NET 2.0
update
installing with the more recent Orcas CTPs and Betas.
In your particular case, your development machine seems to
be more
gracious regarding a subtle bug: you don't correctly
associate the
generic parameters of your base type with those of your
derived type.
With System.Reflection.Emit, this association does not come
implicitly
from the parameter names, it must be explicitly specified by
calling
MakeGenericType on the base type, specifying the
GenericParameterBuilders of the derived type.
I've updated your sample, which now works as intended on my
machine, see below.
There is another issue I've noticed (of course, this is only
a quick
sample, but just for completeness): You are assuming that if
you get
any generic arguments back from the typeToWrap, then your
derived type
should also be generic.
Actually, that might not be what you really want because you
also get
something back from Type.GetGenericArguments if you have a
closed
generic type (where all type parameters are bound to
concrete types).
In that closed generic base type case, you probably don't
want your
derived type to be generic at all.
In fact, you could even get a mixture of unbound type
parameters and
bound type arguments back; in that case, you would probably
only want
your derived type to have the unbound generic arguments. You
can check
whether a generic argument is bound or not via its
IsGenericParameter
property (true: unbound; false: bound).
Best regards,
Fabian
--- updated sample ---
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
also does
}
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");
// FS: changed the next line: removed the base type
TypeBuilder typeBuilder = module.DefineType
("Wrappers." +
typeToWrap.Name, typeToWrap.Attributes);
// 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);
}
// FS: added this line to correlate the generic
parameters
typeToWrap = typeToWrap.MakeGenericType
(genericBuilders);
}
// FS: added this line to set the base type after
generic
parameter correlation
typeBuilder.SetParent (typeToWrap);
// Return wrapper type
return typeBuilder.CreateType ();
}
}
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|
|
| Re: What's wrong with
typeof(GenericClass<>) for
Reflection Emit? |

|
2007-07-23 12:24:03 |
On 7/23/07, Curt Hagenlocher <curt hagenlocher.org> wrote:
>
> On 7/23/07, Sébastien Lorion <sebastien.lorion gmail.com
> wrote:
> >
> > Your code works fine on my work machine (win2k3,
.NET 2.0/3.0) ... I get
> > an
> > instance of Wrappers.NonGenericClass and
Wrappers.GenericClass`1
>
>
> Fails for me. 64-bit Windows XP Pro, .NET 2.0/3.0.
>
As I'm sure you've already discovered, it fails even if you
reference as
parent a generic type that's been specifically defined
within the
ModuleBuilder. My money's on a weird CLR bug.
Does the box where it works have an Orcas or framework 3.5
beta installed on
it?
--
Curt Hagenlocher
curt hagenlocher.org
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|
|
|
|