【问题标题】:Using Abstract Classes in C#在 C# 中使用抽象类
【发布时间】:2011-09-06 13:40:21
【问题描述】:

我有一个抽象类,其他类正在继承它。

public abstract class GenericAccess<TEntity>
{
    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        //Your help need is here.
    }
}

public class Doors : GenericAccess<DoorEntity>
{

}

public class DoorEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

我需要创建一个通用方法,例如当我可以的时候

IList<DoorEntity> Doors.GetObjectListFromArray(int[] {1,2,3,4,5});

它将返回一个 IList,其中包含所有对象,其中属性 Id 加载了传递的值。在上面的示例中,它将返回一个列表,列表中包含 5 个项目,并加载了 DoorEntity 的 Id。

【问题讨论】:

  • 您可以使用 Dictionary 来执行此操作。
  • DoorEntity 对象是从哪里实现的?它们是否存储在 GenericAccess 对象中的某个位置?它们是从数据库中提取的吗?
  • DoorEntity 是使用 Nhibernate Fluent 从数据库中加载的。

标签: c# .net oop class


【解决方案1】:

我不确定这是否是你想要的,但这里是:

return from int id in Ids
       select new TEntity (id);

您必须更正 GenericAccess 类的定义以向泛型参数添加约束,如下所示:

public abstract class GenericAccess<TEntity> where TEntity : class, new

好的,根据你的 cmets...

使用带有 NHibernate 的 LINQ 来获取实体,类似于:

return from int id in Ids
       select Session.Query (...).Where (x => x.Id === id);

【讨论】:

    【解决方案2】:

    使用接口或基类...

    带接口:

    public abstract class GenericAccess<TEntity> where TEntity : IEntity, new()
    {
        public static IList<TEntity> GetObjectListFromArray(int[] IDs)
        {
            return IDs.Select(id => new TEntity { Id = id }).ToList();
        }
    }
    
    public class Doors : GenericAccess<DoorEntity>
    {
    
    }
    
    public class DoorEntity : IEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public interface IEntity
    {
        int Id { get; set; }
        string Name { get; set; }
    }
    

    带有基类:

    public abstract class GenericAccess<TEntity> where TEntity : Entity, new()
    {
        public static IList<TEntity> GetObjectListFromArray(int[] IDs)
        {
            return IDs.Select(id => new TEntity { Id = id }).ToList();
        }
    }
    
    public class Doors : GenericAccess<DoorEntity>
    {
    
    }
    
    public class DoorEntity : Entity
    {
    
    }
    
    public abstract class Entity
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

    • 请将您的示例放在答案中,而不是在链接后面!
    • 效果很好!完美的! - 看到你的解决方案真的很有趣!
    • 谢谢。我已将我的示例插入到答案中,而不是 Gist 链接,对此感到抱歉。
    【解决方案3】:

    你的函数可以是这样的

    public static IList<TEntity> GetObjectListFromArray(int[] IDs)
    {
        var r = new List<TEntity>();
        foreach (var item in IDs)
        {
            var obj = typeof(TEntity).Assembly.CreateInstance(typeof(TEntity).FullName);
            var p = obj.GetType().GetProperty("Id", System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            if (p != null)
            {
                p.SetValue(obj, item, null);
                var m = r.GetType().GetMethod("Add");
                m.Invoke(r, new object[] { obj });
            }
        }
        return r;
    }
    

    }

        IList<DoorEntity> r = Doors.GetObjectListFromArray(new int[] {1,2,3,4,5});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-28
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多