Пример #1
0
        /// <summary>
        /// Returns the DbContext type given the <see cref="DomainService"/> type. Uses late binding so as to avoid adding a reference to EF 4.1.
        /// </summary>
        /// <param name="domainServiceType">The type of the domain service.</param>
        /// <returns>The type of the DbContext.</returns>
        public static Type GetDbContextType(Type domainServiceType)
        {
            Type dbContextType = null;
            Type currType      = domainServiceType;

            while (currType != null && currType != typeof(object) && currType != typeof(DomainService))
            {
                if (currType.IsGenericType && DbContextUtilities.CompareWithSystemType(currType, DbContextUtilities.DbDomainServiceTypeName))
                {
                    Type[] typeArgs = currType.GetGenericArguments();
                    if (typeArgs != null && typeArgs.Count() > 0)
                    {
                        dbContextType = typeArgs[0];
                    }
                    break;
                }
                currType = currType.BaseType;
            }
            return(dbContextType);
        }
Пример #2
0
 /// <summary>
 /// Finds the reference to System.Data.Entity.DbContext type given the contextType, if it exists.
 /// </summary>
 /// <param name="contextType">The context type from which the DbContext type reference is to be found</param>
 /// <returns>The reference to DbContext type.</returns>
 public static Type GetDbContextTypeReference(Type contextType)
 {
     if (DbContextUtilities._dbContextTypeReference == null)
     {
         // if contextType is an interface or a value type, then we know it is not a DbContext type.
         if (!contextType.IsInterface && !contextType.IsValueType)
         {
             Type t = contextType;
             // If the type if null or object, then it is not the DbContext type.
             // We need to check for null as well, since Walking an interface hierarchy does not lead to Object.
             while (t != null && t != typeof(object))
             {
                 if (DbContextUtilities.CompareWithSystemType(t, DbContextUtilities.DbContextTypeName))
                 {
                     DbContextUtilities._dbContextTypeReference = t;
                     break;
                 }
                 t = t.BaseType;
             }
         }
     }
     return(DbContextUtilities._dbContextTypeReference);
 }