Known Reflection.Emit Restrictions

Shreeman suggested us to publish a list of Reflection.Emit known restrictions (that is not going to be made into Whidbey). I happen to have such a list on my machine for my own reference. So I just post them out here:

  • Cannot Emit nested enum type

    • Basically, we need a CreateEnum API on TypeBuilde
  • Cannot Emit global field 

     

  • Cannot Emit private enum field 

  • Small things like: 

    •  Cannot Emit some of the AssemblyNameFlags such as 0; Cannot Emit CallingConventions.WinAPI

       

  • EventBuilder should derive from EventInfo 

    • This will probably never be fixed because if we fix this it is a breaking change from previous versions.
  • Cannot Emit new format security attribute 

In Whidbey, Security Attributes got a new form which has a less length in blob and the layout is closer to normal custom attributes.

For example, the MemberAccessPermission attribute looks like this in ildasm:

.permissionset reqmin

= {[mscorlib]System.Security.Permissions.ReflectionPermissionAttribute = {property enum class 'System.Security.Permissions.ReflectionPermissionFlag, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 'Flags' = int32(2)}}

The old format in Everett is like this:

.permissionset reqmin

            "<PermissionSet class=\"System.Security.PermissionSe"

  + "t\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permis"

  + "sions.ReflectionPermission, mscorlib, Version=2.0.0.0, Cultu"

  + "re=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nF"

  + "lags=\"MemberAccess\"/>\r\n</PermissionSet>\r\n"

If you use Ctrl+M to open up the metdata info, you will find the new attribute's blob length is shorter.They serve the same purpose and has same effect. The only problem is that Reflection doesn't support Reflect on the old format security attribute.

  • On Method Emit, some ordering of the API usage can cause the method not being emitted fully right.

The order of setting custom attribute, set parameters and set implementation flags on method could matter.

A best practice is that you always SetImplementationFlags last and you always set return type before setting parameter types.

 

Finally, I was asked by a user about CodeDom and Reflection.Emit comparison. I know little about CodeDom, so I'd rather not comment it here. Here is an article I found on the web that give some light on this topic:

 

https://www.fawcette.com/reports/vslivesf/2004/holmes/

 

Edited:7/2/2005

Remove some restrictions since they are going to be fixed in Whidbey.