Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing AppDomain in Net Core

So, I am new in C# and Net Core, and someone passed me code to an evaluate command. However, it wasn't made in Net Core, so, the AppDomain doesn't exist in Net Core,what should I do?

ScriptOptions so = ScriptOptions.Default;

            so = so.WithImports(new string[] { "System.Math", "System.Diagnostics", "System.Runtime.InteropServices",
            "Discord.WebSocket", "Discord.Commands", "Discord", "System.Threading.Tasks", "System.Linq", "System", "System.Collections.Generic", "System.Convert", "System.Reflection"});
            so = so.WithReferences(AppDomain.CurrentDomain.GetAssemblies().Where(xa => !xa.IsDynamic && !string.IsNullOrWhiteSpace(xa.Location)));
like image 292
Animadoria Avatar asked Dec 19 '25 22:12

Animadoria


1 Answers

There is no AppDoamain in .net core you can use Microsoft.Extensions.DependencyModel library to achieve what you need.

Below is code from an article. You can see full details there.

return GetReferencingLibraries("Specify")
.SelectMany(assembly => assembly.ExportedTypes);

public static IEnumerable<Assembly> GetReferencingAssemblies(string assemblyName)
{
    var assemblies = new List<Assembly>();
    var dependencies = DependencyContext.Default.RuntimeLibraries;
    foreach (var library in dependencies)
    {
        if (IsCandidateLibrary(library, assemblyName))
        {
            var assembly = Assembly.Load(new AssemblyName(library.Name));
            assemblies.Add(assembly);
        }
    }
    return assemblies;
}

private static bool IsCandidateLibrary(RuntimeLibrary library, assemblyName)
{
    return library.Name == (assemblyName)
        || library.Dependencies.Any(d => d.Name.StartsWith(assemblyName));
}

You will need to include Microsoft.Extensions.DependencyModel package to use above code.

like image 84
Mairaj Ahmad Avatar answered Dec 22 '25 12:12

Mairaj Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!