English 中文(简体)
System.Type.GetCustomAttributes on an assembly loaded from a network share is not showing all attributes
原标题:

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-testscratchlukasza
epro.dll -OutputType Library
>>
PS C:> # local copy would work...
PS C:> # Copy \axp-testscratchlukasza
epro.dll ~
epro.dll
PS C:>
PS C:> $a = [System.Reflection.Assembly]::LoadFrom("\axp-testscratchlukasza
epro.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
问题回答

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





相关问题
Is there a better tool than permcalc to analyse permissions?

I have some code which needs to run under medium trust but doesn t. I ve used permcalc in the past but it is rather painful to get the output and compare it to the medium trust definition. What I ...

When to use SharePointPermissionAttribute

When do I actually need to decorate my methods with the SharePointPermission attribute? I ve come across some code samples that have them and some that don t.

热门标签