How to get parameter value from StackTrace [duplicate] - c#

This question already has answers here:
Is it possible to get parameters' values for each frame in call stack in .NET
(2 answers)
Closed 10 years ago.
From within a method call I need to "jump" three layers up the stack and retrieve the type and value of the parameters passed to that method.
Getting the parameter type is easy but I couldn't find a way to get the value passed to a certain method on the stack.
var st = new StackTrace();
var frames = st.GetFrames();
var methodParameters = frame[2].GetMethod().GetParameters;
// get each parameter value
Note: using StackTraceis not mandatory.
Is there a wayto find a value of a parameter passed to a method during runtime?

I do not think there is a method unless you develop your own system for storing the values.
The reflection namespace represents static data about an assembly, and you would need to retrieve values at runtime.
I found PostSharp mentioned in MSDN forums, but I have never tried it.

Related

AutoProperty as out value [duplicate]

This question already has answers here:
Is it possible to pass properties as "out" or "ref" parameters?
(3 answers)
Closed 8 years ago.
Why exactly I can't use an AutoProperty as an out parameter?
For example (This gives me an error):
public int HeightValue { get; set; }
//...
private void Parse()
{
int.TryParse(WidthText.Text, out HeightValue);
//Intellisense Error: out argument is not classified as a variable
}
Possibly because properties are in essence methods and you need to give a field to set the value to the out parameter. You can define a backing field for your property and give its value as the out parameter.
See Jon Skeet's answer here:
Passing a property as an 'out' parameter in C#
The method itself needs a variable as the out parameter. It's got to have a storage location it can just write values to. Not a property, not anything it needs to invoke: just a storage location. A property doesn't satisfy that requirement. So there's nothing that can be done by the compiler in the method to allow this.

Can I get a method by signature, using reflection? [duplicate]

This question already has answers here:
Get only Methods with specific signature out of Type.GetMethods()
(5 answers)
Closed 8 years ago.
In C++, we can scan through the memory to get the function by some assembly pattern, I'm thinking about that if we can have a relevant ways to get the function by using function signature in .Net, and if could, post an example.
You are looking for this overloaded method:
public MethodInfo GetMethod(
string name,
Type[] types
)
Searches for the specified public method whose parameters match the specified argument types.
You could find examples in the documentation.

How to invoke a property's method by property's name [duplicate]

This question already has answers here:
Get property value from string using reflection
(24 answers)
Closed 8 years ago.
I'd like to call a property's method of an object through reflection. For instance,
var query = DataContext().SomeTable.Where(u => u.UserID.contains("P");
I've tried the following but no luck:
var query = DataContext().SomeTable.Where(u => u.GetType().GetProperty("UserID").Name.contains("P");
Which returns null. Please help.
You want GetValue, not Name
((string)u.GetType().GetProperty("UserID").GetValue(u) ?? "").Contains("P")
The use of the ?? operator here is just a safeguard to make sure that Contains doesn't throw an exception if UserID is null.
You have to get the value of u, then use ToString on it:
u.GetType().GetProperty("UserID").GetValue(u, null).ToString().Contains("P");
Of course it have be a little bit improved: check if GetValue do not return null etc.
Note: Remember that you can somewhere cache PropertyInfo obtained from u.GetType().GetProperty("UserID") so you won't have to call it every time.

Get the name of the parameter that is passed to function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get name of a variable or parameter
I have this function.
public void AddVariable( String str)
{
Response.Write(str); // will write the value of str
}
But I need to write the name of the string variable that is passed into the function.
For example:
String temp = "test Variable";
AddVariable(temp);
Here I need to get the name of the variable inside my function, Instead of value.
ie, I need to get 'temp' inside my function, Instead of 'test Variable'.
Is it possbile?
You could get the name of the variable inside the function ("str"), but you cannot get the name of the variable that was passed into the function unless you pass it in as a second parameter.
It doesn't make sense to get the name of the variable passed to the function because there may not have even been a variable if a literal was passed such as AddVariable("test variable").
You don't need parameter name (possible from reflection) but rather a variable name that has been passed as a parameter. AFAIK, for all practical purpose, this is not possible for the code within the function.
On the other hand, it's as such possible to do the code analysis of all assemblies loaded in the app-domain and find all invocations to your method and then do the stack-walk to determine possible invocation so that you may able to nail the variable name in the calling method (again if there can be many invocations in calling method, making it difficult to guess the variable name and you have to then rely on IL offset etc) but its just too convoluted.
Perhaps, you can state the reason for such requirements, there can be some alternative. For example, you can get and log stack trace within your method code that can be used for say trouble-shooting.

Getting the name of the parameter passed into a method [duplicate]

This question already has answers here:
How can I get the name of a variable passed into a function?
(23 answers)
Closed 9 years ago.
Duplicate:
Determine the name of the variable used as a parameter to a method
Is there any way to retrieve the name of a parameter that was passed into a method e.g.
int someParameter = 1;
Method(someParameter);
public void Method(int parameter)
{
// I want the name of 'parameter' which will be 'someParameter'.
}
No. It's only the value which is passed in as the argument. All the method gets is the integer. The fact that the expression happened to be just evaluating a variable is unknown as far as your method is concerned.
No, there is no way.
This will be followed by a bunch of people showing weird lambda expression ways to change the call site and kinda get the name, but the short answer is no.
The only way perhaps will be with annotations with run-time retention. But then, it will be the name of the annotation, not of the parameter itself. A parameter name is just a syntactic artifact of the language. It does not get carried over to the compiled result.

Categories

Resources