/// <summary> /// Loads exported types from a given assembly /// </summary> /// <param name="assembly">The assembly to search for exported types</param> /// <param name="signedOnly">Only load exported types from this assembly if the assembly has been digitally signed</param> /// <param name="actionIfUnsigned">If only signed assemblies should be loaded, and this assembly is not signed, load /// exported types nonetheless if this action returns true</param> public static void LoadExports(Assembly assembly, bool signedOnly = false, Func <Assembly, bool> actionIfUnsigned = null) { if (signedOnly) { try { X509Certificate.CreateFromSignedFile(assembly.Location); } catch (CryptographicException e) { if (actionIfUnsigned == null || !actionIfUnsigned(assembly)) { return; } } } foreach (var type in assembly.GetExportedTypes()) { var custom = type.GetCustomAttributes(); foreach (var attrib in custom) { switch (attrib) { case ExportAttribute ea: ExportedTypes.Add(new ExportedType(type, ea.ExportedType, false)); break; case ExportManyAttribute ema: ExportedTypes.Add(new ExportedType(type, ema.ExportedType, true)); break; } } } }
public static void AddExportedType(Type actualType, Type exportedAsType, bool exportMany = false) { if (ExportedTypes.Any(x => x.ExposedType == exportedAsType)) { throw new Exception($"Type {exportedAsType.FullName} has already been exported"); } ExportedTypes.Add(new ExportedType(actualType, exportedAsType, exportMany)); }