基于字符串值在 .NET 中创建方法调用
现在,我的代码看起来像这样:
Private Sub ShowReport(ByVal reportName As String)
Select Case reportName
Case "Security"
Me.ShowSecurityReport()
Case "Configuration"
Me.ShowConfigurationReport()
Case "RoleUsers"
Me.ShowRoleUsersReport()
Case Else
pnlMessage.Visible = True
litMessage.Text = "The report name """ + reportName + """ is invalid."
End Select
End Sub
有没有办法创建使用我的方法命名约定来简化事情的代码? 这是一些描述我正在寻找的内容的伪代码:
Private Sub ShowReport(ByVal reportName As String)
Try
Call("Show" + reportName + "Report")
Catch ex As Exception
'method not found
End Try
End Sub
Right now, I have code that looks something like this:
Private Sub ShowReport(ByVal reportName As String)
Select Case reportName
Case "Security"
Me.ShowSecurityReport()
Case "Configuration"
Me.ShowConfigurationReport()
Case "RoleUsers"
Me.ShowRoleUsersReport()
Case Else
pnlMessage.Visible = True
litMessage.Text = "The report name """ + reportName + """ is invalid."
End Select
End Sub
Is there any way to create code that would use my method naming conventions to simplify things? Here's some pseudocode that describes what I'm looking for:
Private Sub ShowReport(ByVal reportName As String)
Try
Call("Show" + reportName + "Report")
Catch ex As Exception
'method not found
End Try
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
“最接近您的问题”的解决方案。
您可以从这些报告中创建委托,并通过在哈希表中查找匹配的字符串来调用它们:
这样您就可以添加任意数量的报告,并且您反映它们的能力以及反射的性能命中率都不会很高。这是一个问题。
The "Closest to your question" solution.
You could make delegates out of those reports, and call them by looking up the matching String in a Hashtable:
That way you can add as many reports as you like, and your ability to reflect them, and the perf hit of reflection, aren't an issue.
如果我正确理解了这个问题,您将不得不使用反射来查找方法“show”+reportName,然后间接调用它:
半生不熟的示例:
在那里插入您自己的逻辑来组成要调用的方法的名称。
有关详细信息,请参阅 MethodInfo 和 Assembly 的 MSDN 文档。
If i understand the question correctly, you'll have to use Reflection to find the method "show" + reportName and then invoke it indirectly:
Half-baked example:
Insert your own logic there to make up the name for the method to call.
See MSDN documentation of MethodInfo and Assembly for details.
使用反射:
但我同意之前的观点,最好不要改变你的原始代码;-)
Using reflection:
But I agree with ago, better not change your original code ;-)
Python(和 IronPython)可以非常轻松地完成这件事。 但对于 .Net,您需要使用反射。
在 C# 中: http://www.dotnetspider .com/resources/4634-Invoke-me-ods-dynamically-using-reflection.aspx
我的 VB.Net 快速移植:
Python (and IronPython) can do this thing very easily. With .Net though, you need to use reflection.
In C#: http://www.dotnetspider.com/resources/4634-Invoke-me-ods-dynamically-using-reflection.aspx
My quick port to VB.Net:
使用反射。 在
System.Reflection
命名空间中,您需要在类型上使用GetMethod("methodName")
获取所需方法的MethodInfo
对象包含方法。获得
MethodInfo
对象后,您可以使用该对象实例和任何参数调用.Invoke()
。例如:
Use reflection. In the
System.Reflection
namespace you need to get aMethodInfo
object for the method you want, usingGetMethod("methodName")
on the type containing the method.Once you have the
MethodInfo
object, you can call.Invoke()
with the object instance and any parameters.For Example:
您可以使用反射。 虽然就我个人而言,我认为你应该坚持使用 switch 语句。
抱歉,我正在做C#。 只需将其翻译为 VB.NET 即可。
You can use reflection. Though personally, I think you should just stick with the switch statement.
Sorry, I'm doing C#. Just translate it to VB.NET.
你有一个更深层次的问题。 你的琴弦太重要了。 谁在给你传弦? 你能让他们不那么做吗?
坚持使用 switch 语句,因为它将您的内部实现(方法名称)与外部视图分离。
假设您将其本地化为德语。 你要重命名所有这些方法吗?
You've got a deeper problem. Your strings are too important. Who is passing you strings? can you make them not do that?
Stick with the switch statement, as it decouples your internal implementation (method names) from your external view.
Suppose you localize this to German. You gonna rename all those methods?
反射 API 允许您从方法获取
MethodInfo
,然后动态调用它的Invoke
。 但就你的情况而言,这太过分了。您应该考虑拥有一个按字符串索引的委托字典。
Reflection API allows you to get a
MethodInfo
from a method, then callingInvoke
dynamically on it. But it is overkill in your case.You should consider having a dictionary of delegates indexed by strings.
您可以使用反射来执行此操作,但说实话,我认为这对于您的特定场景(即同一类中的代码和 switch() )来说过于复杂。
现在,如果您将应用程序设计为将每种报告类型包含在其自己的程序集中(有点像加载项/插件架构)或捆绑在单个外部程序集中,那么您可以将报告程序集加载到应用程序域中,然后使用反射来做这种事情。
You could use reflection to do this but to be honest I think it's overcomplicating things for your particular scenario i.e. code and switch() in the same class.
Now, if you had designed the app to have each report type in its own assembly (kinda like an add-in/plugin architecture) or bundled in a single external assembly then you could load the reporting assemblie(s) into an appdomain and then use reflection to do this kinda thing.
这是C#,应该很容易将其转换为VB。 如果需要将参数传递到方法中,可以将它们添加到 Invoke 的第二个参数中。
This is C#, should be easy enough to turn it into VB. If you need to pass parameter into the method, they can be added in the 2nd argument to Invoke.
在 VB .Net MSVS 2015 为我工作
Worked for me in VB .Net MSVS 2015
您可以使用 System.Reflection。 有关详细信息,请参阅此代码项目文章。
You can, using System.Reflection. See this code project article for more information.