Tuesday, February 20, 2007

CustomAttributes with ILASM 2.0

There are some great debugging attributes you can add to your .Net project. Sadly I have not found a good way yet to get the debugger to use them on "system" assemblies that have private or internal classes. You can view all this information in the debugger, so I can't say it makes a lot of sense.



[assembly: DebuggerDisplay("{{_completionCallback.Method}}", Target=typeof(System.Web.HttpApplication.SyncEventExecutionStep))]



This fine code gets you this error message.


autoexp.cs(18,103): error CS0122: 'System.Web.HttpApplication.SyncEventExecutionStep' is inaccessible due to its protection level


Lame, very lame.

You can work around this by using ILASM. I mean we just need the type after all, can't be that hard. Here's a working ILDasm of a custom attribute.


.custom instance void [mscorlib]System.Diagnostics.DebuggerDisplayAttribute::.ctor(string) = ( 01 00 0C 5C 7B 7B 4D 65 73 73 61 67 65 7D 7D 01 // ...\{{Message}}.
00 54 50 06 54 61 72 67 65 74 7A 53 79 73 74 65 // .TP.TargetzSyste
6D 2E 52 65 66 6C 65 63 74 69 6F 6E 2E 52 65 66 // m.Reflection.Ref
6C 65 63 74 69 6F 6E 54 79 70 65 4C 6F 61 64 45 // lectionTypeLoadE
78 63 65 70 74 69 6F 6E 2C 20 6D 73 63 6F 72 6C // xception, mscorl
69 62 2C 20 56 65 72 73 69 6F 6E 3D 32 2E 30 2E // ib, Version=2.0.
30 2E 30 2C 20 43 75 6C 74 75 72 65 3D 6E 65 75 // 0.0, Culture=neu
74 72 61 6C 2C 20 50 75 62 6C 69 63 4B 65 79 54 // tral, PublicKeyT
6F 6B 65 6E 3D 62 37 37 61 35 63 35 36 31 39 33 // oken=b77a5c56193
34 65 30 38 39 ) // 4e089



This is just a joy to edit and try and use. Luckily with v2.0 it's much better.


.custom instance void [mscorlib]System.Diagnostics.DebuggerDisplayAttribute::.ctor(string)
= {string('\\{{_completionCallback.Method}}')property type 'Target' = type(class 'System.Web.HttpApplication.SyncEventExecutionStep, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')}

There you go, now it's editable, you just have build into your DLL of choice and you can almost tell what's happening.

No comments: