如何使我的 DynamicMethod 成为安全关键型?

发布于 2024-09-10 05:17:54 字数 596 浏览 6 评论 0原文

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

这在 .NET 2.0 中工作正常,但在 .NET 4.0 中我收到一个错误:

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

从我读到的内容来看,我的动态方法(上面错误中的 Max)是安全透明的,因为它附加到的程序集(AssemblyBuilder)是透明的。我猜 AssemblyBuilder 是透明的,因为它是动态程序集。

如何使我的动态方法成为关键方法或尽一切努力授予其调用 GetReplacement 的权限?我想在 GetReplacement 的程序集中调用其他几个方法,因此修复动态方法比以某种方式标记 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.

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.

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-09-17 05:17:54

创建 AssemblyBuilder 时,您应该使用 CustomAttributeBuilder 分配 SecurityCriticalAttribute 到大会。

一旦程序集被标记为安全关键,您就可以将相同的属性添加到任何 DynamicMethod。

While creating the AssemblyBuilder you should use a CustomAttributeBuilder to assign a SecurityCriticalAttribute to the assembly.

Once the assembly is marked a Security-Critical you could add the same attribute to any DynamicMethod.

帝王念 2024-09-17 05:17:54

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

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

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文