public static void CheckCryptoLibary(this IBaseOnvifService s) { var msg = string.Empty; var cryptoIsAvailable = true; try { CheckCryptoLibary(); } catch (AssertException e) { cryptoIsAvailable = false; msg = e.Message; } s.Test.Assert(cryptoIsAvailable, msg, "Check crypto library is available"); }
//Implement an extension method "InitializeService" for derived interface and it will be called by BaseOnvifTest.RunTest at the start of the test public static void GeneralInitialize(this IBaseOnvifService s) { var Current = s.GetImplementedOnvifServices(); Func <Assembly, Type[]> typesRetriever = assembly => { //If BouncyCastle.Crypto.dll is absent then GetAssemblies().SelectMany() will fail. try { return(assembly.GetTypes()); } catch (Exception) { return(new Type[0]); } }; var allExtensionClasses = AppDomain.CurrentDomain.GetAssemblies().SelectMany(typesRetriever).Where(T => null != T.GetCustomAttribute(typeof(ExtensionAttribute))); var allExtensionMethods = allExtensionClasses.SelectMany(t => t.GetMethods()).Where(m => null != m.GetCustomAttribute <ExtensionAttribute>()); var allServiceInitializationMethods = allExtensionMethods.Where(m => "InitializeService" == m.Name); var currentServiceInitializationMethods = allServiceInitializationMethods.Where(m => m.GetParameters().First().ParameterType.GetInterfaces().Any(i => i.IsGenericType && Current.Any(e => e.IsAssignableFrom(i)))); Func <Type, int> orderBy = type => null != type.GetCustomAttribute <OnvifServiceAttribute>() ? (int)type.GetCustomAttribute <OnvifServiceAttribute>().InitializationPriority : int.MaxValue; try { foreach (var method in currentServiceInitializationMethods.OrderBy(e => orderBy(e.GetParameters().First().ParameterType))) { method.Invoke(null, new object[] { s }); } } catch (TargetInvocationException e) { if (e.InnerException is AssertException || e.InnerException is AccessDeniedException || e.InnerException is FaultException) { throw e.InnerException; } throw; } }
internal static IEnumerable <Type> GetImplementedOnvifServices(this IBaseOnvifService s) { var Base = typeof(IBaseOnvifService2 <,>); return(s.GetType().GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == Base)); }