/// <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); }
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); }
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)); }
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); }
/// <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)); }
/// <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); } }
/// <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); }
/// <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; }