コードで有効にする方法が見つかりませんloadFromRemoteSources
。もしあれば、作成後にアプリケーション ドメインに適用できるとは思えません。しかし、私は間違っている可能性があります!
代わりに、「 CAS ポリシーの暗黙的な使用: loadFromRemoteSources 」で説明されている手法を使用できます。で新しいアプリケーション ドメインを作成し、PermissionState.Unrestricted
リモート アセンブリをそこにロードすることを提案しています。この方法で作成されたアプリケーション ドメインはNotSupportedException
、リモート アセンブリを読み込もうとしてもスローしません。
この手法は、一部のリモート アセンブリのみを完全に信頼して読み込みたいためloadFromRemoteSources
、app.config で有効にしたくない場合のソリューションとして説明されています。同じ手法を使用できますが、プログラム全体を新しいアプリ ドメインにロードします。
以下は、実行されるとすぐに新しいアプリケーション ドメインを作成し、PermissionState.Unrestricted
その中で自身を実行するプログラムの例です。
public class Program : MarshalByRefObject
{
public Program()
{
Console.WriteLine("Program is running.");
Assembly remoteAssembly = Assembly.LoadFrom(
@"\\server\MyRemoteAssembly.dll");
IAddIn addIn = (IAddIn)remoteAssembly.CreateInstance("MyAddIn");
addIn.Initialize();
}
static void Main()
{
// This program needs to run in an application domain with
// PermissionState.Unrestricted, so that remote assemblies can be loaded
// with full trust. Instantiate Program in a new application domain.
PermissionSet permissionSet =
new PermissionSet(PermissionState.Unrestricted);
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase =
AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
AppDomain appDomain = AppDomain.CreateDomain(
"Trusted Domain", null, setup, permissionSet);
appDomain.CreateInstance(
Assembly.GetExecutingAssembly().FullName, "Program");
}
}
public interface IAddIn
{
void Initialize();
}
ネットワーク共有に配置できるMyRemoteAssembly.dllのソース コードは次のとおりです。
public class MyAddIn : IAddIn
{
public void Initialize()
{
Console.WriteLine("Hello from a remote assembly!");
Console.ReadLine();
}
}