views:

248

answers:

1

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  A: 

Great answer is here: http://blogs.msdn.com/haibo_luo/archive/2006/02/21/536470.aspx

Lukasz Anforowicz