473,804 Members | 3,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

generic collection with anonymous types

24 New Member
dear friends

I have a problem with generic collection with anonymous types;

my code is :

Expand|Select|Wrap|Line Numbers
  1.  public class BASE
  2.     {
  3.     public List<????> CreateCollection(){
  4.         List<????> x = new List<????>;
  5.         object o = Activator.CreateInstance(GetType());
  6.                 o = Convert.ChangeType(o, GetType());
  7.         x.Add((????) o);
  8.     return x;
  9.     }
  10.     }
  11.  
  12.     public class class_1 : BASE{.....}
  13.     public class class_2 : BASE{.....}
  14.  
  15.  

Test Code

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     class_1 c = new class_1;
  4.     List<class_1> list1 = c.CreateCollection();
  5.     // I want to get the collection of class_1 with CreateCollection Method;
  6.  
  7.     class_2 c = new class_2;
  8.     List<class_2> list2 = c.CreateCollection();
  9.     // I want to get the collection of class_2 with CreateCollection Method;
  10.  

thank you;
Jul 9 '09 #1
2 2727
Christian Binder
218 Recognized Expert New Member
Hi,

you could try to do the following:
Expand|Select|Wrap|Line Numbers
  1.   public class BASE {
  2.     protected List<T> CreateCollection<T>() where T : BASE {
  3.       List<T> x = new List<T>();
  4.       object o = Activator.CreateInstance(GetType());
  5.       o = Convert.ChangeType(o, GetType());
  6.       x.Add((T)o);
  7.       return x;
  8.     }
  9.   }
  10.  
  11.    public class class_1 : BASE {
  12.      public List<class_1> CreateCollection() {
  13.        return base.CreateCollection<class_1>();
  14.      }
  15.    }
  16.  
  17.    public class class_2 : BASE {
  18.      public List<class_2> CreateCollection() {
  19.        return base.CreateCollection<class_2>();
  20.      }
  21.    }
  22.  
Your base-class has a generic method, returning a list of anything derived of BASE. It can't be called from outside because of the protected-keyword.
Jul 10 '09 #2
IanWright
179 New Member
@mironline
But you never stated what your problem is???
Jul 10 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

18
3054
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
8
1834
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; namespace DeterminedGenericCollection { // I got tired of copy and pasting IDisposable
8
3284
by: Steven Cummings | last post by:
Hello, I've scoured this usenet group and didn't find anything specific to my problem, so hopefully this won't be a repeated question. I'm all but certain it's not. I would like to *declare* (not just instantiate at runtime) a generic collection whose element-type is a generic class too. But I don't want to declare what the element-type's generic parameters are.
2
2228
by: anders.forsgren | last post by:
I have a generic collection looking like this Set<T> : ICollection<T> { // Create set from existing collection. public Set(ICollection<T> objs) {/* */} // Add objects to set public void AddRange(ICollection<T> objs){ /* */ } }
6
7663
by: Jorge Varas | last post by:
Hi all, Is it possible to have a collection of a generic type? for example: class a<twhere t : class, new { }
2
1491
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1 : Inherits ColecBase(Of Elem1)
5
1870
by: Ethan Strauss | last post by:
Hi, I have just started using Generic Collections for .Net 2.0 and so far they are working very nicely for me. But, I do have a couple of questions. If I have a Generic collection which has a type which is a reference type, is there a way to get Collection.ContainsKey(RefKey) and Collection to work with different instances of equal keys? For example, I have defined a class of "codon" and I have made a dictionary private...
26
3632
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
2
4187
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
9705
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
9576
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
10323
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...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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...
1
7613
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
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
5516
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2988
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.