【问题标题】:Getting type from a Collectiontype with an abstract class使用抽象类从 Collectiontype 获取类型
【发布时间】:2014-05-30 12:05:43
【问题描述】:

我编写了一个通用服务,它将完成我所有的工作,将我的模型保存在数据库中。 现在,当我有一个包含模型列表的模型时,我遇到了问题。

我无法获取列表项的抽象类型。

有什么想法吗?

public abstract class AbstractEntity
{
    [Key]
    public virtual int Id
    {
        get;
        set;
    }
}


public class Invoice : AbstractEntity
{


    public int ContactId
    {
        get;
        set;
    }
    public virtual Contact ContactInformation
    {
        get;
        set;
    }

    public virtual ObservableCollection<Position> Positions
    {
        get;
        set;
    }

    public DateTime? BillingDate
    {
        get;
        set;
    }

    public string Number
    {
        get;
        set;
    }

    public double Sum
    {
        get;
        set;
    }

}

public abstract class AbstractService<T>: IDisposable where T : AbstractEntity
{
    #region Fields
    protected Context _context;
    #endregion

    public virtual void Add(params T[] items)
    {
        foreach (T item in items)
        {
            SetState(item, EntityState.Added);
        }        
    }

    public virtual void Update(params T[] items)
    {
        foreach (T item in items)
        {                
            SetState(item, EntityState.Modified);
        }         
    }

    public virtual void Remove(params T[] items)
    {
        foreach (T item in items)
        {
            SetState(item, EntityState.Deleted);
        }            
    }


    public int SaveChanges()
    {
        return _context.SaveChanges();
    }

    public void Dispose()
    {
        _context.Dispose();
    }

    #endregion

    #region Private Methodes
    private void SetState(AbstractEntity Object, EntityState state)
    {

        if (Object.Id != null)
            _context.Entry(Object).State = state;
        else
            _context.Entry(Object).State = EntityState.Added;

        Type objType = Object.GetType();
        PropertyInfo[] properties = objType.GetProperties();

        foreach (var prop in properties)
        {
            if ((prop.PropertyType.BaseType == typeof(AbstractEntity)))
            {
                var entity = prop.GetValue(Object, null);
                if (entity is AbstractEntity)
                    SetState((AbstractEntity)entity, state);
            } else
            if (prop.PropertyType.BaseType == typeof(Collection<???>)) <=== Problem here
            {
                var entity = prop.GetValue(Object, null);
                foreach (var pos in (Collection<???>)entity) <=== and here
                {
                    SetState(pos, state);
                }
            }
        }
    }
    #endregion
}

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    您可以检查该对象是否正在实现ICollection 接口。

    此问题中的更多信息:How to determine if a type is a type of collection?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-11
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      相关资源
      最近更新 更多