Skip to content

PackageManager load additional version without App.config #64

Description

@lechuckcaptain

It should be possible to configure additional versions without the need to configure the App.config of the main executable (or assembly).

Activity

  1. milkshakeuk commented on Apr 12, 2021

    @milkshakeuk
    Member

    This should already be possible since PackageManager has 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 PackageManager was public which currently it isn't.
    This is probably one for the future when we tackle adding configuration as code.

  2. franjom commented on Jan 23, 2025

    @franjom
    public 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions