473,408 Members | 2,813 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,408 software developers and data experts.

List of generics List<myObject<T>>, is this possible with the Tpossibly changing?

Hi all,

I have a class (named for the example myObject) that can be of
several types (int, string, float, etc), instead of using a object to
define it's type I used a generic.

public class MyObject<T: ChangeObject
{
...
public MyObject(string name, T value)
{
// invoke the properties, since there's additional work to
do
Name = name;
Value = value;
}

}

Until now, everything is ok, what I want to do now, is to set a
property inside a class that is a list of these objects.
So I thought it would be has easy has:

List<MyObject<T>Objects;

or even:

MyObject<T>[] Objects;

But it seems that the T only works on class definitions, is this
true?
What can I do to have a list of generic objects of this kind?

Thanks
Jun 27 '08 #1
10 6434
The "T" is defined for the entirety of the generic type or generic
method that declares it. So any code inside MyObject<Tcan make use of
T. However, code outside of MyObject<Thas no idea *which* T you are
talking about.

In your regular C# code, you must indicate what you are talking about -
for example you might have a "List<MyObject<Foo>objects".
Alternatively, if you are inside a generic type/method, you can use your
local generic arguments instead:

public void Bar<TSomethingElse>() {
List<MyObject<TSomethingElse>objects = ...
}

(TSomethingElse here named to distinguish that this is a *different*
generic type-argument to the T in MyObject<T>).

Constraints and type-inference can greatly simplify this - but it is
hard to show a meaningful example without more context as to what
exactly you want to do.

Marc
Jun 27 '08 #2
On Jun 19, 10:20 am, Marc Gravell <marc.grav...@gmail.comwrote:
The "T" is defined for the entirety of the generic type or generic
method that declares it. So any code inside MyObject<Tcan make use of
T. However, code outside of MyObject<Thas no idea *which* T you are
talking about.

In your regular C# code, you must indicate what you are talking about -
for example you might have a "List<MyObject<Foo>objects".
Alternatively, if you are inside a generic type/method, you can use your
local generic arguments instead:

public void Bar<TSomethingElse>() {
List<MyObject<TSomethingElse>objects = ...

}

(TSomethingElse here named to distinguish that this is a *different*
generic type-argument to the T in MyObject<T>).

Constraints and type-inference can greatly simplify this - but it is
hard to show a meaningful example without more context as to what
exactly you want to do.

Marc
On Jun 19, 10:20 am, Marc Gravell <marc.grav...@gmail.comwrote:
The "T" is defined for the entirety of the generic type or generic
method that declares it. So any code inside MyObject<Tcan make use of
T. However, code outside of MyObject<Thas no idea *which* T you are
talking about.

In your regular C# code, you must indicate what you are talking about -
for example you might have a "List<MyObject<Foo>objects".
Alternatively, if you are inside a generic type/method, you can use your
local generic arguments instead:

public void Bar<TSomethingElse>() {
List<MyObject<TSomethingElse>objects = ...

}

(TSomethingElse here named to distinguish that this is a *different*
generic type-argument to the T in MyObject<T>).

Constraints and type-inference can greatly simplify this - but it is
hard to show a meaningful example without more context as to what
exactly you want to do.

Marc
Hi Marc,

thanks for your reply.

I will try to give you my concrete example:
We have several objects that can have a set of user attributes
(defined by the user), these attributes can be of several types (int,
string, float, etc), so, we defined a UserDefinedAttribute class:

public class UserDefinedAttribute<T>
{
[XmlElement("Value")]
public T Value
{
get {
return this.value;
}

set {
// Make sure the type information is not violated
validateType(value);

this.value = value;

// Infer type and size information if unknown
inferType();
}
}

private void inferType()
{
if (this.dataType == (sbyte)Globals.DataTypesEnum.Unknown)
{
Type type = typeof(T);
if (type.IsArray)
{
this.size = (this.value as Array).Length;
}

if (type == typeof(int) || type == typeof(int []))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Integer;
}
else if (type == typeof(bool) || type ==
typeof(bool[]))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Boolean;
}
else if (type == typeof(string) || type ==
typeof(string[]))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.String;
}
else if (type == typeof(float) || type ==
typeof(float[]))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Float;
}
else
{
}
}
}

public UserDefinedAttribute(string name, T value)
{
// invoke the properties, since there's additional work
todo
Name = name;
Value = value;
}
}
So, the attributes are defined.

Now I have other classes like Auto that must have a list of
UserDefinedAttribute, the original developer used an array list for
this, but I would like to have something like
List<UserDefinedAttribute<T>>, but the framework doesn't let me.
I know that I can have List<UserDefinedAttribute<string>or
List<UserDefinedAttribute<int>>, but since the type may vary from
UserDefinedAttribute to UserDefinedAttribute I would like to have a
generic type.

Can you point me in the right direction?

Thanks
Jun 27 '08 #3
Right; in this case, I might have a non-generic interface to use -
soemthing like IUserDefinedType below. Then you can have a list of
IUserDefinedType. That is the probably closest you can get if the T can
change per item in the list.

Note also that there are other ways of modelling flexible properties -
for example, if you use data-binding, System.ComponentModel allows you
to have runtime properties via ICustomTypeDescriptor or
TypeDescriptionProvider. Quite an involved area, but if you make use of
data-binding, let me know - I can give you some hints to get this working...

Marc

public interface IUserDefinedType
{
object Value { get; set; }
Type Type { get; }
}
public interface IUserDefinedType<T: IUserDefinedType
{
new T Value { get; set; }
}
public class UserDefinedType<T: IUserDefinedType<T>
{
public T Value { get; set; }
public Type Type { get { return typeof(T); } } // or whatever
object IUserDefinedType.Value
{
get { return Value; }
set { Value = (T)value; }
}
}
Jun 27 '08 #4
lpinho wrote:
Now I have other classes like Auto that must have a list of
UserDefinedAttribute, the original developer used an array list for
this, but I would like to have something like
List<UserDefinedAttribute<T>>, but the framework doesn't let me.
I know that I can have List<UserDefinedAttribute<string>or
List<UserDefinedAttribute<int>>, but since the type may vary from
UserDefinedAttribute to UserDefinedAttribute I would like to have a
generic type.
You can't declare any other type than List<Objectthat can hold both
UserDefinedAttribute<stringand UserDefinedAttribute<int>, as Object is
the only common base class for those types.

You can make a non-generic interface:

interface IUserDefinedAttribute {}

Let the UserDefinedAttribute class implement this interface, then you
can declare a list of attributes:

List<IUserDefinedAttribute>

This of course doesn't do much unless you put some method signatures in
the interface, but at least you are able to declare a list that can only
contain attributes.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #5
(I meant IUserDefinedAttribute, btw, not IUserDefinedType; head on
sdrawkcab today...)
Jun 27 '08 #6
On Jun 19, 1:04 pm, Marc Gravell <marc.grav...@gmail.comwrote:
(I meant IUserDefinedAttribute, btw, not IUserDefinedType; head on
sdrawkcab today...)
What's in a name :)

Thanks for the reply, I will try that approach

Jun 27 '08 #7
lpinho wrote:
Hi all,

I have a class (named for the example myObject) that can be of
several types (int, string, float, etc), instead of using a object to
define it's type I used a generic.

public class MyObject<T: ChangeObject
{
...
public MyObject(string name, T value)
{
// invoke the properties, since there's additional work to
do
Name = name;
Value = value;
}

}

Until now, everything is ok, what I want to do now, is to set a
property inside a class that is a list of these objects.
So I thought it would be has easy has:

List<MyObject<T>Objects;

or even:

MyObject<T>[] Objects;

But it seems that the T only works on class definitions, is this
true?
What can I do to have a list of generic objects of this kind?
List<ChangeObject>
>
Thanks

Jun 27 '08 #8
Hi Again Marc,

sorry to bother you again, I seemed to block in another point, I did
has you suggested and it worked very well.

Now I'm facing another problem, serialization :(

This is what I've done:

[XmlArray("UserDefinedAttributes")]
[XmlArrayItem("UserDefinedAttribute")]
public ArrayList XmlAttributes
{
get {
ArrayList output = new ArrayList();
if (attributes != null)
{
foreach (IUserDefinedAttributes
tmpUserDefinedAttributes in this.attributes)
{
Type typeOfUDA = tmpUDA.Type;
if (typeOfUDA == typeof(string))

output.Add((UserDefinedAttribute<string>)tmpUDA);
else if (typeOfUDA == typeof(int))

output.Add((UserDefinedAttribute<int>)tmpUDA);
else if (typeOfUDA == typeof(float))

output.Add((UserDefinedAttribute<float>)tmpUDA);
else if (typeOfUDA == typeof(DateTime))

output.Add((UserDefinedAttribute<DateTime>)tmpUDA) ;
else if (typeOfUDA == typeof(TimeSpan))

output.Add((UserDefinedAttribute<TimeSpan>)tmpUDA) ;
else if (typeOfUDA == typeof(bool))

output.Add((UserDefinedAttribute<bool>)tmpUDA);
else
output.Add((UDA<object>)tmpUDA);
}
}
return output;
}
set {
ArrayList input = value;
this.attributes = new List<IUserDefinedAttribute>();
foreach (IUserDefinedAttribute tmpUDA in input)
{
attributes.Add(tmpUDA);
}
}
}

[XmlIgnore]
public List<IUserDefinedAttributeAttributes
{
get {return this.attributes;
}
set { this.attributes = value; }
}

It seems that a list of interfaces would not work since it seems that
only "real" classes can be serialized, so I created an array list,
catch the type and the serialization seems to work correctly, the
problems appear when the deserialization occur, for some reason, it
seems that the deserialization for this property doesn't occur, and
unfortunately, if I try to set the type of the ArrayItem
(IUserDefinedAttribute) it will not work because IUserDefinedAttribute
is an interface.

Is it possible to overcome this? (If I set a breakpoint on the set
method, it not even being called)

On Jun 19, 12:52*pm, Marc Gravell <marc.grav...@gmail.comwrote:
Right; in this case, I might have a non-generic interface to use -
soemthing like IUserDefinedType below. Then you can have a list of
IUserDefinedType. That is the probably closest you can get if the T can
change per item in the list.

Note also that there are other ways of modelling flexible properties -
for example, if you use data-binding, System.ComponentModel allows you
to have runtime properties via ICustomTypeDescriptor or
TypeDescriptionProvider. Quite an involved area, but if you make use of
data-binding, let me know - I can give you some hints to get this working....

Marc

public interface IUserDefinedType
{
* * *object Value { get; set; }
* * *Type Type { get; }}

public interface IUserDefinedType<T: IUserDefinedType
{
* * *new T Value { get; set; }}

public class UserDefinedType<T: IUserDefinedType<T>
{
* * *public T Value { get; set; }
* * *public Type Type { get { return typeof(T); } } // or whatever
* * *object IUserDefinedType.Value
* * *{
* * * * *get { return Value; }
* * * * *set { Value = (T)value; }
* * *}

}- Hide quoted text -

- Show quoted text -
Jun 27 '08 #9
Not really; you'd need to be able to specify the actual type to
instantiate in the XmlArrayItemAttribute - which you simply can't in
this case. You could perhaps use custom serialization
(IXmlSerializable), but it isn't (necessarily) easy...

Marc
Jun 27 '08 #10
On Jun 23, 1:18 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Not really; you'd need to be able to specify the actual type to
instantiate in the XmlArrayItemAttribute - which you simply can't in
this case. You could perhaps use custom serialization
(IXmlSerializable), but it isn't (necessarily) easy...

Marc
OK :(

Thanks
Jun 27 '08 #11

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

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
3
by: Eric | last post by:
I have a string representation of an object. I create an object of that type through reflection. I would like to create a List<> of those objects. I obviously can't do List<myObject.GetType()>...
8
by: Brian P | last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>. I tried to do it this way: private Dictionary<string, MyChildClass> _dictionary; public Dictionary<string,...
5
by: Jimp | last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>. MyObject implements IMyObject, and of course, List implements ICollection. Thanks
2
by: Jinsong Liu | last post by:
I have following 3 classes public class MyMainClass { MyCollection<MyObject> m_oMyObjectCollection = null; private string m_sID = string.Empty; public MyCollection<MyObject> Collection {
0
by: sloan | last post by:
Here is one I've been tinkering around with for a few days. Given the bottom 2 classes and one interface. (at the very bottom) Is there a way to represent EmployeeCollection :...
7
by: daokfella | last post by:
I have a business object that exposes a collection of other objects via a List<of Type>. How can I intercept when an item is either added or removed from this list. Is it possible? private...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...
0
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...
0
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...
0
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...
0
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...

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.