使用反射按名称获取方法时如何重构?

发布于 2024-10-29 20:35:11 字数 434 浏览 1 评论 0原文

假设我通过以下方式获得 MethodInfo:

Assembly assembly = Assembly.Load(assemblyName);
Type type = assembly.GetType(nameSpaceName+"."+className);
MethodInfo mi = type.GetMethod("myMethod", bf); // bf are the binding flags.

但后来我决定更改 myMethod 的大小写/名称。

有没有办法:

  1. 重构以便更改字符串中的名称。
  2. 更改反射调用,以便它在不使用方法名称作为字符串的情况下获取方法?

这样做的原因是我可以测试需要使用反射的代码,但我不想要求任何人都更改代码中方法的名称。

Suppose I get a MethodInfo in the following way:

Assembly assembly = Assembly.Load(assemblyName);
Type type = assembly.GetType(nameSpaceName+"."+className);
MethodInfo mi = type.GetMethod("myMethod", bf); // bf are the binding flags.

But later I decide to change the case/name of myMethod.

Is there a way to either:

  1. Refactor so that it changes the name in the string.
  2. Change the reflection call so it gets the method without using the method's name as a string?

The reason for this is so I can test my code which requires the use of reflection, but I'd rather not require that nobody ever change the name of the methods in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天气好吗我好吗 2024-11-05 20:35:11

如果您使用 Visual Studio 进行重构,则可以选择搜索文字字符串和注释以查找名称并更改它们。不过,我强烈建议您在使用该选项时使用预览,以验证您是否只更改了您期望的部分。

当然,您可以使用像 internal const string methodName = "methodName"; 这样的常量,这样您就只有一次文字字符串。当您重构方法名称时,您可以手动更改一个字符串文字。您还可以更轻松地重命名 methodName

If you use Visual Studio to do the refactoring, there is an option to search literal strings and comments for the name and change those too. I highly recommend using the preview when using that option, though, to verify that you're only changing the parts you expect.

Of course, you could use a constant like internal const string methodName = "methodName"; so that you only have the literal string once. You could manually change the one string literal when you refactor the method name. You'd also be able to rename the methodName more easily.

橘虞初梦 2024-11-05 20:35:11

您可以使用自定义属性,并用此属性装饰您的方法。然后,您可以通过属性中定义的 ID 来获取方法,而不是通过名称来获取方法。这样方法名称就可以根据需要经常更改......只是一个想法。

[AttributeUsage(AttributeTargets.Method)]
public class CustomMethodAttribute : Attribute
{
    public string ID { get; set; }
}

用法:

[CustomMethodAttribute(ID = "UniqueIDHere")]
public void Test()
{

}

You could use a custom attribute, and decorate your methods with this attribute. Then instead of getting the method by its name, you could get it by the ID defined in the attribute. That way the method name could change as often as it needs to...just a thought.

[AttributeUsage(AttributeTargets.Method)]
public class CustomMethodAttribute : Attribute
{
    public string ID { get; set; }
}

Usage:

[CustomMethodAttribute(ID = "UniqueIDHere")]
public void Test()
{

}
第七度阳光i 2024-11-05 20:35:11

您对相关类型有具体的参考吗?

即使您没有明确地指定该方法,也可以将该方法设为通用方法。

public void TestMethod<TargetType>(object o)
{
    if (typeof(TargetType).IsAssignableFrom(o.GetType())) {
        TargetType strongType = o as TargetType;
        strongType.myMethod();
    }
}

事实上,你完全可以不加反思地做到这一点:

public void TestMethod<TargetType>(object o)
{
    if (o is TargetType) {
        TargetType strongType = o as TargetType;
        strongType.myMethod();
    }
}

Do you have a concrete reference to the type in question?

Even if you don't have it explicitly, you can make the method generic.

public void TestMethod<TargetType>(object o)
{
    if (typeof(TargetType).IsAssignableFrom(o.GetType())) {
        TargetType strongType = o as TargetType;
        strongType.myMethod();
    }
}

In fact, you could do this without reflection at all:

public void TestMethod<TargetType>(object o)
{
    if (o is TargetType) {
        TargetType strongType = o as TargetType;
        strongType.myMethod();
    }
}
幽梦紫曦~ 2024-11-05 20:35:11

对于方法和属性,请尝试使用表达式树。
您可以从中获取与反射相关的信息,从而节省编译时检查并启用自动重构。
我相信您也可以获得与程序集相关的信息和命名空间名称。
只需编写几个辅助函数即可从表达式树中检索此类信息。

您可以在此处找到多个辅助函数
他们允许编写这样的代码:

FirePropertyChanged(() => PropertyName);

这与

FirePropertyChanged("PropertyName");

For methods and properties try using expression trees.
You can get reflection-related information from them, saving compile-time checking and enabling automatic refactoring.
I believe you can get assembly-related information and namespace names as well.
Just write several helper functions which can retrieve such information from expression trees.

You can find several helper functions here
They allow write such code:

FirePropertyChanged(() => PropertyName);

which is the same as

FirePropertyChanged("PropertyName");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文