4

The usual way to integrate pythonnet in your project is the following:

import clr
clr.AddReference('My.Assembly')
import My.Assembly

My.Assembly.DoSomething()

What if I don't want the assembly namespace to be imported and be available globally. Is there any way to achieve something like this:

my_assembly = magic_loader('My.Assembly.dll')
my_assembly.DoSomething()
Ax_6
  • 91
  • 9

1 Answers1

0

It seems that this might be relevant to your case: How are DLLs loaded by the CLR?

So you could do:

using System;
using System.Reflection;
public class Utilities {
  public static Object LoadCustomCls(string file) {
    Assembly a = Assembly.LoadFrom(file) ;
    return a.CreateInstance("namespace.someclass") ;
  }
}

And calling LoadCustomCls(file).DoSomething() might work.

Notice that there is a deeper question here. Are we talking about a security boundary or a code boundary? Why do you need to force this restriction?

Because I am not sure anything you would do , would be a security boundary, as long as you run in the same process. And about the general availability of the assembly, I don't understand why does it matter.

PS. noticed just now that you want python. I am sure it is possible to do the equivalent in python / to call this C# code directly (ie through the clr.AddReference ).

Eyal
  • 53
  • 6