473,748 Members | 11,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ellegant code generation for generics using reflection.

Hi all,
I'm creating code generator for wrappers of some c# classes. With the
introduction of c# 2.0 and generics the following problem arises.
When encountering generic types anywhere inside the assembly, for
example a generic method parameter declared as:

System.Collecti ons.Generic.Lis t<int>

the full name of that parameter type returned by reflection is :

"System.Collect ions.Generic.Li st`1[[System.Int32, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9]]"

This is probably meant to allow runtime creation of that type using
Emit mechanism, I guess.
For the purpose of code generation is quite impractical, because I have
go through all generic types replacing the `1[...] part with the
desired <...>. Although this is not a complicated task, I am suprised
that there is no built-in functionality meant to do that.

Does anybody know if my task can be acomplished without manual string
operations, or the reason why MS haven't added such methods?

Thanks for any comments.
Konrad Kaczanowski

May 11 '06 #1
0 1213

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

Similar topics

3
1372
by: MattC | last post by:
Hi, I have a collection class derived from ArrayList, it stores colletions of objects from my objet model. I Didn't want to have to create a specialised collection for each object type so I created the following static method. public static MyCollection CreateGenericCollection(DataTable rates, Assembly a, Type t) { MyCollection c = new MyCollection(); // create new collection
4
1559
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that extends to having strictly-typed classes at run-time? I would gladly give up the compile-time errors if I could get rid of all these CType()s :)
1
2438
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class able to accept "ThrowInConstructor" and "ThrowInFoo" as type parameter "<T>". There are two methods in...
4
2818
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 another library and call this list of functions. This could provide a new way to build rule base...
6
2013
by: LordHog | last post by:
Hello all, I recently ran into a strange behavior which I don't understand. I have two 'Add' method which a slightly different signature which they look like public void Add( string varName, ScriptVarType type, object value ) public void Add( string varName, ScriptVarType type, object values )
7
2213
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
4
2018
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
7
1445
by: colin | last post by:
Hi, Is there a way of doing simple code generation inside visual c# express such as similar to preprocessing in c++ ? I need to generate a library for some vector maths, but I need to implement it for different types wich would be difficult to do with generic types and/or interfaces for numerous reasons and would probaly sufer a considerable performance hit.
7
6348
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to that list, but it is not explicitly creating an instance of MyClass, but instead using the Activator to create an instance based on it's type name. How can I accomplish this in C# using reflection?
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
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 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.