/// <summary> /// Creates a named class registration of a static platform service. /// </summary> /// <param name="type">Type to register</param> /// <param name="name">Name of registration</param> public static void RegisterStaticPlatformService(Type type, string name) { EnsureInitialized(); // This is a sufficient check since an abstract class cannot be sealed or static in C# if (!(type.IsAbstract && type.IsSealed)) { throw new ArgumentException("The provided type is not a static class.", nameof(type)); } object[] attributes = type.GetCustomAttributes(typeof(PlatformServiceAttribute), false); if (attributes.Length == 0) { throw new ArgumentException("Service class must be decorated with PlatformService attribute.", nameof(type)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Registration name cannot be null or empty.", nameof(name)); } // Right now use the default context string contextName = string.Empty; ContextInfo contextObj = _contexts.GetOrAdd(contextName, s => new ContextInfo(s)); ServiceInfo service = new ServiceInfo(type, name); if (!contextObj.TryAddService(service)) { throw new ArgumentException("A service has already been registered with the same name.", nameof(name)); } }
/// <summary> /// Creates a named class registration of a platform service instance. /// </summary> /// <param name="instance">Instance to register</param> /// <param name="name">Name of registration</param> public static void RegisterPlatformService(object instance, string name) { EnsureInitialized(); if (instance == null) { throw new ArgumentNullException(nameof(instance)); } Type type = instance.GetType(); if (!type.IsValidPlatformService()) { throw new ArgumentException("Instance does not represent a valid platform service.", nameof(instance)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Registration name cannot be null or empty.", nameof(name)); } // Right now use the default context string contextName = string.Empty; ContextInfo contextObj = _contexts.GetOrAdd(contextName, s => new ContextInfo(s)); ServiceInfo service = new ServiceInfo(type, name, instance); if (!contextObj.TryAddService(service)) { throw new ArgumentException("A service has already been registered with the same name.", nameof(name)); } service.SubscribeToEvents(); }