在.net framerwork 4.5中,MS已弃用AddAssembly()
,并提示开发人员使用AddAssemblyEvidence()
。
现在,在我的旧代码中,我将Assembly.GetExecutingAssembly().FullName
作为参数传递给AddAssembly()
。
但是当我将相同的参数传递给AddAssemblyEvidence()
时,我得到了编译时错误
类型'string'不能用作泛型类型中的类型参数'T'。从字符串到
没有隐式引用system.security.policy.evidencebase
答案 0 :(得分:1)
我创建了一个控制台项目,另一个是DLL。 在DLL lib中我有以下类:
using System;
namespace HelloWorld2
{
[Serializable]
public class Class1
{
public Class1()
{
}
public void ExcuteMe()
{
Console.WriteLine("Hello");
}
}
}
在控制台应用程序中,我正在调用ExecuteMe方法:
namespace ConsoleApplication1
{
using System;
using System.Reflection;
using System.Security;
using System.Security.Policy;
using System.Security.Principal;
using HelloWorld2;
class Program
{
static void Main(string[] args)
{
var fullPathToDll = "HelloWorld2.dll";
var domainSetup = new AppDomainSetup
{
ApplicationBase = Environment.CurrentDirectory,
PrivateBinPath = fullPathToDll
};
var ev1 = new Evidence();
ev1.AddAssemblyEvidence(new ApplicationDirectory(typeof(Class1).Assembly.FullName));
ev1.AddHostEvidence(new Zone(SecurityZone.MyComputer));
var ad = AppDomain.CreateDomain("Class1", ev1, domainSetup, Assembly.GetExecutingAssembly().PermissionSet, null);
var identity = new GenericIdentity("Class1");
var principal = new GenericPrincipal(identity, null);
ad.SetThreadPrincipal(principal);
var remoteWorker = (Class1)ad.CreateInstanceFromAndUnwrap(
fullPathToDll,
typeof(Class1).FullName);
remoteWorker.ExcuteMe();
}
}
}
您现在免费,上面的代码只包含这个概念。
我想你究竟需要这一行:
ev1.AddAssemblyEvidence(new ApplicationDirectory(Assembly.GetExecutingAssembly()。Location));