使用反射按名称获取方法时如何重构?
假设我通过以下方式获得 MethodInfo:
Assembly assembly = Assembly.Load(assemblyName);
Type type = assembly.GetType(nameSpaceName+"."+className);
MethodInfo mi = type.GetMethod("myMethod", bf); // bf are the binding flags.
但后来我决定更改 myMethod
的大小写/名称。
有没有办法:
- 重构以便更改字符串中的名称。
- 更改反射调用,以便它在不使用方法名称作为字符串的情况下获取方法?
这样做的原因是我可以测试需要使用反射的代码,但我不想要求任何人都更改代码中方法的名称。
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:
- Refactor so that it changes the name in the string.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 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 themethodName
more easily.您可以使用自定义属性,并用此属性装饰您的方法。然后,您可以通过属性中定义的 ID 来获取方法,而不是通过名称来获取方法。这样方法名称就可以根据需要经常更改......只是一个想法。
用法:
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.
Usage:
您对相关类型有具体的参考吗?
即使您没有明确地指定该方法,也可以将该方法设为通用方法。
事实上,你完全可以不加反思地做到这一点:
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.
In fact, you could do this without reflection at all:
对于方法和属性,请尝试使用表达式树。
您可以从中获取与反射相关的信息,从而节省编译时检查并启用自动重构。
我相信您也可以获得与程序集相关的信息和命名空间名称。
只需编写几个辅助函数即可从表达式树中检索此类信息。
您可以在此处找到多个辅助函数
他们允许编写这样的代码:
这与
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:
which is the same as