c# - Interpreting MethodBody.ExceptionHandlingClauses collection -


i using reflection analyse method's exception handling blocks [exceptionhandlingclauses] property of [methodbody] class. not figure out msdn documentation how collection behaves , how interpret it. let's want assert that:

  • a method contains 1 try block.
  • that single block contains 1 catch clause.
  • that single block contains finally clause.

please try not derive context out of following code complex explain here. illustrative code , may of bad design. consider following heirarchy:

// tier 1. base class. namespace productname {     // not abstract class.     // these tier 1 class methods not have implementations or exception handling blocks.     public class template: system.idisposable     {         public sealed void launch () { this.onlaunch(); }         public sealed void simulate () { this.onsimulate(); }         public sealed void test () { this.ontest(); }         public sealed void submit () { this.onsubmit(); }         public sealed void exit () { this.onexit(); }          protected virtual void onlaunch () { }         protected virtual void onsimulate () { }         protected virtual bool ontest () { return (false); }         protected virtual void onsubmit () { }         protected virtual void onexit () { }     } }  // tier 2. defines template platforms e.g. access, excel, etc. // these tier 2 classes not have implementations or exception handling blocks. namespace productname.access { public class template: productname.template { } } namespace productname.excel { public class template: productname.template { } } namespace productname.outlook { public class template: productname.template { } } namespace productname.powerpoint { public class template: productname.template { } } namespace productname.word { public class template: productname.template { } }  // tier 3. defines individual templates in each platform. // each platform have hundreds of classes [template_########] naming convention. // each class overrides virtual methods 1 try/catch/finally block in ontest , none in other methods. namespace productname.access.templates { public class template_00000001: productname.access.template { } } namespace productname.excel.templates { public class template_00000001: productname.excel.template { } } namespace productname.outlook.templates { public class template_00000001: productname.outlook.template { } } namespace productname.powerpoint.templates { public class template_00000001: productname.powerpoint.template { } } namespace productname.word.templates { public class template_00000001: productname.word.template { } } 

i know following these classes:

  • tier 1 base class has no implementation, no exception handling blocks.
  • tier 2 derived classes have no implementation, no exception handling blocks.
  • tier 3 derived classes have implementations of virtual methods methods except [ontest] not contain exception handling blocks.
  • the [ontest] method of tier 3 classes contains 1 exception handling block , in cases nested block and/or few [using] statements.

i tier 3 classes assembly, iterate through each type, [methodinfo] objects each method, [methodbody] object , inspect [exceptionhandlingclauses] collection. result quite strange [ontest] method. [exceptionhandlingclauses] collection shows having anywhere between 0 6 clauses, each of has [flag] value of either [catch] or [finally]. there seems absolutely no correlation between expected number of [catch/finally] blocks , collection shows.

sometimes, evens shows 2 [finally] clauses in methods not have [try/catch] block.

at first thought may have inheritance none of base classes have implementations, let alone exception handling blocks.

some guidance appreciated.

the exceptionhandlingclauses property providing information exception handling clauses in compiled bytecode. semantics of exception handling blocks in bytecode governed ecma-335 standard, not ecma-334 (the c# language standard). rules exception handling blocks in c# differ imposed bytecode, compiler compiles code in way produces seem strange exception handling blocks in fact result proper runtime semantics according original c# code.

in other cases, c# provides "syntactic sugar" features not supported bytecode primitives. example, using , lock statements in c# both implemented compiling down try/finally blocks, included in exceptionhandlingclauses property.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -