예제 #1
0
        /// <summary>
        /// Gets an object instance from DbSet T
        /// </summary>
        /// <returns>The object from set.</returns>
        /// <param name="predicate">Predicate.</param>
        /// <param name="context">Context.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        private static T GetObjectFromSet <T>(Func <T, bool> predicate, VaporStoreDbContext context)
            where T : class, new()
        {
            var dbSetType = context.GetType()
                            .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                            .FirstOrDefault(p => p.PropertyType.IsAssignableFrom(typeof(DbSet <T>)));

            if (dbSetType == null)
            {
                throw new ArgumentException($"DbSet with type: {dbSetType.Name} does not exist!");
            }

            DbSet <T> set = (DbSet <T>)dbSetType
                            .GetMethod
                            .Invoke(context, null);

            T desiredObject = set
                              .FirstOrDefault(predicate);

            return(desiredObject);
        }