Get Name of Action/Func Delegate
It's giving you the name of the method which is the action of the delegate. That just happens to be implemented using a lambda expression.
You've currently got a delegate which in turn calls Bark. If you want to use Bark
directly, you'll need to create an open delegate for the Bark
method, which may not be terribly straightforward. That's assuming you actually want to call it. If you don't need to call it, or you know that it will be called on the first argument anyway, you could use:
private T Get<T>(T task, Action method) where T : class
{
string methodName = method.Method.Name //Should return Bark
}
private void MakeDogBark()
{
dog = Get(dog, dog.Bark);
}
You could get round this by making the parameter an expression tree instead of a delegate, but then it would only work if the lambda expression were just a method call anyway.
Static and Instance methods with the same name?
No you can't. The reason for the limitation is that static methods can also be called from non-static contexts without needing to prepend the class name (so MyStaticMethod() instead of MyClass.MyStaticMethod()). The compiler can't tell which you're looking for if you have both.
You can have static and non-static methods with the same name, but different parameters following the same rules as method overloading, they just can't have exactly the same signature.
Get the name of the first argument in an extension method?
No. At the point you're using it, the "name" would be "obj" - This could be retrieved (with debugging symbols in place) via MethodBase.GetCurrentMethod().GetParameters()[0].Name.
However, you can't retrieve the variable name from the calling method.
C#: how to get an object by the name stored in String?
No, it's not.
Objects don't have names - variables do. An object may be referenced by any number of variables: zero, one or many.
What you can do, however, is get fields (static or instance variables) by name (using Type.GetField
) and get the values of those fields (for a specific instance, if you're using instance variables).
Depending on what you're trying to do, you might also want to consider a dictionary from names to objects.
How to get property name from within getter/setter of that property?
After a while I figure out a solution as below, which is quite convenient to me.
string SomeProperty
{
get
{
return GetDesignModeValue(() => this.SomeProperty);
}
}
string GetDesignModeValue<T>(Expression<Func<T>> propertyExpression)
{
var propName = (propertyExpression.Body as MemberExpression).Member.Name;
var value = string.Format(
"<{0}>"
, propName);
return value;
}
So, from now on, it is much much easier for us to show the binding properties' mocking
value when in design mode.
I hope you would find it helpful somedays!
How to get name of a class property?
The result of ClassName.IntProperty
is just an integer value. As soon as it's executed and the result is returned, there's no trace of it having come from IntProperty
.
If you're using .NET 3.5 you can use an expression tree instead, usually created via a lambda expression:
Expression<Func<int>> exp = () => ClassName.IntProperty;
You can then compile and execute the expression and separately find out what it's doing (retrieving IntProperty
in this case). I'm not really sure whether this is suitable for what you want to do though.
If you do work out how to save the property name in the database, then GetProperty
is the way to go on the retrieval front.
Perhaps if you could give more context in the question in terms of how you want to use this, we could help more. You've shown just an expression - if you could show it in terms of where you'd be using it, that would be great.
EDIT: You've expanded the property, but not how it's being called. Do you need to call it directly, rather than just fetching the list of properties using Type.GetProperties
and storing the list of property names in the database?
Again, if you could show the code which calls the property, and how you want it to interact with the database, we may be able to make more progress.
Related Topics
How to Return a Value from a Void Closure in Swift
Initializing Property via Closure
How to Masking the Last Number in Swift
Swift Firebase Check If User Exists
Make Touch-Area for Sklabelnode Bigger for Small Characters
Guard Let Error: Initializer for Conditional Binding Must Have Optional Type Not 'String'
How to Place Uisearchcontroller to the Navigationtitle and How to Enable and Disable It by Button
Loading Views into Nscontainerview with Swift
Integrating Congnito User Pools with Amazon Cognito Identity
Coerced to Any' But Property Is of Type Uicolor
Value of Type 'Tags' Has No Member 'Lastused'
Swift Spritekit Arc for Dummies
How to Customize the Font and Appearance of a Uialertcontroller in the New Xcode W/ iOS8