473,795 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic List class property

I want to define a class like this

[DataContract]
class MyClass
{
[DataMember]
List<TMyList<Tw here T : IMyList {get; set;}
}

or something like this. I don't want to put <Ton my class definition
because it is only after the instance has been created that I will
know exactly which type of object MyList will use. And I want the
DataContractSer ializer to serialize the object straightforward ly so I
want it to remain a Property rather than a method.

But I can't seem to declare it like this. I have tried a few
variations. Is there a solution that is simple or is it not possible?

thanks
Phil

Oct 7 '08 #1
6 3041
types can be generic, and methods can be generic - however, fields and
properties can only use what the type defines. In short, this would
have to be a MyClass<Tto do what you want, or would have to use a
non-generic list interface such as IList - or settle for a fixed
List<somebasety pe>. What exactly is the context - i.e. what are you
trying to model? there may be better approaches...

Marc Gravell
[C# MVP]
Oct 7 '08 #2
On 7 Oct, 15:05, Marc Gravell <marc.grav...@g mail.comwrote:
types can be generic, and methods can be generic - however, fields and
properties can only use what the type defines. In short, this would
have to be a MyClass<Tto do what you want, or would have to use a
non-generic list interface such as IList - or settle for a fixed
List<somebasety pe>. What exactly is the context - i.e. what are you
trying to model? there may be better approaches...

Marc Gravell
[C# MVP]
i have a Survey object that will hold a static data object and a user
data object.

First I read from the database and populate the survey attributes.
Then I populate the static data object with various lists (these are
basically just records from the static data tables in the db).
One of these lists is a list of Elements but this list could be one of
2 types of business object depending on the type of survey (an
attribute of the survey object). I know that both classes implement
the IElement interface and I want to serialize the object at the end
with whichever list is relevant.

I could create 2 properties (List<ElementEl ements and
List<OtherEleme ntOtherElements ), populate the appropriate one and
then use XSL to translate the serialized XML so it only looks like a
single property? But I wondered if there was a neater solution.

thanks for any advice
Phil
Oct 7 '08 #3
On Tue, 07 Oct 2008 07:21:51 -0700, phancey <de**@2bytes.co .ukwrote:
[...]
One of these lists is a list of Elements but this list could be one of
2 types of business object depending on the type of survey (an
attribute of the survey object). I know that both classes implement
the IElement interface and I want to serialize the object at the end
with whichever list is relevant.
If you don't know at compile time what the type is, then using a generic
list isn't all that helpful anyway. At the very least, you might as well
use List<Objector an ArrayList instead.

That said, you say that the type will always implement IElement. So maybe
it makes sense to make your list simply List<IElement>.

Whether this will work with your serialization depends on what
serialization technique you're using. But my recollection is that the
built-in serialization uses reflection to figure out what to write out, so
the static typing of the object shouldn't be an impediment.

Pete
Oct 7 '08 #4
On 7 Oct, 19:12, "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
On Tue, 07 Oct 2008 07:21:51 -0700, phancey <d...@2bytes.co .ukwrote:
[...]
One of these lists is a list of Elements but this list could be one of
2 types of business object depending on the type of survey (an
attribute of the survey object). I know that both classes implement
the IElement interface and I want to serialize the object at the end
with whichever list is relevant.

If you don't know at compile time what the type is, then using a generic *
list isn't all that helpful anyway. *At the very least, you might as well *
use List<Objector an ArrayList instead.

That said, you say that the type will always implement IElement. *So maybe *
it makes sense to make your list simply *List<IElement> .

Whether this will work with your serialization depends on what *
serialization technique you're using. *But my recollection is that the *
built-in serialization uses reflection to figure out what to write out, so *
the static typing of the object shouldn't be an impediment.

Pete
Yes I think you are right. The only trouble is that the way everything
has already been coded in our system makes this very difficult. I
thought I could code as List<IElementbu t then kept getting errors
saying that a List<Elementcou ld not be converted to a List<IElement>
because the methods in our existing system return List<concretecl ass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementin stead of adding them to a
List<Elementand then trying to cast that back as it doesn't work
that way. Trouble is this may be very difficult with the code I have
inherited.

thanks
Oct 8 '08 #5
On Wed, 08 Oct 2008 00:57:57 -0700, phancey <de**@2bytes.co .ukwrote:
Yes I think you are right. The only trouble is that the way everything
has already been coded in our system makes this very difficult. I
thought I could code as List<IElementbu t then kept getting errors
saying that a List<Elementcou ld not be converted to a List<IElement>
because the methods in our existing system return List<concretecl ass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementin stead of adding them to a
List<Elementand then trying to cast that back as it doesn't work
that way. Trouble is this may be very difficult with the code I have
inherited.
Yes, you're right. With a List<IElementev erywhere, you can add Element
instances as well as IElements. But you can't just go assigning a
List<Elementto a variable of List<IElementor vice a versa. If you
could, then that would open the door for code that compiles just fine but
which tries to add list items of the wrong type to the list.

Note that List<Timplement s IList, and that interface supports all the
basic list operations. That's why Marc suggested using it as the type for
the variable.

Without the bigger picture, it's hard to know exactly what the right
advice is. But it seems like if you can't go back and fix all your code
so that it's using a more basic data type, or just use IList. Of course,
it's also possible that you should just skip the idea of having a
general-purpose type for the purpose mentioned in your original post.

Pete
Oct 8 '08 #6
On 8 Oct, 18:58, "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
On Wed, 08 Oct 2008 00:57:57 -0700, phancey <d...@2bytes.co .ukwrote:
Yes I think you are right. The only trouble is that the way everything
has already been coded in our system makes this very difficult. I
thought I could code as List<IElementbu t then kept getting errors
saying that a List<Elementcou ld not be converted to a List<IElement>
because the methods in our existing system return List<concretecl ass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementin stead of adding them to a
List<Elementand then trying to cast that back as it doesn't work
that way. Trouble is this may be very difficult with the code I have
inherited.

Yes, you're right. *With a List<IElementev erywhere, you can add Element *
instances as well as IElements. *But you can't just go assigning a *
List<Elementto a variable of List<IElementor vice a versa. *If you *
could, then that would open the door for code that compiles just fine but*
which tries to add list items of the wrong type to the list.

Note that List<Timplement s IList, and that interface supports all the *
basic list operations. *That's why Marc suggested using it as the type for *
the variable.

Without the bigger picture, it's hard to know exactly what the right *
advice is. *But it seems like if you can't go back and fix all your code *
so that it's using a more basic data type, or just use IList. *Of course, *
it's also possible that you should just skip the idea of having a *
general-purpose type for the purpose mentioned in your original post.

Pete
thanks. I ended up recoding the other bits around my code so that I
could use a List<IElementan d it would be created as such in the
first place instead of the code only creating List<concretecl ass>.

thanks
Oct 28 '08 #7

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

Similar topics

1
4398
by: INeedADip | last post by:
I am trying to use the following generic (reflection) class as the ICamparer parameter for a generic list..but I get the error: "Unable to cast object of type 'GenericComparer' to type 'System.Collections.Generic.IComparer `1' My code looks like the following: List<AccountDB.Queue> oList = getAllQueuesFunction(); oList.Sort(New GenericComparer("QueueName"));
2
1319
by: omellet | last post by:
I'm trying to define a class, A, that has a List<> of interface instances, IA. IA has a property pointing back to a class A instance, so I need to forward define IA in order to use it in A. #include "stdafx.h" using namespace System; using namespace System::Collections::Generic; interface class IA;
0
5930
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
8
2021
by: Nip | last post by:
Hi I want to access the Generic List of the following c++-class inside a vb.net app. Is this possible/how can I do that? Atm, I get the following error in the vb.net app: "Field 'GeoElems' is of an unsupported type." public ref class clsPhysik {
1
1881
by: Pieter | last post by:
Hi, I use a customized Generic List (VB.NET 2.0) inherited from BindingList(Of T). I made some methods in this BindingList, to allow me to do some standard functions on the BindingList-Items. One of them is for instance SumItems, which returns me the sum of a given Property of all the Items in the List.
4
7569
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed for performance not inheritance and, therefore, does not contain any virtual members. The following generic collections are designed for inheritance and should be exposed instead of System.Collections.Generic.List<T>. *...
3
6678
by: dgk | last post by:
I figured that I'd just set the datasource of a gridview to a generic list of classx, but at runtime databind complains: "The data source for GridView with id 'gv1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content." The list (of classx) has public properties, which means rows and columns. Ok, so the gridview can't handle it. What's a good way to display the items in the...
4
2299
by: tadmill | last post by:
Hi, Is it possible for a generic list class to use Reflection on the generic type being used to detect a property within that type that refers to the same generic class, only with a different type as the parameter? From various posts and help, this is what I've got so far: public class DataList<T: BindingList<T>
11
2554
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
2
4186
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
9522
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
10216
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
10165
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
10002
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
9044
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
6783
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
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2921
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.