5

I have a generic class that looks like this:

public class DataServiceBase<T> : Screen where  T : EntityManager, new (){

    private T _entityManager;

    public T EntityManager {
        get {
            if (_entityManager == null)
            {
                _entityManager = new T();
            }
            return _entityManager;
        }
    }

Basically all I am trying to do is create an EntityManager of if it doesn't exist. This actually works fine. However, I need to modify this as T no longer has a parameterizerless constructor. And so I can't use this methodology at all.

But I do need the EntityManager strongly typed at the derived level of the DataService as different entity managers handled different entities.

I am not sure how to resolve this. One alternative I have tried is:

public DataServiceBase(EntityManager entityManager) {
        this._entityManager = entityManager;

    }

In other words, I pass it into the constructor, but now I no longer have the property strong typed.

Greg

4

1 回答 1

8

Just make the constructor argument take the generic type also

public DataServiceBase(T entityManager) {
    this._entityManager = entityManager;

}
于 2013-01-04T02:38:24.813 回答