从网络共享加载的程序集上的System.Type.GetCustomAttributes未显示所有属性

时间:2010-01-06 19:06:53

标签: code-access-security getcustomattributes

我有一个托管的dll - repro.dll,它包含用2个属性修饰的类TestModuleCommand:System.ObsoleteAttribute和System.Management.Automation.CmdletAttribute(来自System.Management.Automation.dll,它位于Windows的GAC中) 7)

        namespace Test {
            [System.Obsolete]
            [System.Management.Automation.Cmdlet("Test", "Module")]
            public class TestModuleCommand : System.Management.Automation.PSCmdlet {
                protected override void ProcessRecord() {
                    this.WriteObject("In test-module");
                }
            }
        }

如果将repro.dll放在本地目录中,我可以看到从System.Type.GetCustomAttributes(false)返回的两个属性。如果repro.dll放在网络路径中,那么我只能看到一个属性(虽然我仍然通过System.Reflection.CustomAttributeData.GetCustomAttributes(MemberInfo)看到这两个属性)。这是不受欢迎的 - 我想看到这两个属性(我知道实例化CmdletAttribute没有安全影响)。

从我在网上找到的,我隐约知道repro.dll(如果从网络位置加载)无法完全看到S.M.A.dll。我认为CAS允许我在System.Management.Automation中声明CmdletAttribute是安全的,但我还是无法弄清楚如何编写该声明。我在哪里可以阅读更多内容以完全了解发生了什么?任何智慧的话都受到欢迎。

谢谢,

卢卡斯

PS。下面是一个repro,任何人都可以在powershell.exe提示符下尝试(在Windows 7中 - Add-Type cmdlet是PowerShell v2中的新增功能):

PS C:\> Add-Type -TypeDefinition @"
>>             namespace Test {
>>                 [System.Obsolete]
>>                 [System.Management.Automation.Cmdlet("Test", "Module")]
>>                 public class TestModuleCommand : System.Management.Automation.PSCmdlet {
>>                     protected override void ProcessRecord() {
>>                         this.WriteObject("In test-module");
>>                     }
>>                 }
>>             }
>> "@ -OutputAssembly \\axp-test\scratch\lukasza\repro.dll -OutputType Library
>>
PS C:\> # local copy would work...
PS C:\> # Copy \\axp-test\scratch\lukasza\repro.dll ~\repro.dll
PS C:\>
PS C:\> $a = [System.Reflection.Assembly]::LoadFrom("\\axp-test\scratch\lukasza\repro.dll")
PS C:\> $t = $a.GetType("Test.TestModuleCommand")
PS C:\> $t.GetCustomAttributes($false) # only 1 attribute is visible here

Message                                   IsError TypeId
-------                                   ------- ------
                                          False     System.ObsoleteAttribute


PS C:\>
PS C:\> [System.Reflection.CustomAttributeData]::GetCustomAttributes($t) # but I can see both attributes here

Constructor                             ConstructorArguments  NamedArguments
-----------                             --------------------  --------------
Void .ctor(System.String, System.Str... {"Test", "Module"}    {}
Void .ctor()                            {}                    {}


PS C:\>
PS C:\> $a.Evidence

                                           SecurityZone
                                           ------------
                                           Intranet

1 个答案:

答案 0 :(得分:2)

这是我找到的答案:Haibo Luo - My Attribute Disappears