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…
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
CodeAccessSecurityAttributeand notSecurityAttribute.If you look at the IL generated when applying theFooattribute when it inherits fromCodeAccessSecurityAttributeit looks like this:When
Fooinherits from SecurityAttribute it looks like this: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
We get the following IL:
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
Programclass into separate files we can cause it by rearranging the files in the class like this:Even more interesting if we do the following with class
OtherandOther2give the error butProgramdoes not. Only the classes that come beforeFooin the file will have the errorWhat 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.