Kevin,
| Nice idea, Jay. It is important to note, however, that Reflection would
| still need to be used to discover the type of the particular version of
| "PropertyObject" that would be in the list.
If by Reflection you mean Object.GetType or "is" or "as", then I agree, you
need use Reflection to know the specific type of object in the list. As you
state normally with polymorphic code you don't need to know the specific
type of an object in the list. I hope you would agree that would defeat the
purpose of having polymorphic code.
| In other words,
| nothing would really be gained by using Generics here.
The "Gain" of Generics here is you don't have to explicitly declare
StringPropertyObject, IntPropertyObject, DoublePropertyObject, etc... They
are implicitly declared via the generic type PropertyObject<T>. In fact I
would hope for any T, you now have a specific PropertyObject (bearing in
mind any constraints that T might have). Of course if you need a truely
specialized version of PropertyObject you could define that in addition to
PropertyObject<T>.
One could define normal virtual methods on PropertyObject so I could use the
items in List<PropertyObject> polymophically, just as I would in .NET 1.0 or
1.1. This may however mean that PropertyObject has an object version of a
method/property, where PropertyObject<T> has a T version of the
method/property. Something like:
abstract class PropertyObject
{
// common behavior for a property object
public abstract object Value { get; set; }
}
class PropertyObject<T> : PropertyObject
{
// T specific behavior for a property object
private T m_value;
public PropertyObject(T value)
{
m_value = value;
}
public T TypedValue
{
get { return m_value; }
set { m_value = value; }
}
public override object Value
{
get { return TypedValue; }
set { TypedValue = (T)value; }
}
}
Of course the above could also be done with interfaces rather then a base
class, with an interface you can at least use explicit interface
implementation to avoid have both TypeValue & Value on PropertyObject<T>.
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Nice idea, Jay. It is important to note, however, that Reflection would
| still need to be used to discover the type of the particular version of
| "PropertyObject" that would be in the list. Or, assuming that the base
class
| contained all the characteristics necessary to work with the objects while
| in the List, Reflection could be termporarily unnecessary, at least until
| the typed members of the objects in the List were used. In other words,
| nothing would really be gained by using Generics here. A simple base class
| and a number of derived classes would work just as well, since the Generic
| List object would not contain any type information about what is stored in
| it, other than the base type.
|
| --
| HTH,
|
| Kevin Spencer
| Microsoft MVP
| .Net Developer
| You can lead a fish to a bicycle,
| but you can't make it stink.
|
| "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in
| message news:(E-Mail Removed)...
| > As Kevin states, you cannot do it directly, however you may be able do
it
| > indirectly.
| >
| > Have PropertyObject<T> inherit from a base class. Then use this base
class
| > in List<T>.
| >
| > Something like:
| >
| > abstract class PropertyObject
| > {
| > // common behavior for a property object
| > }
| >
| > class PropertyObject<T> : PropertyObject
| > {
| > // T specific behavior for a property object
| > }
| >
| > List<PropertyObject> props = new List<PropertyObject>();
| > props.Add(new PropertyObject<string>("goot"));
| > props.Add(new PropertyObject<int>(20));
| >
| > PropertyObject would contain all the common behavior that you expect to
| > use
| > polymorphically via the List<T>, while PropertyObject<T> would contain
all
| > the T specific behavior for a property.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley -
http://www.tsbradley.net
| >
| >
| > "msnews.microsoft.com" <(E-Mail Removed)> wrote in message
| > news:(E-Mail Removed)...
| > |I got (what I hope to be is) a simple question....
| > |
| > | I have a class called PropertyObject<T>
| > |
| > | so in my code I created something like this..
| > |
| > | name = new PropertyObject<string>("goot");
| > | age = new PropertyObject<int>(20);
| > |
| > | but what I would rather do is this
| > |
| > | List<PropertyObject<T>> props = new List<PropertyObject>();
| > | props.Add(new PropertyObject<string>("goot"));
| > | props.Add(new PropertyObject<int>(20));
| > |
| > | where the List<> props can only contain PropertyObject<T>'s of any T
| > |
| > | Is this posible or does List<> have to contain only PropertyObjects of
| > the
| > | same generic type parameter and in which case I have to use an
| > ArrayList?
| > |
| > |
| >
| >
|
|