473,387 Members | 1,859 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,387 software developers and data experts.

generic collection with anonymous types

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 2703
Christian Binder
218 Expert 100+
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 100+
@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
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...
8
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; ...
8
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*...
2
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...
6
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
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...
5
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...
26
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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,...
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.