473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with generics and dynamic class creation

I've been trying to dynamically create a class DevT that's derived
from a generic base GenBase<T>. It doesn't seem to work. I'm attaching
a code sample below that illustrates the problem.

CreateType() fails when the base class is a parametrized class, as in
DevT : GenBase<int>.
CreateType() works if the base class is not parametrized, even if the
base of the base was parametrized, as in DevT : GenBaseInt :
GenBase<int>

The error dialog I'm getting is this:

An unhandled exception of type 'System.TypeLoadException' occurred in
mscorlib.dll

Additional information: Type name 'GenDerivBug.GenBase[[System.Int32,
mscorlib, Version=1.2.3400.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]' from assembly 'GenDerivBug,
Version=1.0.1419.18373, Culture=neutral, PublicKeyToken=null' is
invalid.

Can anyone tell me whether the code should work and it's just a
problem with Whidbey?

Thanks much,
Andrew Queisser

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

namespace GenDerivBug
{
// Generic base class with testing function
public class GenBase<T>
{
public virtual T foo(T t) { return t; }
}

// Non-generic class derived from constructed type
public class GenBaseInt : GenBase<int>
{
}

public class Test
{
public static void Main()
{
// create a dynamic assembly and module
AssemblyName asn = new AssemblyName();
asn.Name = "GenDynAssy";
AssemblyBuilder asb = Thread.GetDomain().DefineDynamicAssembly(asn,
AssemblyBuilderAccess.Run);
ModuleBuilder module;
module = asb.DefineDynamicModule("GenAlgoDynMod");

// create a class derived from the generic base
// this is what I want but it leads to CreateType() failing
TypeBuilder derivBuilder = module.DefineType("DevT",
TypeAttributes.Public, typeof(GenBase<int>));

// the following line makes CreateType work but it's not what I
want:
//TypeBuilder derivBuilder = module.DefineType("DevT",
TypeAttributes.Public, typeof(GenBaseInt));

// create a type and see if it works
Type derivType = derivBuilder.CreateType();
GenBase<int> gb =
(GenBase<int>)Activator.CreateInstance(derivType);
int i = 3;
int j = gb.foo(i+1);
Console.WriteLine("i={0} j={1}", i, j);
}
}
}
Nov 15 '05 #1
1 2122
Hi,

The build of Whidbey you are using does not have Reflection.Emit support
for Generics. There are plans to add that support in a later build.

Thanks
-Joel.
Thanks.
From: an*************@hp.com (andrew queisser)
Subject: Problem with generics and dynamic class creation
Date: 20 Nov 2003 10:14:00 -0800

I've been trying to dynamically create a class DevT that's derived
from a generic base GenBase<T>. It doesn't seem to work. I'm attaching
a code sample below that illustrates the problem.

CreateType() fails when the base class is a parametrized class, as in
DevT : GenBase<int>.
CreateType() works if the base class is not parametrized, even if the
base of the base was parametrized, as in DevT : GenBaseInt :
GenBase<int>

The error dialog I'm getting is this:

An unhandled exception of type 'System.TypeLoadException' occurred in
mscorlib.dll

Additional information: Type name 'GenDerivBug.GenBase[[System.Int32,
mscorlib, Version=1.2.3400.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]' from assembly 'GenDerivBug,
Version=1.0.1419.18373, Culture=neutral, PublicKeyToken=null' is
invalid.

Can anyone tell me whether the code should work and it's just a
problem with Whidbey?

Thanks much,
Andrew Queisser

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

namespace GenDerivBug
{
// Generic base class with testing function
public class GenBase<T>
{
public virtual T foo(T t) { return t; }
}

// Non-generic class derived from constructed type
public class GenBaseInt : GenBase<int>
{
}

public class Test
{
public static void Main()
{
// create a dynamic assembly and module
AssemblyName asn = new AssemblyName();
asn.Name = "GenDynAssy";
AssemblyBuilder asb = Thread.GetDomain().DefineDynamicAssembly(asn,
AssemblyBuilderAccess.Run);
ModuleBuilder module;
module = asb.DefineDynamicModule("GenAlgoDynMod");

// create a class derived from the generic base
// this is what I want but it leads to CreateType() failing
TypeBuilder derivBuilder = module.DefineType("DevT",
TypeAttributes.Public, typeof(GenBase<int>));

// the following line makes CreateType work but it's not what I
want:
//TypeBuilder derivBuilder = module.DefineType("DevT",
TypeAttributes.Public, typeof(GenBaseInt));

// create a type and see if it works
Type derivType = derivBuilder.CreateType();
GenBase<int> gb =
(GenBase<int>)Activator.CreateInstance(derivType) ;
int i = 3;
int j = gb.foo(i+1);
Console.WriteLine("i={0} j={1}", i, j);
}
}
}


Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rajiv Das | last post by:
I have run into a design time problem and I look for help. This is the crux of the design. : class IShape{ public: virtual void Draw() = 0; }; class Circle : public IShape{
2
by: yccheok | last post by:
Hello, I have an object called XXX previously derived from CDocument in my MDI project. Later, I create an concrete class called Subject. And I let XXX to have multiple inheritance from Subject...
4
by: consumer62000 | last post by:
Hello All I found some *issue* with the generics in C#. It looks like an issue to me : In C++ the following code compiles fine class Person { public: char last;
16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
2
by: =?Utf-8?B?bmV0dGVsbGVjdA==?= | last post by:
I have a simple situation in which I want to use generics along with dynamic type assignment. Following code snippet can explain in more detail But I am unable to do that will anybody help me why?...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
4
by: =?Utf-8?B?SGF5U2VlZA==?= | last post by:
Is there some way to use Generics in dynamic code using the Type.GetType("MyClassName") as an argument? List<Type.GetType("MyClassName") oList = new List<Type.GetType("MyClassName") > ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.