提取方法的名字来自反射或硬codeD串串?反射、名字、方法、codeD

2023-09-05 04:27:05 作者:劳资就是小清新

我打电话从属性的方法。我要通过属性名称作为方法的属性。我的属性是

I am calling a method from a property. I have to pass property name as attribute of that method. My Property is

string lcl_name = string.Empty;

public string Name 
{
get { return lcl_name; }
set 
    {
        if (lcl_name != value) 
        {
            lcl_name = value;
            Foo_Method(GetCorrectPropertyName(MethodBase.GetCurrentMethod().Name));
        }
}
}

和方法

public string GetCorrectPropertyName(string propertyName)
{
    return propertyName.StartsWith("set_") || propertyName.StartsWith("get_") ? 
           propertyName.Substring(4) : string.Empty;
}

我的学长说,我不应该叫反思,并通过直接的字符串的方法这样

My seniors say that i should not call Reflection and pass direct string to method this way

Foo_Method("Name");

,但在这种情况下,因为这将是很难codeD和如果属性名称被改变,则方法呼叫必须被相应地改变。

but in that case since it would be hardcoded and if property name is changed, then method call have to be changed accordingly.

所以我的问题是他们中的一个将在效率方面会更好?有没有到我无视别的东西,我的学长看到?

So my question is which one of them would be better in terms of efficiency? Is there something else that my seniors are seeing to which I am oblivious to?

推荐答案

也许前pression树木可以帮助你在这里。

Maybe Expression Trees can help you here.

Foo_Method 取属性名作为字符串

void Foo_Method(string name)
{

}

使用类型为防爆pression 的参数通过的 MemberEx pression :

use a parameter of type Expression to retrieve the property name via a MemberExpression:

void Foo_Method(Expression<Func<NameOfYourClass, String>> exp)
{
    var propertyName = ((MemberExpression)exp.Body).Member.Name;
}

和调用它

public string Name 
{
    get { return lcl_name; }
    set 
    {
        if (lcl_name != value) 
        {
            lcl_name = value;
            // Foo_Method("Foo"); string is bad
            Foo_Method(x => x.Name);
        }
    }   
}

这样,重命名名称的时候,你不打断你的code,因为 Foo_Method(X =&GT; x.Name )被更名为太(使用IDE的重构功能重命名时,当然)。

This way, when renaming Name, you don't break your code, since Foo_Method(x => x.Name) gets renamed too (when using your IDEs refactoring capabilities for renaming, of course).

编辑:

要回答您的评论:

如果你真的不能过载增加Foo_Method,你当然可以只创建另一种方法:

If you really can't add an overload to Foo_Method, you can of course just create another method:

if (lcl_name != value) 
{
    lcl_name = value;
    Foo_Method(GetPropName(x => x.Name));
}

...

string GetPropName(Expression<Func<NameOfYourClass, String>> exp)
{
    return ((MemberExpression)exp.Body).Member.Name;
}

EDIT2:

要回答你的其他意见:

您可以创建一个扩展方法

You could create an extension method

public static class Extensions
{
    public static string GetPropName<T>(this T t, Expression<Func<T, String>> exp)
    {
        return ((MemberExpression)exp.Body).Member.Name;
    }
}

var propertyName = yourInstace.GetPropName(y => y.Name);

但你不必,因为前pressions做工精细,没有任何实例。

but you don't have to, since the expressions work fine without any instance.

public static class Extensions
{
    public static string GetPropName<T>(Expression<Func<T, String>> exp)
    {
        return ((MemberExpression)exp.Body).Member.Name;
    }
}

var propertyName  = Extensions.GetPropName<YourClass>(y => y.Name);

关键是要在这里使用泛型。

the trick is to use generics here.

第一种方法是这样的在VB.Net

The first method would look like this in VB.Net

Public Function GetPropName(Of TClass, TProperty)(exp As Expression(Of Func(Of TClass, TProperty))) as String
    Return DirectCast(exp.Body, MemberExpression).Member.Name
End Function

...

GetPropName(Function(x) x.Name)