Generic abstract class

S

SyntaxError

I have a base class Entity<TEntity> as shown below:

public abstract class Entity<TEntity> : IMarconeEntity, ICloneable
where TEntity : class, INotifyPropertyChanged,
INotifyPropertyChanging, IMarconeEntity, new()
{
public bool IsNew{get;set;}
}

In another class, I have an IList<object> collection which contains classes
derived from Entity<TEntity>.

foreach (object item in this.GetChangeSet().Updates) //
GetChangeSet().Updates return IList<object>
{
// I need to set the IsNew property for each of these items.
// How can I cast to the generic type without knowing the TEntity
type?
}


Thanks in advance,
Syntax
 
A

amdrit

You can define your property in the IMacroneEntity interface, or in your
abstract Entity

foreach(IMarconeEntity item in this.GetChangeSet().Updates)
{
Item.IsNew = true;
}

foreach(Entity<T> item in this.GetChangeSet().Updates)
{
Item.IsNew = true;
}
 
P

Peter Duniho

I have a base class Entity<TEntity> as shown below:

public abstract class Entity<TEntity> : IMarconeEntity, ICloneable
where TEntity : class, INotifyPropertyChanged,
INotifyPropertyChanging, IMarconeEntity, new()
{
public bool IsNew{get;set;}
}

In another class, I have an IList<object> collection which contains
classes derived from Entity<TEntity>.

foreach (object item in this.GetChangeSet().Updates) //
GetChangeSet().Updates return IList<object>
{
// I need to set the IsNew property for each of these items.
// How can I cast to the generic type without knowing the
TEntity type?
}

You can't, not easily. But you shouldn't need to either. Using
IList<object> is hardly better than just using IList or, for example, an
ArrayList.

If you have an abstract generic class with members that don't depend on
the type parameter, you are probably better off making a non-generic base
class with those members, and then another abstract generic class that
inherits that non-generic base class. Then when you need the members that
don't depend on the type parameter, just cast to the non-generic base
class.

Alternatively, change the code that uses the generic class so that there
is a useful type parameter available to tell you the type. Your generic
class is much more useful as a generic class if you do that anyway. Why
go to the trouble of creating a generic class and then erase the type
information by storing instances in a context where they need to be cast
back again?

If you really want to shoe-horn the code you've got now, you can use
reflection to get at the property directly. That's probably easier than
using reflection to access the type information and do some casting or
other gymnastics to get at the property. But I'd really advise working
with the language instead of trying to force your existing design onto it.

Pete
 
S

SyntaxError

The IList<object> collection is returned by the LINQ to SQL DataContext.

Implementing the class through the IMarcone interface, as suggested by
amdrit, will solve this situation.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top