/// <summary> /// Gets the type of the i entity for entity. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="guid">The unique identifier.</param> /// <returns></returns> public static Rock.Data.IEntity GetIEntityForEntityType(Type entityType, Guid guid) { var dbContext = Reflection.GetDbContextForEntityType(entityType); Rock.Data.IService serviceInstance = Reflection.GetServiceForEntityType(entityType, dbContext); if (serviceInstance != null) { System.Reflection.MethodInfo getMethod = serviceInstance.GetType().GetMethod("Get", new Type[] { typeof(Guid) }); return(getMethod.Invoke(serviceInstance, new object[] { guid }) as Rock.Data.IEntity); } return(null); }
/// <summary> /// Gets the appropriate Rock.Data.IService based on the entity type /// </summary> /// <param name="entityType">Type of the Entity.</param> /// <param name="dbContext">The database context.</param> /// <returns></returns> public static Rock.Data.IService GetServiceForEntityType(Type entityType, System.Data.Entity.DbContext dbContext) { Type serviceType = typeof(Rock.Data.Service <>); if (entityType.Assembly != serviceType.Assembly) { var serviceTypeLookup = Reflection.SearchAssembly(entityType.Assembly, serviceType); if (serviceTypeLookup.Any()) { serviceType = serviceTypeLookup.First().Value; } } Type service = serviceType.MakeGenericType(new Type[] { entityType }); Rock.Data.IService serviceInstance = Activator.CreateInstance(service, dbContext) as Rock.Data.IService; return(serviceInstance); }