Rock.Reflection.SearchAssembly C# (CSharp) Méthode

SearchAssembly() public static méthode

Searches the assembly.
public static SearchAssembly ( Assembly assembly, Type baseType ) : Type>.Dictionary
assembly System.Reflection.Assembly The assembly.
baseType System.Type Type of the base.
Résultat Type>.Dictionary
        public static Dictionary<string, Type> SearchAssembly( Assembly assembly, Type baseType )
        {
            Dictionary<string, Type> types = new Dictionary<string, Type>();

            try
            {
                foreach ( Type type in assembly.GetTypes() )
                {
                    if ( !type.IsAbstract )
                    {
                        if ( baseType.IsInterface )
                        {
                            foreach ( Type typeInterface in type.GetInterfaces() )
                            {
                                if ( typeInterface == baseType )
                                {
                                    types.Add( type.FullName, type );
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Type parentType = type.BaseType;
                            while ( parentType != null )
                            {
                                if ( parentType == baseType )
                                {
                                    types.Add( type.FullName, type );
                                    break;
                                }
                                parentType = parentType.BaseType;
                            }
                        }
                    }
                }
            }
            catch ( ReflectionTypeLoadException ex )
            {
                string dd = ex.Message;
            }

            return types;
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Gets the appropriate DbContext based on the entity type
        /// </summary>
        /// <param name="entityType">Type of the Entity.</param>
        /// <returns></returns>
        public static System.Data.Entity.DbContext GetDbContextForEntityType(Type entityType)
        {
            Type contextType = typeof(Rock.Data.RockContext);

            if (entityType.Assembly != contextType.Assembly)
            {
                var contextTypeLookup = Reflection.SearchAssembly(entityType.Assembly, typeof(System.Data.Entity.DbContext));

                if (contextTypeLookup.Any())
                {
                    contextType = contextTypeLookup.First().Value;
                }
            }

            System.Data.Entity.DbContext dbContext = Activator.CreateInstance(contextType) as System.Data.Entity.DbContext;
            return(dbContext);
        }
All Usage Examples Of Rock.Reflection::SearchAssembly