Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PackageManager load additional version without App.config #64

Open
lechuckcaptain opened this issue Oct 26, 2016 · 2 comments
Open

PackageManager load additional version without App.config #64

lechuckcaptain opened this issue Oct 26, 2016 · 2 comments

Comments

@lechuckcaptain
Copy link

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

@milkshakeuk
Copy link
Member

milkshakeuk commented Apr 12, 2021

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.

@franjom
Copy link

franjom commented Jan 23, 2025

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
Projects
None yet
Development

No branches or pull requests

3 participants