具有类约束的字段

我有一个具有类类型约束的泛型类型的类


定义看起来像这样


public class ConnectedRepository<TEntity> where TEntity : class

    {

然后我在以下方法中使用它:


 public void LoadData<T>() where T:class

        {

             var GenRep = new ConnectedRepository<T>(c);

            grdData.DataSource = GenRep.ToBindingList();

        }

我的问题是我需要在包含此方法的类中的其他地方使用 GenRep,因此我需要在此方法之外定义它,但我找不到正确的语法


我需要这样的东西:


ConnectedRepository<??> GenRep;

但无法找出正确的类型定义


任何帮助将非常感激


Code for my repository:

 public class ConnectedRepository<TEntity> where TEntity : class

    {

        internal DbContext _context;

        internal DbSet<TEntity> _dbSet;


        public ConnectedRepository(DbContext context)

        {

            _context = context;

            _dbSet = context.Set<TEntity>();

        }


        public IEnumerable<TEntity> ToBindingList()

        {

            _dbSet.Load();

            return _dbSet.Local.ToBindingList();

        }

        public void SaveChanges()

        {

            _context.SaveChanges();

        }


    }

这将从包含 gridview 的用户控件中使用


public partial class DataView : DevExpress.XtraEditors.XtraUserControl 

{

    TechnicalServicesContext c = new TechnicalServicesContext();



   /// <summary>

   /// 

   /// </summary>

    public DataView() 

    {

        InitializeComponent();


    }

    /// <summary>

    /// 

    /// </summary>

    /// <typeparam name="T"></typeparam>

    public void LoadData<T>() where T:class

    {

         var GenRep = new ConnectedRepository<T>(c);

        grdData.DataSource = GenRep.ToBindingList();


    }

   public void SaveData()

    {


    }



}

这是我想用于任何实体的可重复使用的显示


蛊毒传说
浏览 108回答 3
3回答

哈士奇WWW

您不应为每种新数据类型的存储库创建新实例。对于许多不同的数据类型,它将消耗大量内存。您可以将静态类包装器与泛型方法一起使用&nbsp;public static class ConnectedRepository&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private static DbContext _context;&nbsp; &nbsp; &nbsp; &nbsp; public static SetContext(DbContext context)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _context = context;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp;public static IEnumerable<TEntity> ToBindingList<TEntity>() :&nbsp; where TEntity : class&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dbSet = _context.Set<TEntity>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbSet.Load()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return dbSet.Local.ToBindingList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}

翻翻过去那场雪

我不确定你想要什么是完全清楚的。如果我猜对了,您想在周围类的一个对象中的多个位置使用具有相同基础类型的泛型 ConnectedRepository 的相同实例,那么我会说您需要制作依赖于它的类,而不仅仅是类的单个方法。public class ConnectedRepository<TEntity,T>&nbsp; &nbsp;where TEntity : class&nbsp; &nbsp;where T:class{&nbsp; &nbsp; ConnectedRepository<T> GenRep;&nbsp; &nbsp; public ConnectedRepository(int c)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; GenRep = new ConnectedRepository<T>(c); // now sure what c is, bt you could pass it to the constructor like this, if GenRep needs it&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;public void LoadData()&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; grdData.DataSource = GenRep.ToBindingList();&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;public void DoStuff()&nbsp; &nbsp; &nbsp;{&nbsp; // do something else with&nbsp; GenRep ?&nbsp; &nbsp; &nbsp;}&nbsp;}

慕慕森

将其作为私有存储在类中,例如private&nbsp;ConnectedRepository<T>&nbsp;_genRep;进一步看,你是说你想要一个 T 类型的泛型函数,而类头中没有提到 T?那是不允许的。泛型类操作的所有类型都必须在其头文件中声明,并且替换发生在编译时。
打开App,查看更多内容
随时随地看视频慕课网APP