AndyAinscow
asked on
Getting the Exceptions HResult value in .net 4 or earlier
An exception has a HResult property. From .net 4.5 onwards the getter is public but in .net 4 or earlier it is protected.
I want to trap an explicit (Microsoft) exception and suppress the default warning message so the obvious way is:
if(e.HResult == ....
but this won't compile in a .net 4 based app because the e.HResult is protected.
So is there a simple workaround to extract the HResult value in a .net 4 based app?
(The app will run on English and non-English operating systems so I can't rely on the text of the message).
I want to trap an explicit (Microsoft) exception and suppress the default warning message so the obvious way is:
if(e.HResult == ....
but this won't compile in a .net 4 based app because the e.HResult is protected.
So is there a simple workaround to extract the HResult value in a .net 4 based app?
(The app will run on English and non-English operating systems so I can't rely on the text of the message).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Marshal.GetHRForException slipped right out of my full brain.
@Bob - *in sarcastic voice* Oh I never have one of those moments... ;)
@Andy - Example using bob's code -
@Andy - Example using bob's code -
using System;
namespace EE_Q28620045
{
class Program
{
static void Main(string[] args)
{
try
{
throw new InvalidOperationException("Test");
}
catch (Exception ex)
{
Console.WriteLine(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
}
}
}
}
Produces the following output --saige-
ASKER
@it_saige
The System.Runtime.InteropServ ices.Marsh al.GetHRFo rException worked without problems. So my question is answered with that. I'll leave it open for a short while should Bob wish to respond to ---
@Bob
var property = ex.GetType().GetProperty(" HResult", BindingFlags.NonPublic | BindingFlags.Instance);
property was null after that line so the GetValue line faulted with an exception.
Do you want more info / to look into it further (out of academic interest)?
The System.Runtime.InteropServ
@Bob
var property = ex.GetType().GetProperty("
property was null after that line so the GetValue line faulted with an exception.
Do you want more info / to look into it further (out of academic interest)?
I looked at a project with 3.5 framework set, and the Exception class had the HResult property as protected. With that it worked for me, but if there is no property with that name, and only a private field, you would switch to use ex.GetType().GetField("_hR esult", BindingFlags.NonPublic | BindingFlags.Instance) for example. The specifics really depend on the class structure.
@Andy - I implemented Bob's code and did get the expected results:
-saige-
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Reflection;
namespace EE_Q28620045
{
class Program
{
static void Main(string[] args)
{
try
{
throw new InvalidOperationException("Test");
}
catch (Exception ex)
{
Console.WriteLine("Using linq to reflect Exception properties and get HResult property value:");
var prop1 = (from prop in ex.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance) where prop.Name.Equals("HResult") select prop).FirstOrDefault();
Console.WriteLine("{0} = {1}", prop1.Name, prop1.GetValue(ex, null));
Console.WriteLine();
Console.WriteLine("Using reflection to get Exception HResult property value:");
var prop2 = ex.GetType().GetProperty("HResult", BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine("{0} = {1}", prop2.Name, prop2.GetValue(ex, null));
Console.WriteLine();
Console.WriteLine("Using GetHRForException() to get Exception HResult property value:", ex.GetType().ToString());
Console.WriteLine("HResult = {0}", Marshal.GetHRForException(ex));
}
Console.ReadLine();
}
}
}
Produced the following output -P.S - Sorry, couldn't resist throwing in a bit of linq. ;)-saige-
ASKER
I'll look a little closer - I'm fighting to get the office ribbon to accept a custom image at present - but not certain if I have time today.
@Bob - In all honesty, if you are using reflection in order to get the values of private/protected members, I would imagine that it would, ultimately, make more sense to get the field value as opposed to the property value. That is unless the property has some specialized logic in it.
-saige-
-saige-
ASKER
<Image problem is temporarily solved.>
var property = e.GetType().GetField("_hRe sult", System.Reflection.BindingF lags.NonPu blic | System.Reflection.BindingF lags.Insta nce);
Still has property as null.
var property = e.GetType().GetField("_hRe
Still has property as null.
ASKER
New console app. .Net framework 4:
with a breakpoint on the var value.... line the var property is null for both wheras the value of i is that of the HResult put into the exception
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
throw new System.Security.Cryptography.CryptographicException(-2146233296); // 0x80131430;
}
catch (System.Security.Cryptography.CryptographicException e)
{
int i = System.Runtime.InteropServices.Marshal.GetHRForException(e);
var property = e.GetType().GetProperty("HResult", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
//var property = e.GetType().GetField("_hResult", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var value = property.GetValue(e, null);
}
}
}
}
with a breakpoint on the var value.... line the var property is null for both wheras the value of i is that of the HResult put into the exception
@Andy - I found the problem. You had mentioned that in .NET 4, the HResult is a public property. This means that you need to add a BindingFlag for the public properties in order to retrieve it:
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Reflection;
namespace EE_Q28620045
{
class Program
{
static void Main(string[] args)
{
try
{
//throw new InvalidOperationException("Test");
throw new System.Security.Cryptography.CryptographicException(-2146233296);
}
catch (Exception ex)
{
Console.WriteLine("Using linq to reflect Exception properties and get HResult property value:");
var prop1 = (from prop in ex.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public) where prop.Name.Equals("HResult") select prop).FirstOrDefault();
Console.WriteLine("{0} = {1}", prop1.Name, prop1.GetValue(ex, null));
Console.WriteLine();
Console.WriteLine("Using reflection to get Exception HResult property value:");
var prop2 = ex.GetType().GetProperty("HResult", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
Console.WriteLine("{0} = {1}", prop2.Name, prop2.GetValue(ex, null));
Console.WriteLine();
Console.WriteLine("Using GetHRForException() to get Exception HResult property value:", ex.GetType().ToString());
Console.WriteLine("HResult = {0}", Marshal.GetHRForException(ex));
}
Console.ReadLine();
}
}
}
Produces the following output (regardless of using Framework 3.5 or 4):-saige-
I would suggest using what works (Marshal.GetHRForException , but if you are interested in the reflection issue, the specifics depends on the specific implementation.
The reflection example that I showed you attempted to the get HResult property from the Exception. I threw an InvalidOperationException, and use F12 to Go to Definition, until I got down to the base Exception class.
Here is the screen shot of the properties available for the class. Fields are not shown in this view, which is why I reflected against property, and not field. You would need to use a disassembler, like Reflector or Just Decompile to get the real field names.
The reflection example that I showed you attempted to the get HResult property from the Exception. I threw an InvalidOperationException,
Here is the screen shot of the properties available for the class. Fields are not shown in this view, which is why I reflected against property, and not field. You would need to use a disassembler, like Reflector or Just Decompile to get the real field names.
If it is a public property, then you certainly don't need to use anything heavy like reflection.
ASKER
@it_saige. It is only public in .net 4.5 and later and this is for a previous version of .net.
ASKER
Or according to the documentation it is - and mine didn't compile when attempting to access the HResult directly.
Another approach is to use Exception.GetType().GetPro perties(Bi ndingFlags .NonPublic | BindingFlags.Public | BindingFlags.Instance) to get a list of all properties, so that you can see the entire list. Then, you can pick out the property name that you want.
@Andy - That may definately be true. In testing for .NET 4, I received this output for the properties of Exception without the Public BindingFlag:And this output with the Public BindingFlag:
Conversely using .NET 3.5, I recieve this output for the properties of Exception without the Public BindingFlag:And this output with the Public BindingFlag:
-saige-
Conversely using .NET 3.5, I recieve this output for the properties of Exception without the Public BindingFlag:And this output with the Public BindingFlag:
-saige-
ASKER
This is rather wierd - I suspect a bug somewhere in VS or .net.
The code from Bob works with .net 3.5 but not with .net 4
Changing the flags to include public properties then the code from Bob works with .net 4
HOWEVER the HResult is still unavailable due to its protection level in .net 4 - ie still as protected (as the documentation states).
In any case the Marshal.GetHRForException works in all instances that I have tested.
The code from Bob works with .net 3.5 but not with .net 4
Changing the flags to include public properties then the code from Bob works with .net 4
HOWEVER the HResult is still unavailable due to its protection level in .net 4 - ie still as protected (as the documentation states).
In any case the Marshal.GetHRForException works in all instances that I have tested.
ASKER
Thanks.
For anyone reading this note my final summary comment.
For anyone reading this note my final summary comment.