如何使我的 DynamicMethod 成为安全关键型?
我有一个相当复杂的场景,我想创建一个附加到内存中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建
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.
糟糕,这是我的一个错误。文档是正确的:动态方法从它所附加的类型继承其安全性。未附加到类型的方法将附加到安全透明程序集。您不能向 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).