PC Review


Reply
Thread Tools Rate Thread

Dynamic cast situation...

 
 
Ole Hanson
Guest
Posts: n/a
 
      4th Jan 2005
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)

..

..

}


 
Reply With Quote
 
 
 
 
Ole Hanson
Guest
Posts: n/a
 
      4th Jan 2005
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
..
 
Reply With Quote
 
Richard A. Lowe
Guest
Posts: n/a
 
      4th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:e6G$(E-Mail Removed)...
>> 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)
>>
>> ..
>>
>> ..
>>
>> }
>>
>>

>
>



 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      5th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:e6G$(E-Mail Removed)...
> 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)
>
> ..
>
> ..
>
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic cast? Jamie McQuay Microsoft C# .NET 9 21st Feb 2008 08:55 PM
Dynamic cast with generics Néstor Sánchez Microsoft C# .NET 3 29th Jan 2007 08:28 PM
BinaryWriter - Dynamic cast jfbaro Microsoft C# .NET 1 18th Dec 2006 09:28 PM
Dynamic cast possible? Zark3 Microsoft C# .NET 4 7th May 2006 09:23 AM
How to cast to a dynamic type ? John Microsoft Dot NET Framework 4 13th Jan 2005 10:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 PM.