Casting back up the inheritance chain

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a method that gets an LdapException. It calls a method that receives an Exception object.

After checking via GetType().FullName and verifying that the object is in fact an LdapException, I want to cast it back to an LdapException.

Can I do that? How do I do that?

Code snippet:

Method1
{
try
{
someFunc();
}
catch (LdapException e)
{
PrintError(e);
}
}

PrintErr(Exception e)
{
switch (e.GetType().FullName)
{
case ("LdapException"):
LdapException x = (LdapException)e; <throws an InvalidCastException>
break;
default:
...
}
}

- Bruce
 
With the pseudocode you've provided I can't say for sure where the error is.
One thing I can tell you is that the preferred way of determining if an
object is of a certain type is to use the "is" or "as" operators, e.g.

if (e is LdapException)
{
LdapException x = (LdapException) e;
// ...
}

of, alternatively,

LdapException x = e as LdapException;

if (x != null)
{
//...
}

If you're still having more problems, post a minimal example (i.e. real code
but without unnecessary stuff) that demonstrates the issue.

Ken


brckcc said:
I have a method that gets an LdapException. It calls a method that receives an Exception object.

After checking via GetType().FullName and verifying that the object is in
fact an LdapException, I want to cast it back to an LdapException.
 
Here's the real code. Even though I pass in an LdapException as the fourth (e)parameter, the *** line returns false.

public ErrorMessageBox(MigExceptionLogger log, string message, string caption, Exception e)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = caption;
m_message.Text = message;
*** if (e is LdapException)
{
m_ldapException = e as LdapException;
}
else
{
m_exception = e;
}

m_log = log;
if (e != null)
{
moreInfoButton.Enabled = true;
}
else
{
moreInfoButton.Enabled = false;
}
}
 
Is it possible there are two classes with the name LdapException (but in
different namespaces) that are visible within your code? If so, the
LdapException you're referring to will be the one in the same namespace as
the class which defines that method, but the exception could be an
LdapException from a different namespace.

If you're not sure and you want to test this, put this code at the start of
your function:

Console.WriteLine(typeof(LdapException).FullName);
Console.WriteLine(e.GetType().FullName);

This should print the full name of each including namespace. If they match,
then I'm stumped.

Ken


brckcc said:
Here's the real code. Even though I pass in an LdapException as the
fourth (e)parameter, the *** line returns false.
 
Ken Kolda said:
Is it possible there are two classes with the name LdapException (but in
different namespaces) that are visible within your code? If so, the
LdapException you're referring to will be the one in the same namespace as
the class which defines that method, but the exception could be an
LdapException from a different namespace.

If you're not sure and you want to test this, put this code at the start of
your function:

Console.WriteLine(typeof(LdapException).FullName);
Console.WriteLine(e.GetType().FullName);

This should print the full name of each including namespace. If they match,
then I'm stumped.

It could be the problem described here:

http://www.pobox.com/~skeet/csharp/plugin.html
 
That's good -- I hadn't thought of that possibility but I can certainly see
that being a problem. Thanks for the info.

Ken
 
Back
Top