Jump to content
  • Advertisement
Sign in to follow this  
Justaddwater

[.net] Getting param values at run time

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to log some information in the case of an error to the event log, but I would like to have some really generic code to reuse. I want to get the paramaters and their values of the current method. I am thing using reflection or perhaps the params object[] but just not sure what is a good way to do it. so for example I would like to automagically fill in all the stuff in [] in the catch below.

private void AddUser(string name)
{
try
{
SomeClass.Add(name);
}
catch
{
ErrorLoggingClass.LogError([Method Name] had an error when the value [value of name] was passed in as varible [name])
}

}

}



Share this post


Link to post
Share on other sites
Advertisement
Using Reflection is the way to go :) Here's your solution (it's in VB.NET, if you don't understand that maybe I could try and convert it later).

Firstly, make sure you import then System.Reflection namespace.
Then, in a public module, put this function:

    Private Function GetExceptionText(ByVal ex As Exception, ByVal ParamArray methodParams As Object()) As String
Dim method As MethodBase = ex.TargetSite
Dim params As ParameterInfo() = method.GetParameters()
Dim paramUB As Integer = params.GetUpperBound(0)
Dim paramNames(paramUB) As String
Dim paramValues(paramUB) As String

For p As Integer = 0 To paramUB
paramNames(p) = params(p).Name & " (" & params(p).ParameterType.Name & ")"
paramValues(p) = methodParams.ToString()
Next

Return "An error occured in " & method.Name & " when the values [" & String.Join(", ", paramValues) & "] were passed to the parameters [" & String.Join(", ", paramNames) & "]."
End Function


and an example using it:

    Dim a As Byte = 255

Try
a += 1
Catch ex As Exception
Debug.WriteLine(GetExceptionText(ex, sender, e))
End Try


the only problem is that you have to pass the parameter values (in the right order) to the function. I searched and there was no way to get around this unfortunately, but it shouldn't be too much of a problem.
hope it helps :)

Share this post


Link to post
Share on other sites
I was able to convert to c# fine, the only problem I have is i would really like to be able to call it generically.

instead of


Debug.WriteLine(GetExceptionText(ex, sender, e))



I would love to just say:


Debug.WriteLine(GetExceptionText(ex,[some generic thing]))

Share this post


Link to post
Share on other sites
yeah, that was the prob - i researched it a bit but atm it seems like .NET doesn't have the capabilities to get the param values...

Share this post


Link to post
Share on other sites
I think you'll find it's actually impossible to get the parameter values outside of the call to that method. Reason is, when an exception is thrown the stack pointer is moved to recover the stack space used when calling the method (among other things). Once this happens, ANY use of the stack has the potential to corrupt those values....and as you can imaging the stack gets used a lot.

Share this post


Link to post
Share on other sites
That makes good sense to me. But it seems like when im throwing within the method the var's should still be reachable.



void foo(string bar)

try

catch

getexetext(ex,[something right here to get current varibles while they still in scope])



Share this post


Link to post
Share on other sites
Quote:
Original post by Justaddwater
That makes good sense to me. But it seems like when im throwing within the method the var's should still be reachable.

*** Source Snippet Removed ***


And they are with the parameter variables :) The problem is that the classes provided are for generic use, not just when in the executing method, so they can't provide a way to access this information. The other problem is that even if they could, there is no guarenttee that the exception wasn't thrown by some other function that this one called, and therefore has lost it's stack space.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!