Skip to content

[API Proposal]: Assembly.LoadFrom(myassembly.cs) #122707

@lakani

Description

@lakani

Background and motivation

It would be nice to load Assembly from just a .cs file, this would easily allow new kind of add-ons to be developed,

Assembly.LoadFrom(myassembly.cs)

API Proposal

Assembly.LoadFrom(myassembly.cs)

API Usage

using System;
using System.Reflection;

// Assume you have an interface defined in a shared project
public interface IPlugin
{
void Run();
}

// In your main application code:
string assemblyPath = "C:\path\to\MyLibrary.dll";
string fullClassName = "MyNamespace.MyClass"; // Full name including namespace

try
{
// Load the assembly from the file path
Assembly assembly = Assembly.LoadFrom(assemblyPath);

// Get the Type information for the specific class
Type type = assembly.GetType(fullClassName);

if (type != null && typeof(IPlugin).IsAssignableFrom(type))
{
    // Create an instance of the class
    object instance = Activator.CreateInstance(type);

    // Cast it to the shared interface and use it
    IPlugin plugin = (IPlugin)instance;
    plugin.Run();
}
else
{
    Console.WriteLine($"Class {fullClassName} not found or does not implement IPlugin.");
}

}
catch (Exception ex)
{
Console.WriteLine($"Error loading assembly or creating instance: {ex.Message}");
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions