Finding exception with reflection

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
____ I am coming across a problem when I am doing dynamic instantiation of objects using reflection. I will use a basic example but in real world implementation is much more complex.
If I have a class:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; class Sack
{
<span style="color:Blue; public Sack()
{
<span style="color:Blue; throw <span style="color:Blue; new Exception();
}
}
[/code]
<br/>

And I instantiate this object:

<div style="color:Black;background-color:White; <pre>
Sack s = <span style="color:Blue; new Sack();
[/code]

Visual Studio will throw an exception at the line: <span style="font-family:monospace; white-space:pre throw new Exception();
<span style="font-family:monospace; font-size:xx-small <span style="font-size:9px; white-space:pre <span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:small <span style="font-size:11px; white-space:normal Which is desirable (for my
purposes).

<span style="font-family:monospace; font-size:xx-small <span style="font-size:9px; white-space:pre <span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:small <span style="font-size:11px; white-space:normal However if I instantiate the
object using reflection:

<div style="color:Black;background-color:White; <pre>
Assembly testAssembly = Assembly.GetExecutingAssembly();

Type calcType = testAssembly.GetType(<span style="color:#A31515; "DynamicObjectReflectionInstantiation.Sack");
<span style="color:Blue; object calcInstance = Activator.CreateInstance(calcType);
[/code]

The exception will be thrown at the line:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; object calcInstance = Activator.CreateInstance(calcType);
[/code]

This is undesirable because if the constructor of Sack is complex it is difficult to track down where the exception is being thrown...

I hope this makes sense. Thanks for your help.

View the full article
 
Back
Top