如何使我的DynamicMethod安全性至关重要? [英] How can I make my DynamicMethod security-critical?

查看:96
本文介绍了如何使我的DynamicMethod安全性至关重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的场景,我想创建一个动态方法,该方法附加到内存中AssemblyBuilder中的类。动态方法在我的(常规)程序集中调用方法 GetReplacement。

I have a rather convoluted scenario where I want to create a DynamicMethod that's attached to a class in an in-memory AssemblyBuilder. The dynamic method calls a method "GetReplacement" in a (regular) assembly of mine.

在.NET 2.0中该方法工作正常,但在.NET 4.0中却出现错误:

This worked fine in .NET 2.0, but in .NET 4.0 I get an error:

MethodAccessException: Attempt by security transparent method 'DynamicClass.Max(Int32, Int32)' 
to access security critical method 'xxx.GetReplacement()' failed.

根据我的阅读,我的动态方法(上面错误中的Max)是安全透明的因为它所附加的程序集(AssemblyBuilder)是透明的。我猜测AssemblyBuilder是透明的,因为它是一个动态程序集。

From what I've read, my dynamic method (Max in the error above) is security-transparent because the assembly it's attached to (the AssemblyBuilder) is transparent. I'm guessing the AssemblyBuilder is transparent because it's a dynamic assembly.

我该如何使我的动态方法变得很重要,或者如何做才能授予其调用GetReplacement的权限?我想在GetReplacement的程序集中调用其他几种方法,因此修复动态方法要比以某种方式标记GetReplacement更好。

How do I make my dynamic method critical or do whatever it takes to grant it permission to call GetReplacement? There are a couple of other methods I want to call in GetReplacement's assembly so fixing the dynamic method would be better than marking GetReplacement in some way.

我有点迷失了希望能有所帮助!

I'm a bit lost and would love some help!

推荐答案

糟糕,这是我的错误。该文档是正确的:动态方法从其附加类型继承其安全性。您未附加到类型的方法将附加到安全透明程序集。您不能向超越安全性的DynamicMethod添加属性-您必须将其附加到适当的类型。

Oops, this was a mistake on my part. The docs are right: the dynamic method inherits its security from the type to which it's attached. Methods that you don't attach to a type are attached to a security transparent assembly. You can't add an attribute to a DynamicMethod overriding the security -- you have to attach it to an appropriate type.

我犯了一个愚蠢的错误:附加到AssemblyBuilder上的动态方法,而不是附加到mscorlib(安全透明)的动态方法。

I was making a dumb mistake: looking at the dynamic method attached to an AssemblyBuilder, not the one attached to mscorlib (which is security transparent).

这篇关于如何使我的DynamicMethod安全性至关重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆