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.TypeLoa dException' occurred in
mscorlib.dll
Additional information: Type name 'GenDerivBug.Ge nBase[[System.Int32,
mscorlib, Version=1.2.340 0.0, Culture=neutral ,
PublicKeyToken= b77a5c561934e08 9]]' from assembly 'GenDerivBug,
Version=1.0.141 9.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.Reflecti on;
using System.Reflecti on.Emit;
using System.Threadin g;
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.GetDomai n().DefineDynam icAssembly(asn,
AssemblyBuilder Access.Run);
ModuleBuilder module;
module = asb.DefineDynam icModule("GenAl goDynMod");
// create a class derived from the generic base
// this is what I want but it leads to CreateType() failing
TypeBuilder derivBuilder = module.DefineTy pe("DevT",
TypeAttributes. Public, typeof(GenBase< int>));
// the following line makes CreateType work but it's not what I
want:
//TypeBuilder derivBuilder = module.DefineTy pe("DevT",
TypeAttributes. Public, typeof(GenBaseI nt));
// create a type and see if it works
Type derivType = derivBuilder.Cr eateType();
GenBase<int> gb =
(GenBase<int>)A ctivator.Create Instance(derivT ype);
int i = 3;
int j = gb.foo(i+1);
Console.WriteLi ne("i={0} j={1}", i, j);
}
}
} 1 2130
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.TypeLoa dException' occurred in mscorlib.dll
Additional information: Type name 'GenDerivBug.Ge nBase[[System.Int32, mscorlib, Version=1.2.340 0.0, Culture=neutral , PublicKeyToken =b77a5c561934e0 89]]' from assembly 'GenDerivBug, Version=1.0.14 19.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.Reflecti on; using System.Reflecti on.Emit; using System.Threadin g;
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.GetDomai n().DefineDynam icAssembly(asn, AssemblyBuilde rAccess.Run); ModuleBuilder module; module = asb.DefineDynam icModule("GenAl goDynMod");
// create a class derived from the generic base // this is what I want but it leads to CreateType() failing TypeBuilder derivBuilder = module.DefineTy pe("DevT", TypeAttributes .Public, typeof(GenBase< int>));
// the following line makes CreateType work but it's not what I want: //TypeBuilder derivBuilder = module.DefineTy pe("DevT", TypeAttributes .Public, typeof(GenBaseI nt));
// create a type and see if it works Type derivType = derivBuilder.Cr eateType(); GenBase<int> gb = (GenBase<int>) Activator.Creat eInstance(deriv Type); int i = 3; int j = gb.foo(i+1); Console.WriteLi ne("i={0} j={1}", i, j); } } } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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{
|
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 (Subject is an object with protected
constructor)
The problem is, whenever I used dynamic_cast to cast a Subject pointer
to XXX (I am quite sure...
|
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;
|
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);
}
public class B
{
|
by: Pascal Costanza |
last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Monday, February 13, 2006, VUB Campus Etterbeek
The VUB (Programming Technology Lab, System and Software Engineering
Lab), ULB (deComp) and the Belgian Association for Dynamic Languages
(BADL) are very pleased to invite you to a whole...
| |
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 RuleDelegate<T>(T item);
In my class my goal is to use a generic list collection to contain my
generic delegates. This will allow me to pass this List to...
|
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?
namespace genericTest
{
public partial class Form1 : Form
{
public Form1()
{
|
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 application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The...
|
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") >
.... or is there a way to declare a generic using strings that hold class
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |