Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:38:17+00:00 2026-05-23T07:38:17+00:00

I’m writing custom security attribute and got strange compiler behaviour… When I’m using the

  • 0

I’m writing custom security attribute and got strange compiler behaviour… When I’m using the attribute at the same file, default parameter values works fine:

using System.Security.Permissions;

[System.Serializable]
sealed class FooAttribute : CodeAccessSecurityAttribute {
    public FooAttribute(SecurityAction action = SecurityAction.Demand) : base(action) { }
    public override System.Security.IPermission CreatePermission() { return null; }
}

[Foo] class Program {
    static void Main(string[] args) { }
}

But when I’m separating the code above into two files like that – file 1:

using System.Security.Permissions;

[System.Serializable]
sealed class FooAttribute : CodeAccessSecurityAttribute {
    public FooAttribute(SecurityAction action = SecurityAction.Demand) : base(action) { }
    public override System.Security.IPermission CreatePermission() { return null; }
}

And file 2:

[Foo] class Program {
    static void Main(string[] args) { }
}

I’ve got an compiler error:

Error:
‘FooAttribute’
does not contain a constructor that
takes 0 arguments

This occurs only with the CodeAccessSecurityAttribute inheritors, looks very strange…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:38:17+00:00Added an answer on May 23, 2026 at 7:38 am

    So I don’t have an exact answer but I took it as far as I could looking into it. I think I understand why it happens when you inherit from CodeAccessSecurityAttribute and not SecurityAttribute. If you look at the IL generated when applying the Foo attribute when it inherits from CodeAccessSecurityAttribute it looks like this:

    .permissionset demand = {class 'ConsoleApplication1.FooAttribute, ConsoleApplication1, Version=1.0.0.0, Culture=neutral' = {}}
    

    When Foo inherits from SecurityAttribute it looks like this:

    .custom instance void ConsoleApplication1.FooAttribute::.ctor(valuetype [mscorlib]System.Security.Permissions.SecurityAction) = ( 01 00 02 00 00 00 00 00 ) 
    

    Clearly the CodeAccessSecurityAttribute drastically changes the IL generated by applying the attribute.

    Looking at the IL more if we change the Foo declaration to be like as follows

    [Foo(SecurityAction.Demand)]
    

    We get the following IL:

    .permissionset demand = {class 'ConsoleApplication1.FooAttribute, ConsoleApplication1, Version=1.0.0.0, Culture=neutral' = {}}
    

    Its the same as it was when we did not specify the optional parameter. Further we can cause the error not just by splitting the attribute and the Program class into separate files we can cause it by rearranging the files in the class like this:

    [Foo]
    class Program
    {
    
        static void Main(string[] args) {}
    
    
    }
    
    [System.Serializable]
    sealed class FooAttribute : CodeAccessSecurityAttribute
    {
        public FooAttribute(SecurityAction action = SecurityAction.Demand) : base(action) { }
        public override System.Security.IPermission CreatePermission() { return null; }
    }
    

    Even more interesting if we do the following with class Other and Other2 give the error but Program does not. Only the classes that come before Foo in the file will have the error

     [Foo]
     class Other
     {
    
     }
    
     [Foo]
     class Other2
     {
     }
    
     [System.Serializable]
     sealed class FooAttribute : CodeAccessSecurityAttribute
     {
          public FooAttribute(SecurityAction action = SecurityAction.Demand) : base(action) { }
          public override System.Security.IPermission CreatePermission() { return null; }        }
    
     [Foo]
     class Program
     {
    
      static void Main(string[] args) {}
     }
    

    What this says to me is that there is a problem somewhere in the build process. I don’t know enough about how Code Access Security works to put my finger on what the exact problem is. There has to be part of the process that looks at the CodeAccessSecurityAttributes and does something with attempting to apply the SecurityAction to the code. I assume it builds some sort of metadata for the assembly. It must do this in some sort of ordered way so that it doesn’t see the optional parameter until after it has already passed the Program class. It then must use that metadata in some way during the build process and that is where you are seeing the failure. For any more detail we’ll have to hope someone who knows the compiler i.e. Eric can shed some light on it. I’d submit it on connect.microsoft.com as one of the comments suggested as it seems like a bug caused by the order things are traversed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
i want to parse a xhtml file and display in UITableView. what is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.