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

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.

Related

Can I set the value of decimal variable within an extension method? [duplicate]

This question already has answers here:
Impossible to use ref and out for first ("this") parameter in Extension methods?
(6 answers)
Closed 7 years ago.
I have created a void extension method which can be used with decimal data types. I wanted to be able to modify the this parameter variable inside the scope of the method. This is the code for my extension method:
public static void SetAndConvertIfHasValue(this decimal assignTo, double? valueToAssign)
{
if (valueToAssign.HasValue)
assignTo = (decimal)valueToAssign.Value;
else
assignTo = 0m;
}
However, when I call it:
data.MyDecimalToSet.SetAndConvertIfHasValue(nullableDouble);
data.MyDecimalToSet is not set to the value in nullableDouble if it has one.
In debug if I step into the extension method, assignTo is changed to the correct value, this change just doesn't bubble up to data.MyDecimalToSet.
At this point I have decided to use a standard method rather than an extension method as a solution to this problem, however I was curious as to why this doesn't work? And whether there is a way around it, or if it simply is impossible?
This doesn't work because when you pass a variable to a method, a copy of that variable is passed. When you operate on that copy, you will only be changing the copy.
Note that this will happen regardless of whether the variable is a reference or a value type. However, if you pass a reference type, you can change the contents of the reference type, since the reference passed to the method will be copied, but it will still reference the original object.
(decimal is a value type, so that last point does not apply to it.)
You are doing it the right way if you use a standard method instead.

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 a property name and a method name be same in C#? [duplicate]

This question already has answers here:
How can there be ambiguity between a property getter and a method with one argument?
(4 answers)
Closed 9 years ago.
I have a class which contains a property:
public bool IsMandatory {get;set;}
Now I am adding a method IsMandatory(string str).
public bool IsMandatory(string str)
{
//return false;
//return true;
}
I am getting a compile time error that
the type already contains a definition for 'IsMandatory'
Can't a method name and property name be same in C# ? We use a method and property in different way, why is this giving compile error ?
It's a compiler error because it would cause confusion if the names could be the same. There are some cases where ambiguity could result - for example, when using Action delegates and so on, where methods do not need to have parenthesis, and when using var.
In short: It is just not allowed. Member names (field, property, and method) must be
unique.

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.

How to get parameter value from StackTrace [duplicate]

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.

Categories

Resources