public TClass ActivateClass <TClass>([NotNull] string libraryPath) where TClass : NativeLibraryBase { var classType = typeof(TClass); // Check for remapping if (Options.HasFlagFast(EnableDllMapSupport)) { libraryPath = new DllMapResolver().MapLibraryName(classType, libraryPath); } return((TClass)ActivateClass(libraryPath, classType, classType.GetInterfaces())); }
public TInterface ActivateInterface <TInterface>([NotNull] string libraryPath) where TInterface : class { // Check for remapping if (Options.HasFlagFast(EnableDllMapSupport)) { libraryPath = new DllMapResolver().MapLibraryName(typeof(TInterface), libraryPath); } var anonymousInstance = ActivateClass <NativeLibraryBase, TInterface>(libraryPath); return(anonymousInstance as TInterface ?? throw new InvalidOperationException ( "The resulting instance was not convertible to an instance of the interface." )); }
protected MapResolverTestBase() { Resolver = new DllMapResolver(); }
public TClass ActivateClass <TClass, TInterface>([NotNull] string libraryPath) where TClass : NativeLibraryBase where TInterface : class { var classType = typeof(TClass); if (!classType.IsAbstract) { throw new ArgumentException("The class to activate must be abstract.", nameof(TClass)); } var interfaceType = typeof(TInterface); if (!interfaceType.IsInterface) { throw new ArgumentException ( "The interface to activate on the class must be an interface type.", nameof(TInterface) ); } // Check for remapping if (Options.HasFlagFast(EnableDllMapSupport)) { libraryPath = new DllMapResolver().MapLibraryName(interfaceType, libraryPath); } // Attempt to resolve a name or path for the given library var resolveResult = PathResolver.Resolve(libraryPath); if (!resolveResult.IsSuccess) { throw new FileNotFoundException ( $"The specified library (\"{libraryPath}\") was not found in any of the loader search paths.", libraryPath, resolveResult.Exception ); } libraryPath = resolveResult.Path; // Check if we've already generated a type for this configuration var key = new GeneratedImplementationTypeIdentifier(classType, interfaceType, libraryPath, Options); if (!TypeCache.TryGetValue(key, out var generatedType)) { lock (BuilderLock) { generatedType = GenerateInterfaceImplementationType <TClass, TInterface>(); TypeCache.TryAdd(key, generatedType); } } try { var anonymousInstance = CreateAnonymousImplementationInstance <TInterface> ( generatedType, libraryPath, Options, TransformerRepository ); return(anonymousInstance as TClass ?? throw new InvalidOperationException ( "The resulting instance was not convertible to an instance of the class." )); } catch (TargetInvocationException tex) { if (tex.InnerException is null) { throw; } // Unwrap target invocation exceptions, since we can fail in the constructor throw tex.InnerException; } }