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

Commit d46bd0d

Browse files
committed
Use reflection to instantiate VSGitExt implementations
We can't reference all of the GitHub.TeamFoundation.* projects at once, so instead we use reflection to retrieve and instantiate the appropriate VSGitExt implementation.
1 parent 3f4cf75 commit d46bd0d

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

src/GitHub.TeamFoundation.14/Services/VSGitExt.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@
1515

1616
namespace GitHub.VisualStudio.Base
1717
{
18-
// This is a workaround to avoid using reference aliases, which don't currently work with <PackageReference>.
19-
#if TEAMEXPLORER14
20-
public class VSGitExt14 : VSGitExt
21-
{
22-
public VSGitExt14(IServiceProvider serviceProvider, IGitService gitService) : base(serviceProvider, gitService) { }
23-
}
24-
#elif TEAMEXPLORER15
25-
public class VSGitExt15 : VSGitExt
26-
{
27-
public VSGitExt15(IServiceProvider serviceProvider, IGitService gitService) : base(serviceProvider, gitService) { }
28-
}
29-
#elif TEAMEXPLORER16
30-
public class VSGitExt16 : VSGitExt
31-
{
32-
public VSGitExt16(IServiceProvider serviceProvider, IGitService gitService) : base(serviceProvider, gitService) { }
33-
}
34-
#endif
35-
3618
/// <summary>
3719
/// This service acts as an always available version of <see cref="IGitExt"/>.
3820
/// </summary>
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using GitHub.Logging;
34
using GitHub.VisualStudio.Base;
45
using Serilog;
@@ -20,24 +21,18 @@ public VSGitExtFactory(int vsVersion, IServiceProvider serviceProvider, IGitServ
2021
this.gitService = gitService;
2122
}
2223

24+
// The GitHub.TeamFoundation.* assemblies target different .NET and Visual Studio versions.
25+
// We can't reference all of their projects directly, so instead we use reflection to retrieve
26+
// and instantiate the correct implementation.
2327
public IVSGitExt Create()
2428
{
25-
switch (vsVersion)
29+
if(Type.GetType($"GitHub.VisualStudio.Base.VSGitExt, GitHub.TeamFoundation.{vsVersion}", false) is Type type)
2630
{
27-
case 14:
28-
return Create(() => new VSGitExt14(serviceProvider, gitService));
29-
case 15:
30-
return Create(() => new VSGitExt15(serviceProvider, gitService));
31-
case 16:
32-
return Create(() => new VSGitExt16(serviceProvider, gitService));
33-
default:
34-
log.Error("There is no IVSGitExt implementation for DTE version {Version}", vsVersion);
35-
return null;
31+
return (IVSGitExt)Activator.CreateInstance(type, serviceProvider, gitService);
3632
}
37-
}
3833

39-
// NOTE: We're being careful to only reference VSGitExt14 and VSGitExt15 from inside a lambda expression.
40-
// This ensures that only the type that's compatible with the running DTE version is loaded.
41-
static IVSGitExt Create(Func<IVSGitExt> factory) => factory.Invoke();
34+
log.Error("There is no IVSGitExt implementation for DTE version {Version}", vsVersion);
35+
return null;
36+
}
4237
}
4338
}

0 commit comments

Comments
 (0)