Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit e4bf7e1

Browse files
committed
Only enable MEF services for named process
When current process is different to named process, set the MEF export's contract name to "GitHub.Disabled". Since this contract name isn't imported, the service is effectively disabled.
1 parent d6e8c22 commit e4bf7e1

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/GitHub.Exports/Exports/ExportForProcess.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ namespace GitHub.Exports
1414
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
1515
public sealed class ExportForProcessAttribute : ExportAttribute
1616
{
17+
// Unique name for exports that have been disabled
18+
const string DisabledContractName = "GitHub.Disabled";
19+
1720
/// <summary>
1821
/// Define an export that is only exposed in a specific named process.
1922
/// </summary>
2023
/// <param name="contractType">The contract type to expose.</param>
2124
/// <param name="processName">Name of the process to expose export from (e.g. 'devenv').</param>
22-
public ExportForProcessAttribute(Type contractType, string processName) : base(ExportForProcess(contractType, processName))
25+
public ExportForProcessAttribute(Type contractType, string processName) :
26+
base(ContractNameForProcess(contractType, processName), contractType)
2327
{
2428
ProcessName = processName;
2529
}
2630

27-
static Type ExportForProcess(Type contractType, string processName)
31+
static string ContractNameForProcess(Type contractType, string processName)
2832
{
29-
return Process.GetCurrentProcess().ProcessName == processName ? contractType : null;
33+
var enabled = Process.GetCurrentProcess().ProcessName == processName;
34+
return enabled ? null : DisabledContractName;
3035
}
3136

3237
/// <summary>

0 commit comments

Comments
 (0)