首页 新闻 赞助 找找看

C#泛型的这种约束有什么用呢

0
[已解决问题] 解决于 2018-11-23 11:11

public class people
{
public int id { get; set; }
public string name { get; set; }
}


public class myclass<T> where T : people//对比下方的写法,此处的people有什么意义呢?
{
public T get(T t)
{
return t;
}
}

因为这样约束,只能传入people才行,为什么不直接写成这种形式呢
public people get(people p)
{
return p;
}

一禅·小和尚的主页 一禅·小和尚 | 小虾三级 | 园豆:519
提问于:2018-11-15 16:46
< >
分享
最佳答案
0

where T : people 泛型约束表示的是 T 可以用 people 或者 people 的子类

奖励园豆:5
dudu | 高人七级 |园豆:31075 | 2018-11-15 16:57
其他回答(4)
0

好好从语文上 理解 模版 和 约束 两个词语。
可以你说的那样写,甚至你写object、dynamic都可以。
你怎么能 让 其 具备 上面两个词的 意义呢 —— 我要调用子类函数,如果是object或者父类怎么办,是不是要妥妥的麻烦呢?至于约束就更不必提了,且约束有很多种约束,举个例子什么是约束 ——
意义:概念->工具->使用;
虽然有了手动档,但我设想要有一种手自一体的,于是我厂的车子就是手自一体的,你使用我厂的车子就这么用了。

花飘水流兮 | 园豆:13560 (专家六级) | 2018-11-15 23:43
0

你说的这个场景的确都可以,根据实际需求选择,使用泛型方法,调用端少了强转,语意会比较清楚。本来泛型就是语法糖而已。

holdengong | 园豆:212 (菜鸟二级) | 2018-11-16 08:17
0

约束你的 T,只能是某个类或者这个类的派生类。
比如 MyClass<T> 的约束为 Person。那么你写代码的时候 MyClass<Person> bbb = new MyClass<Person> 是可以的。但是你要写 MyClass<string> bbb 就会报错!

何旭 | 园豆:306 (菜鸟二级) | 2018-11-16 17:27
0

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using WebApiHost.Core;
using WebApiHost.Entities;

namespace WebApiHost.Repositories
{
[ExcludeFromCodeCoverage]
public abstract class RepositoryBase<T> where T : class
{
protected readonly IDbSet<T> dbset;
protected LoyaltyServiceContext dataContext;

    protected RepositoryBase(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory { get; }

    protected LoyaltyServiceContext DataContext
    {
        get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
    }

    public virtual void Add(T entity)
    {
        dbset.Add(entity);
    }

    public bool Exist(Expression<Func<T, bool>> where)
    {
        return dbset.AsQueryable().Any(where);
    }

    public virtual void AddRange(T[] entities)
    {
        DataContext.Set<T>().AddRange(entities);
    }
    public virtual void AddOrUpdateRange(T[] entities)
    {
        dbset.AddOrUpdate(entities);
    }

    public virtual IQueryable<T> AsQueryable()
    {
        return dbset.AsQueryable();
    }

    public bool RefreshEntityInContext(T entity)
    {
        var objContext = ((IObjectContextAdapter)dataContext).ObjectContext;
        var objSet = objContext.CreateObjectSet<T>();
        var entityKey = objContext.CreateEntityKey(objSet.EntitySet.Name, entity);

        object foundEntity;
        var exists = objContext.TryGetObjectByKey(entityKey, out foundEntity);

        if (exists)
        {
            objContext.Detach(foundEntity);
        }

        dbset.Attach(entity);

        return (exists);
    }


    public virtual void Update(T entity)
    {
        RefreshEntityInContext(entity);
        dbset.Attach(entity);
        dataContext.Entry(entity).State = EntityState.Modified;
    }

    public virtual void Delete(T entity)
    {
        dbset.Remove(entity);
    }

    public virtual void Delete(Expression<Func<T, bool>> where)
    {
        var objects = dbset.Where(where).AsEnumerable();
        foreach (var obj in objects)
            dbset.Remove(obj);
    }

    public virtual T GetById(int id)
    {
        return dbset.Find(id);
    }
    public virtual T GetById(Guid id)
    {
        return dbset.Find(id);
    }

    public virtual T GetById(string id)
    {
        return dbset.Find(id);
    }

    public virtual IEnumerable<T> GetAll()
    {
        return dbset.ToList();
    }

    public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).ToList();
    }

    public T Get(Expression<Func<T, bool>> where)
    {
        return dbset.FirstOrDefault(where);
    }

    public async Task<T> GetByIdAsync(int id)
    {
        return await dataContext.Set<T>().FindAsync(id);
    }
    public async Task<T> GetByIdAsync(string id)
    {
        return await dataContext.Set<T>().FindAsync(id);
    }

    public virtual async Task<T> GetAsync(Expression<Func<T, bool>> where)
    {
        return await dataContext.Set<T>().AsNoTracking().Where(where).SingleOrDefaultAsync();
    }

    public async Task<IList<T>> GetAllAsync()
    {
        return await dataContext.Set<T>().AsNoTracking().ToListAsync();
    }

    public virtual async Task<IList<T>> GetManyAsync(Expression<Func<T, bool>> where)
    {
        return await dataContext.Set<T>().AsNoTracking().Where(where).ToListAsync();
    }
}

}

chester·chen | 园豆:507 (小虾三级) | 2018-11-23 10:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册