views:

43

answers:

2

I've been working on understanding Code Access Security and I was hoping somebody might be able to explain to me this behavior and why it requires added permissions. Say I have two extension methods

   public static string dib(this string source)
   { 
     return souce.dob();
   }

   public static string dob(this string source)
   {
     return source+"dob":
   }

If these two methods appear in the same class they require no special CAS permissions. However as soon as I move dob into another class I require System.Security.Permissions.SecurityPermission with the ControlEvidence flag. Is there some restriciton on calling extension methods from within extension methods between classes? Is there a workaround other than merging all extensions into a huge single class?

'''Edit:''' As it turns out the actual problem was not extension method related. There was a constructor which used a RegEx in one of the classes. Regexes, along with other functions which are compiled at runtime, require ControlEvidence. Thanks for your help, this CAS stuff is very tricky.

A: 

That seems very odd to me. I'm frankly not very knowledgeable about CAS issues, but I would be very surprised if this were genuinely an extension method issue.

If you call the extension methods using non-extension method syntax (i.e. TypeName.Method(args)) does it work? If not, and you make them non-extension methods, does that work? If you run into the same issue in both cases, then clearly it's not an extension method problem - which should at least help you in researching the problem. If you don't run into the same issue, please give details of exactly what works... the only different with the extension method declarations should be extra attributes, and I can't see how that would affect CAS.

I assume you're calling these methods in a "normal" scenario rather than via lambda expressions building expression trees? That would introduce extra complications.

Jon Skeet
A: 

I cannot reproduce the problem by denying SecurityPermission\ControlEvidence to either the caller or the callee or to both. What version of the .NET Framework are you running? Also, to help with repro attempts, could you please specify which of the relevant assemblies are missing the SecurityPermission\ControlEvidence permission?

Nicole Calinoiu