Dynamic cast situation...

  • Thread starter Thread starter Ole Hanson
  • Start date Start date
O

Ole Hanson

Hi

Trying to cast an exception to the correct type, I encounter some "strange"
experiences:
The compiler complains that the variable "t" is unknown in line 2 (Exception
trueException = (t)exception; )!

Why? I am getting the correct System.Type information in line 1 (Type t =
exception.GetType();), but I can't cast my "trueException" to the correct
type...

Please, give me some input on this..

Thanks!!
/Ole
/// <summary>
/// Extract the property information and add it to the Stringbuilder.
/// </summary>
/// <param name="builder">the builder to populate</param>
/// <param name="exception">the excepton to investigate</param>
private void ExtractExceptionProperties(ref StringBuilder builder, Exception
exception)
{
Type t = exception.GetType();
Exception trueException = (t)exception; //here I want to cast the passed
in System.Exception to the correct type,

if(trueException == null)
return;

PropertyInfo[] aryPublicProperties =
trueException.GetType().GetProperties();
NameValueCollection currentAdditionalInfo;

foreach (PropertyInfo p in aryPublicProperties)
{

do some stuff... (not shown)

..

..

}
 
Just for the record - I DID try with the "as" keyword with same lack of
success.
Like this:

private void ExtractExceptionProperties(ref StringBuilder builder, Exception
exception)
{
Type t = exception.GetType();
Exception trueException = exception as t; //here I want to cast the
passed in System.Exception
..
 
Ole, if you are going to use Reflection (as your original post suggests) to
somehow output the properties of the exception, you don't need to have a
reference to that exception that is exactly of that exception's type.

In your code there is no value in having the variable 'trueException'
because it will hold a reference to exactly the same object as your
parameter 'exception' does. See, the reference is strongly typed as
Exception, but the underlying object is of whatever type was instantiated on
the heap and passed to your method. When you use Reflection, you only need
a reference to the object in question.

hth,
Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Ole Hanson said:
Just for the record - I DID try with the "as" keyword with same lack of
success.
Like this:

private void ExtractExceptionProperties(ref StringBuilder builder,
Exception exception)
{
Type t = exception.GetType();
Exception trueException = exception as t; //here I want to cast the
passed in System.Exception
..
.


}

/Ole




Ole Hanson said:
Hi

Trying to cast an exception to the correct type, I encounter some
"strange" experiences:
The compiler complains that the variable "t" is unknown in line 2
(Exception trueException = (t)exception; )!

Why? I am getting the correct System.Type information in line 1 (Type t =
exception.GetType();), but I can't cast my "trueException" to the correct
type...

Please, give me some input on this..

Thanks!!
/Ole
/// <summary>
/// Extract the property information and add it to the Stringbuilder.
/// </summary>
/// <param name="builder">the builder to populate</param>
/// <param name="exception">the excepton to investigate</param>
private void ExtractExceptionProperties(ref StringBuilder builder,
Exception exception)
{
Type t = exception.GetType();
Exception trueException = (t)exception; //here I want to cast the
passed in System.Exception to the correct type,

if(trueException == null)
return;

PropertyInfo[] aryPublicProperties =
trueException.GetType().GetProperties();
NameValueCollection currentAdditionalInfo;

foreach (PropertyInfo p in aryPublicProperties)
{

do some stuff... (not shown)

..

..

}
 
I'll assume that casting requires that actual name of a type, and not a
variable of type Type. (That is what the docs say). So, since there is no
type named "t", it's unknown.

But, is that section even necessary? You have:
Type t = exception.GetType();

And then later

PropertyInfo[] aryPublicProperties =
trueException.GetType().GetProperties();

I can't imagine that yielding anything different from:


PropertyInfo[] aryPublicProperties = t.GetProperties();

Basically, if exception.GetType() & trueException.GetType() return the same
value, the cast is unnecessary. If they return different values, the cast
wouldn't do anything anyway.
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Ole Hanson said:
Hi

Trying to cast an exception to the correct type, I encounter some "strange"
experiences:
The compiler complains that the variable "t" is unknown in line 2 (Exception
trueException = (t)exception; )!

Why? I am getting the correct System.Type information in line 1 (Type t =
exception.GetType();), but I can't cast my "trueException" to the correct
type...

Please, give me some input on this..

Thanks!!
/Ole
/// <summary>
/// Extract the property information and add it to the Stringbuilder.
/// </summary>
/// <param name="builder">the builder to populate</param>
/// <param name="exception">the excepton to investigate</param>
private void ExtractExceptionProperties(ref StringBuilder builder, Exception
exception)
{
Type t = exception.GetType();
Exception trueException = (t)exception; //here I want to cast the passed
in System.Exception to the correct type,

if(trueException == null)
return;

PropertyInfo[] aryPublicProperties =
trueException.GetType().GetProperties();
NameValueCollection currentAdditionalInfo;

foreach (PropertyInfo p in aryPublicProperties)
{

do some stuff... (not shown)

..

..

}
 
Back
Top