Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this an C# 4.0 compiler optional parameters bug?

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...

like image 791
controlflow Avatar asked Jun 17 '11 22:06

controlflow


People also ask

What are C-level positions?

C-level, also called the C-suite, is a term used to describe high-ranking executive titles in an organization. The letter C, in this context, stands for "chief," as in chief executive officer and chief operating officer.

How many C-level positions are there?

Traditionally, there are three prime C-suite positions: Chief Executive Officer (CEO), Chief Operating Officer (COO) and Chief Financial Officer (CFO).

Is C-level a controller?

A CFO and a controller are on different levels within a company's organizational hierarchy (or on its org chart). The CFO is an executive, working in the c-suite along with the CEO, COO, or any other executive-level employees. A controller, on the other hand, is in middle management, a department head.

What is C-level in business?

C-level or C-suite executives hold senior leadership positions within a company. They make impactful decisions to grow the businesses and make lasting changes to how the company functions. Many companies have several c-suite executive positions, ranging from CEO to CISO.


1 Answers

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.

like image 106
Craig Suchanec Avatar answered Sep 29 '22 11:09

Craig Suchanec