Generics 将反射对象强制转换为反射的通用接口
我正在从事一个基于.NET4.5、EF5和GenericRepository.EntityFramework的项目。我正在尝试创建一个发布服务,该服务将对专门用于创作和移动的数据库中的记录执行一些操作 第一个问题是将我的类型强制转换为Generics 将反射对象强制转换为反射的通用接口,generics,reflection,entity-framework-5,.net-4.5,entityframework.extended,Generics,Reflection,Entity Framework 5,.net 4.5,Entityframework.extended,我正在从事一个基于.NET4.5、EF5和GenericRepository.EntityFramework的项目。我正在尝试创建一个发布服务,该服务将对专门用于创作和移动的数据库中的记录执行一些操作 第一个问题是将我的类型强制转换为(IBaseRepository)失败。 我成功地使用第一个存储库(我在下面的示例DataBucket类中列出的存储库)进行了测试,方法是使用 object myRepository = p.GetValue(entity, null); IBaseReposi
(IBaseRepository)
失败。
我成功地使用第一个存储库(我在下面的示例DataBucket类中列出的存储库)进行了测试,方法是使用
object myRepository = p.GetValue(entity, null);
IBaseRepository<MRClassification> risk = myRepository;
出版物应用程序代码
public static void DeactivateNonTransversableExpiredRecords()
{
IEnumerable<Tuple<IBaseRepository<BaseEntity>, Type>> nonTransversableList =
GetNonTransversableRepositories(Databucket);
foreach (var repository in nonTransversableList)
{
////IQueryable recordsToExpire =
////((IBaseRepository<BaseEntity>)repository).FindBy(
//// p => Convert.ToDateTime(p.ExpirationDate).Date <= DateTime.Now.Date && p.IsActive);
IQueryable recordsToExpire = repository.Item1.FindBy(p => p.IsActive);
foreach (var row in recordsToExpire)
{
((BaseEntity)row).IsActive = false;
((IBaseRepository<BaseEntity>)repository).Edit((BaseEntity)row);
}
}
}
private static IEnumerable<Tuple<IBaseRepository<BaseEntity>, Type>> GetNonTransversableRepositories(object entity)
{
PropertyInfo[] properties =
entity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
List<Tuple<IBaseRepository<BaseEntity>, Type>> list = new List<Tuple<IBaseRepository<BaseEntity>, Type>>();
foreach (PropertyInfo p in properties)
{
if (!typeof(ITransversableRepository).IsAssignableFrom(p.PropertyType))
{
if (OpenGenericIsAssignableFrom(typeof(IBaseRepository<>), p.PropertyType))
{
list.Add(
new Tuple<IBaseRepository<BaseEntity>, Type>(
(IBaseRepository<BaseEntity>)Activator.CreateInstance(p.PropertyType),
p.GetValue(entity, null)));
}
}
}
return list;
}
公共静态无效停用OnTransversableExpiredRecords()
{
IEnumerable不可转换列表=
GetNonTransversableRepositories(Databucket);
foreach(非TransversableList中的var存储库)
{
////IQueryable可记录文件过期=
////((IBaseRepository)存储库)(
////p=>Convert.ToDateTime(p.ExpirationDate).Date p.IsActive);
foreach(recordsToExpire中的var行)
{
((BaseEntity)行).IsActive=false;
((IBaseRepository)存储库)。编辑((BaseEntity)行);
}
}
}
私有静态IEnumerable GetNonTransversableRepositories(对象实体)
{
PropertyInfo[]属性=
entity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
列表=新列表();
foreach(PropertyInfo p in properties)
{
如果(!typeof(ITransversableRepository).IsAssignableFrom(p.PropertyType))
{
if(openGenericsAssignableFrom(typeof(IBaseRepository),p.PropertyType))
{
列表。添加(
新元组(
(IBaseRepository)Activator.CreateInstance(p.PropertyType),
p、 GetValue(实体,null));
}
}
}
退货清单;
}
数据模型解决方案中的POCO实体和存储库
--- DataBucket
public class DataBucket : Audit, IDataBucket
{
private IMRClassificationRepository mrClassCodeRepository;
public IMRClassificationRepository MRClassificationRepository
{
get { return this.mrClassCodeRepository ?? (this.mrClassCodeRepository = new MRClassificationRepository(this.dataContext)); }
}
...
}
--- Repository Class Headers
public class MRClassificationRepository : BaseRepository<MRClassification>, IMRClassificationRepository
{
...
}
public interface IMRClassificationRepository : IBaseRepository<MRClassification>
{
...
}
public abstract class BaseRepository<T> : CoreRepository<T>, IBaseRepository<T> where T : BaseEntity, IBaseEntity
{
...
}
public interface IBaseRepository<T> : ICoreRepository<T> where T : BaseEntity
{
...
}
--- Entity Class Headers
public class MRClassification : RiskClassification
{
...
}
public abstract class RiskClassification : Revisionable
{
...
}
public abstract class Revisionable : BaseEntity, IRevisionableEntity
{
...
}
public class BaseEntity : IBaseEntity
{
...
}
public interface IBaseEntity : IEntity<Guid>
{
...
}
--DataBucket
公共类DataBucket:Audit,IDataBucket
{
私有IMRClassificationRepository mrClassCodeRepository;
公共IMRClassificationRepository MRClassificationRepository
{
获取{返回this.mrClassCodeRepository??(this.mrClassCodeRepository=new MRClassificationRepository(this.dataContext));}
}
...
}
---存储库类标题
公共类MRClassificationRepository:BaseRepository、IMRCClassificationRepository
{
...
}
公共接口IMRClassificationRepository:iBaserRepository
{
...
}
公共抽象类BaseRepository:CoreRepository,IBaseRepository其中T:BaseEntity,IBaseEntity
{
...
}
公共接口IBaseRepository:ICoreRepository,其中T:BaseEntity
{
...
}
---实体类标题
公共分类:风险分类
{
...
}
公共抽象类风险分类:可修改
{
...
}
可修改的公共抽象类:BaseEntity、IRevisionableEntity
{
...
}
公共类BaseEntity:IBaseEntity
{
...
}
公共接口IBaseEntity:通用性
{
...
}
--- DataBucket
public class DataBucket : Audit, IDataBucket
{
private IMRClassificationRepository mrClassCodeRepository;
public IMRClassificationRepository MRClassificationRepository
{
get { return this.mrClassCodeRepository ?? (this.mrClassCodeRepository = new MRClassificationRepository(this.dataContext)); }
}
...
}
--- Repository Class Headers
public class MRClassificationRepository : BaseRepository<MRClassification>, IMRClassificationRepository
{
...
}
public interface IMRClassificationRepository : IBaseRepository<MRClassification>
{
...
}
public abstract class BaseRepository<T> : CoreRepository<T>, IBaseRepository<T> where T : BaseEntity, IBaseEntity
{
...
}
public interface IBaseRepository<T> : ICoreRepository<T> where T : BaseEntity
{
...
}
--- Entity Class Headers
public class MRClassification : RiskClassification
{
...
}
public abstract class RiskClassification : Revisionable
{
...
}
public abstract class Revisionable : BaseEntity, IRevisionableEntity
{
...
}
public class BaseEntity : IBaseEntity
{
...
}
public interface IBaseEntity : IEntity<Guid>
{
...
}