System.Type.GetCustomAttributes on an assembly loaded from a network share is not showing all attributes

半世苍凉 提交于 2019-12-02 13:03:25

问题


I have a managed dll – repro.dll, which contains class TestModuleCommand decorated with 2 attributes: System.ObsoleteAttribute and System.Management.Automation.CmdletAttribute (comes from System.Management.Automation.dll which is in the GAC in Windows 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");
                }
            }
        }

If repro.dll is placed in a local directory, I can see both attributes returned from System.Type.GetCustomAttributes(false). If repro.dll is placed in a network path, then I can see only one attribute (although I still see both attributes via System.Reflection.CustomAttributeData.GetCustomAttributes(MemberInfo)). This is undesired – I want to see both attributes (I know that instantiating CmdletAttribute doesn’t have a security impact).

From what I found online, I am vaguely aware that repro.dll (if loaded from network location) cannot fully see S.M.A.dll. I think that CAS allows me to declare in System.Management.Automation that CmdletAttribute is safe, but I haven’t been able to figure out how to write that declaration. Where can I read more to fully understand what is going on? Any words of wisdom are welcomed.

Thanks,

Lukasz

PS. Below is a repro that anyone can try at a powershell.exe prompt (in Windows 7 - Add-Type cmdlet is new in 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:


This is the answer that I found: Haibo Luo - My Attribute Disappears



来源:https://stackoverflow.com/questions/2015452/system-type-getcustomattributes-on-an-assembly-loaded-from-a-network-share-is-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!