3

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)));
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Animadoria
  • 31
  • 1
  • 2

1 Answers1

4

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.

Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • Just to clarify AppDomain hasn't vanished in entirety as [it is part of .Net Standard 2.0](https://stackoverflow.com/a/45600956/465053). – RBT Nov 16 '17 at 00:29