Repository navigation
PackageManager load additional version without App.config #64
Copy link
Copy link
Open
Labels
Description
Activity
This should already be possible since
PackageManagerhas its instance publicly available i.e.var additionalVersion = new Hl7Package(...); PackageManager.Instance.Packages.Add(additionalVersion); // or PackageManager.Instance.Packages.Insert(7, additionalVersion);
Actually, this would only be possible if
PackageManagerwas public which currently it isn't.
This is probably one for the future when we tackle adding configuration as code.Reacted by Marco Pagliaripublic static class ReflectionExtensions { public static void AddHl7Package(this Assembly assembly, string packageName, string version) { // Get the PackageManager type var packageManagerType = assembly.GetType("NHapi.Base.PackageManager"); if (packageManagerType == null) throw new InvalidOperationException("PackageManager type not found in the assembly."); // Get the singleton instance of PackageManager var instanceProperty = packageManagerType.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static); var packageManagerInstance = instanceProperty?.GetValue(null); if (packageManagerInstance == null) throw new InvalidOperationException("Unable to retrieve PackageManager instance."); // Get the Hl7Package type var hl7PackageType = assembly.GetType("NHapi.Base.Hl7Package"); if (hl7PackageType == null) throw new InvalidOperationException("Hl7Package type not found in the assembly."); // Get the Packages property var packagesProperty = packageManagerInstance.GetType().GetProperty("Packages", BindingFlags.Public | BindingFlags.Instance); var packages = packagesProperty?.GetValue(packageManagerInstance); if (packages == null) throw new InvalidOperationException("Unable to retrieve the Packages property."); // Get the constructor for Hl7Package var constructorInfo = hl7PackageType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new[] { typeof(string), typeof(string) }, null); if (constructorInfo == null) throw new InvalidOperationException("Constructor for Hl7Package not found."); // Create an instance of Hl7Package var hl7PackageInstance = constructorInfo.Invoke(new object[] { packageName, version }); // Add the new Hl7Package instance to the Packages list var addMethod = packages.GetType().GetMethod("Add", BindingFlags.Public | BindingFlags.Instance); if (addMethod == null) throw new InvalidOperationException("Add method not found on the Packages collection."); addMethod.Invoke(packages, new[] { hl7PackageInstance }); } }and to use it (I use it like this in .net8):
var assembly = Assembly.Load("nhapi.base"); assembly.AddHl7Package("your.assembly.that.conforms.to.convention.name", "2.5");Hope it helps somebody.
It should be possible to configure additional versions without the need to configure the App.config of the main executable (or assembly).