/// <summary> /// Checks if the registry contains an implementation of a given interface for a given /// platform. /// </summary> /// <typeparam name="TInterface">The type of the interface needed.</typeparam> /// <param name="platform">The platform for which the interface must be implemented.</param> /// <returns>`true` if the interface is there, `false` otherwise.</returns> public static bool Has <TInterface>(Platform platform) where TInterface : class { Type ifaceType = typeof(TInterface); var platformImpl = new PlatformImplementation(platform, ifaceType); lock (s_implementations) { if (s_implementations.ContainsKey(platformImpl)) { return(true); } } return(s_defaultImplementations.ContainsKey(ifaceType)); }
private static void RegisterImplementationImplNoLock(Platform platform, Type ifaceType, object implementation) { var platformImpl = new PlatformImplementation(platform, ifaceType); object prevImpl = null; if (s_implementations.TryGetValue(platformImpl, out prevImpl)) { if (object.ReferenceEquals(prevImpl, implementation)) { return; } throw new InvalidOperationException($"There is already an implementation of interface {ifaceType} for platform {platform}. Cannot register {implementation.GetType().AssemblyQualifiedName}"); } else { s_implementations.Add(platformImpl, implementation); } }
/// <summary> /// Gets the implementation of a given interface for a given platform. If no implementation /// was defined for that platform, returns the default implementation instead, if one was /// defined. Does not throw an exception if no platform implementation is found. /// </summary> /// <typeparam name="TInterface">The type of the interface to get.</typeparam> /// <param name="platform">The platform whose implementation is requested.</param> /// <returns>The implementation of <typeparamref name="TInterface"/> for a given platform, or `null` if no such implementation or default implementation exists for that platform.</returns> public static TInterface Query <TInterface>(Platform platform) where TInterface : class { Type ifaceType = typeof(TInterface); var platformImpl = new PlatformImplementation(platform, ifaceType); object implObj = null; lock (s_implementations) { if (s_implementations.TryGetValue(platformImpl, out implObj)) { return((TInterface)implObj); } } if (s_defaultImplementations.TryGetValue(ifaceType, out implObj)) { return((TInterface)implObj); } else { return(null); } }
public PlatformImplementationDescriptor(PlatformImplementation impl, Type type) { Implementation = impl; ConcreteType = type; }
public static object GetImplementation(PlatformImplementation impl) { return(sImplementations.ContainsKey(impl) ? sImplementations[impl] : null); }
public static void RegisterImplementation(PlatformImplementation impl, object inst) { sImplementations[impl] = inst; }
public ImplementationOfAttribute(PlatformImplementation impl, string module) { Implementation = impl; ParentModule = module; }