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

Generic List class property

I want to define a class like this

[DataContract]
class MyClass
{
[DataMember]
List<TMyList<Twhere 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
DataContractSerializer to serialize the object straightforwardly 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 3024
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<somebasetype>. 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...@gmail.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<somebasetype>. 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<ElementElements and
List<OtherElementOtherElements), 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...@nnowslpianmk.comwrote:
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<IElementbut then kept getting errors
saying that a List<Elementcould not be converted to a List<IElement>
because the methods in our existing system return List<concreteclass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementinstead 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<IElementbut then kept getting errors
saying that a List<Elementcould not be converted to a List<IElement>
because the methods in our existing system return List<concreteclass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementinstead 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<IElementeverywhere, 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<Timplements 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...@nnowslpianmk.comwrote:
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<IElementbut then kept getting errors
saying that a List<Elementcould not be converted to a List<IElement>
because the methods in our existing system return List<concreteclass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementinstead 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<IElementeverywhere, 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<Timplements 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<IElementand it would be created as such in the
first place instead of the code only creating List<concreteclass>.

thanks
Oct 28 '08 #7

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

Similar topics

1
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...
2
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. ...
0
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...
8
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...
1
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....
4
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...
3
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...
4
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...
11
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.