English 中文(简体)
Calling extension methods across classes requires ControlEvidence permission
原标题:

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.

最佳回答

I cannot reproduce the problem by denying SecurityPermissionControlEvidence 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 SecurityPermissionControlEvidence permission?

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签