Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I decompiled the one dll and exported to Project/Solution.
I tried to understand lines of codes but i could not understand following lines.
C#
Type type;
object objectValue = RuntimeHelpers.GetObjectValue(type.GetField("processInfo", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic).GetValue((object) Process.GetCurrentProcess()));
strFrom = StringType.FromObject(objectValue.GetType().GetField("processName", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetValue(RuntimeHelpers.GetObjectValue(objectValue)));
Logger.write("process: " + strFrom);


i did not understand what these lines of codes are doing.
Could you please explain.

Thank you.

What I have tried:

I tried to looking into the documentation and search in internet
Posted
Updated 6-Dec-16 6:48am
v2
Comments
CHill60 6-Dec-16 12:15pm    
I've removed the VB tag from your question as this is not VB

Getting information about ProcessInfo Class (System.Web)[^].
 
Share this answer
 
The code seems to be using private reflection[^] to read a value that's already exposed via a public property:
Process.ProcessName Property (System.Diagnostics)[^]

It's roughly equivalent to:
C#
strFrom = Process.GetCurrentProcess().ProcessName;
Logger.write("process: " + strFrom);
 
Share this answer
 
Comments
[no name] 6-Dec-16 14:00pm    
I just noticed that above code was in catch block if
strFrom = Process.GetCurrentProcess().ProcessName;
Logger.write("process: " + strFrom);
throw exception.
[no name] 7-Dec-16 11:50am    
Above code block in question is in catch block which is trying an attempt to get the process name if Process.GetCurrentProcess().ProcessName; failed for some reason. But above code doesn't build because Type has not been initialised. So is there any other way to make it work around?
[no name] 7-Dec-16 12:10pm    
code looks like this :
string strFrom = Logger.EmailExceptionFrom.Trim();
if (strFrom.Length == 0)
{
try
{
strFrom = Process.GetCurrentProcess().ProcessName;
}
catch (Exception ex1)
{
ProjectData.SetProjectError(ex1);
Logger.write("Could not determine program name; attempting workaround...", 1);
try
{
Type type;
object objectValue = RuntimeHelpers.GetObjectValue(type.GetField("processInfo", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic).GetValue((object) Process.GetCurrentProcess()));
strFrom = StringType.FromObject(objectValue.GetType().GetField("processName", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetValue(RuntimeHelpers.GetObjectValue(objectValue)));
Logger.write("process: " + strFrom, 1);
}
catch (Exception ex2)
{
ProjectData.SetProjectError(ex2);
Logger.write("Setting program name to 'WebService' since workaround failed: " + ex2.StackTrace, 1);
ProjectData.ClearProjectError();
}
ProjectData.ClearProjectError();
}
}

Problem is I can't re build this code. I get error - Use of unassigned local variable 'type'. Is there any work around?
Richard Deeming 7-Dec-16 13:37pm    
Either use the code I posted, or initialize the type variable.

Type type = typeof(Process);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900