Exemple #1
0
        /// <summary>
        /// Определяет реальный тип инстанцирования
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rtcls"></param>
        /// <returns></returns>
        protected virtual BSharpActivationType DetectActivationtType <T>(IBSharpRuntimeClass rtcls)
        {
            if (null == rtcls.RuntimeDescriptor)
            {
                return(BSharpActivationType.Client);
            }
            if (
                RuntimeClassResolutionType.NotDefined == rtcls.RuntimeDescriptor.ResolutionType
                ||
                RuntimeClassResolutionType.NotResolved == rtcls.RuntimeDescriptor.ResolutionType
                )
            {
                return(BSharpActivationType.Client);
            }
            if (typeof(object) == typeof(T))
            {
                return(BSharpActivationType.Configured);
            }
            var resolvedType = rtcls.RuntimeDescriptor.GetActualType();

            if (null == resolvedType)
            {
                return(BSharpActivationType.Client);
            }

            if (typeof(T).IsAssignableFrom(resolvedType))
            {
                return(BSharpActivationType.Configured);
            }

            return(BSharpActivationType.Client);
        }
Exemple #2
0
        private T ActivateConfiguredType <T>(IBSharpRuntimeClass rtcls) where T : class
        {
            var result = rtcls.Create();

            if (!(result is T))
            {
                throw new BSharpRuntimeException("cannot convert actual type " + result.GetType() + " to expected " + typeof(T));
            }
            return((T)result);
        }
Exemple #3
0
        private static bool GetCanActivateConfigured <T>(IBSharpRuntimeClass rtcls)
        {
            if (null == rtcls.RuntimeDescriptor)
            {
                return(false);
            }
            var resolvedType = rtcls.RuntimeDescriptor.GetActualType();

            return(null != resolvedType && typeof(T).IsAssignableFrom(resolvedType));
        }
Exemple #4
0
        private T ActivateClientType <T>(IBSharpRuntimeClass rtcls)  where T : class
        {
            var instance = typeof(T).IsInterface ? ResolveService <T>() : Activator.CreateInstance <T>();
            var bound    = instance as IBSharpRuntimeBound;

            if (null != bound)
            {
                bound.Initialize(rtcls);
            }
            return(instance);
        }
Exemple #5
0
        /// <summary>
        ///     Создать типизированный объект из динамического объекта BSharp
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Activate <T>(IBSharpRuntimeClass runtimeclass, BSharpActivationType activationType) where T : class
        {
            IBSharpRuntimeActivatorService activator = Activators
                                                       .OrderBy(_ => _.Index)
                                                       .FirstOrDefault(_ => _.CanActivate <T>(runtimeclass, activationType));

            if (null == activator)
            {
                throw new BSharpRuntimeException("cannot find activator for " + runtimeclass.Fullname +
                                                 " to create requested service " + typeof(T).Name);
            }

            return(activator.Activate <T>(runtimeclass, activationType));
        }
Exemple #6
0
 /// <summary>
 ///     Возвращает исходное определение класса BSharp
 /// </summary>
 /// <param name="fullname"></param>
 /// <returns></returns>
 public IBSharpRuntimeClass GetRuntimeClass(string fullname)
 {
     lock (this) {
         IBSharpRuntimeClass result = null;
         foreach (IBSharpRuntimeProvider p in Providers)
         {
             result = p.GetRuntimeClass(fullname);
             if (null != result)
             {
                 break;
             }
         }
         return(result);
     }
 }
Exemple #7
0
 /// <summary>
 ///     Проверяет поддержку сериализации
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public bool CanActivate <T>(IBSharpRuntimeClass rtcls, BSharpActivationType activationType = BSharpActivationType.Auto) where T : class
 {
     if (BSharpActivationType.Auto == activationType)
     {
         activationType = DetectActivationtType <T>(rtcls);
     }
     if (BSharpActivationType.Client == activationType)
     {
         return(GetCanActivateClient <T>());
     }
     if (BSharpActivationType.Configured == activationType)
     {
         return(GetCanActivateConfigured <T>(rtcls));
     }
     throw new Exception("invalid activation type " + activationType);
 }
Exemple #8
0
        /// <summary>
        ///     Создать типизированный объект из динамического объекта BSharp
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Activate <T>(IBSharpRuntimeClass rtcls, BSharpActivationType activationType = BSharpActivationType.Auto) where T : class
        {
            if (activationType == BSharpActivationType.Auto)
            {
                activationType = DetectActivationtType <T>(rtcls);
            }
            if (!CanActivate <T>(rtcls, activationType))
            {
                return(default(T));
            }
            switch (activationType)
            {
            case BSharpActivationType.Client:
                return(ActivateClientType <T>(rtcls));

            case BSharpActivationType.Configured:
                return(ActivateConfiguredType <T>(rtcls));
            }
            return(default(T));
        }
 public void Initialize(IBSharpRuntimeClass cls)
 {
     Cls = cls;
 }