Can I write the method name without using a String to pass the name of the method? (c#)

dlopezgonzalez

I have got this class:

class foo
{
   int val;
   public int Val
   {
      set{ val = values; },
      set{ val = values; }
   }
}

I need to pass the property name to a DataBinding:

String propertyName = "Val";
ctrl.DataBindings.Add(propertyName, object, dataMember, true, DataSourceUpdateMode.Never);

I want to do something like this:

propertyName = typeof(foo).methods.Val.toString();
Lucas Trzesniewski

If you can use C#6, you have the nameof operator, which does just that.

string propertyName = nameof(foo.Val);

If you use C# 5, you can leverage expression trees:

public static string GetPropertyName<TParent>(Expression<Func<TParent, object>> prop)
{
    var expr = prop.Body;

    if (expr.NodeType == ExpressionType.Convert)
        expr = ((UnaryExpression)expr).Operand;

    if (expr.NodeType == ExpressionType.MemberAccess)
        return ((MemberExpression)expr).Member.Name;

    throw new ArgumentException("Invalid lambda", "prop");
}

Use this helper function like this (assuming it's in a ReflectionHelper class):

string propertyName = ReflectionHelper.GetPropertyName<foo>(x => x.Val);

This way, you can safely use refactorings in your IDE.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery ajax POST in Sitecore duplicates method name

来自分类Dev

Is it possible to parameterise the NUnit test case display name when using ``Ticked method names``?

来自分类Dev

BCEL - Get Class name,element names ,and method names

来自分类Dev

在类方法中使用self.method_name

来自分类Dev

奇怪的RSpec错误`undefined method model_name`

来自分类Dev

RSPEC test NAME ERROR - Undefined Local variable or method

来自分类Dev

Undefined method `name' for nil:NilClass for a property from a joined table

来自分类Dev

Neo4j gem undefined method'name'for CypherNode

来自分类Dev

Symfony 2 - How can I get current route name using bundle JMSI18nRoutingBundle

来自分类Dev

Why can I access a protected method in a test?

来自分类Dev

NG2 : Pass attribute name to component using a variable

来自分类Dev

How do I pass async method as Action or Func

来自分类Dev

using function in name space without back-slash

来自分类Dev

&string_name或string_name(%s)在C中打印字符串

来自分类Dev

How to return a NULL from a templated method, without using a pointer

来自分类Dev

sheet = sheetList.FirstOrDefault(ws => ws.Name == string.Format(name,i))是什么?意思是?

来自分类Dev

How can I target a <select> form element by name with CSS?

来自分类Dev

How can I convert an Integer to month name in Groovy?

来自分类Dev

Why can't I have a folder and a file with the same name?

来自分类Dev

How do I mock a method call inside the method I want to test using NUnit?

来自分类Dev

How to parse 'div' without name?

来自分类Dev

Can I use Mockito to insert a delay and then call the real method?

来自分类Dev

How can I discover and compensate for possible flaws in the method of date storage?

来自分类Dev

dart, why can't I use generics on a method

来自分类Dev

how can i check a method in raw php, like oop method_exists()

来自分类Dev

NoMethodError in Articles#new undefined method `model_name' for #<Article:0x000000035a3450>

来自分类Dev

Groovy - String each method

来自分类Dev

How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

来自分类Dev

String name [] = {};和有什么区别?和String [] name = {};

Related 相关文章

  1. 1

    jQuery ajax POST in Sitecore duplicates method name

  2. 2

    Is it possible to parameterise the NUnit test case display name when using ``Ticked method names``?

  3. 3

    BCEL - Get Class name,element names ,and method names

  4. 4

    在类方法中使用self.method_name

  5. 5

    奇怪的RSpec错误`undefined method model_name`

  6. 6

    RSPEC test NAME ERROR - Undefined Local variable or method

  7. 7

    Undefined method `name' for nil:NilClass for a property from a joined table

  8. 8

    Neo4j gem undefined method'name'for CypherNode

  9. 9

    Symfony 2 - How can I get current route name using bundle JMSI18nRoutingBundle

  10. 10

    Why can I access a protected method in a test?

  11. 11

    NG2 : Pass attribute name to component using a variable

  12. 12

    How do I pass async method as Action or Func

  13. 13

    using function in name space without back-slash

  14. 14

    &string_name或string_name(%s)在C中打印字符串

  15. 15

    How to return a NULL from a templated method, without using a pointer

  16. 16

    sheet = sheetList.FirstOrDefault(ws => ws.Name == string.Format(name,i))是什么?意思是?

  17. 17

    How can I target a <select> form element by name with CSS?

  18. 18

    How can I convert an Integer to month name in Groovy?

  19. 19

    Why can't I have a folder and a file with the same name?

  20. 20

    How do I mock a method call inside the method I want to test using NUnit?

  21. 21

    How to parse 'div' without name?

  22. 22

    Can I use Mockito to insert a delay and then call the real method?

  23. 23

    How can I discover and compensate for possible flaws in the method of date storage?

  24. 24

    dart, why can't I use generics on a method

  25. 25

    how can i check a method in raw php, like oop method_exists()

  26. 26

    NoMethodError in Articles#new undefined method `model_name' for #<Article:0x000000035a3450>

  27. 27

    Groovy - String each method

  28. 28

    How can I make Linux system calls from a C/C++ application, without using assembly, and in a cpu-independent manner?

  29. 29

    String name [] = {};和有什么区别?和String [] name = {};

热门标签

归档