Evidence C#类的AddAssemblyEvidence()的参数
本文关键字:参数 AddAssemblyEvidence 类的 Evidence | 更新日期: 2023-09-27 17:59:59
在.net framework 4.5中,MS已弃用AddAssembly()
,并提示开发人员使用AddAssemblyEvidence()
。
现在,在我的旧代码中,我将Assembly.GetExecutingAssembly().FullName
作为参数传递给AddAssembly()
。但当我将相同的参数传递给AddAssemblyEvidence()
时,我会得到的编译时错误
类型"string"不能用作泛型类型中的类型参数"t"。字符串对
system.security.policy.evidencebase
没有隐含引用
我为Projects创建了一个控制台,另一个是DLL。在DLL库中,我有以下类:
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(新ApplicationDirectory(Assembly.GetExecutingAssembly().Location))