变量和常量的CallByName()函数...... [英] CallByName() function for variables and constants...

查看:65
本文介绍了变量和常量的CallByName()函数......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在或者是否知道一个与'CallByName()'非常相似的函数 - 但是根据字符串值选择变量和/或常量?我们有许多模块和类,每个模块和类包含大约100个左右的常量。我不能在这里发布我正在做的任何事实示例代码,因为我是一名军事承包商,他们有点看不起我这样做。但是,要点是,有时在代码中,我们不知道要引用哪个变量或常量,或者在函数中返回,直到
我们要么从用户那里获得输入,要么另一个函数告诉我们。

Does there exist, or does any one know of, a function which works very similar to 'CallByName()' - but for selecting variables and/or constants based on a string value? We have many modules and classes, each containing roughly 100 or so constants. I can't post any factual sample code here of what I'm doing because I'm a military contractor and they sort of look down on me doing that. But, the gist is that sometimes in code, we don't know which variable or constant to reference or return in a function until we either get input from a user or another function tells us.

以下是我们尝试做的一个模拟示例...

Here is a mock example of what we're trying to do...

Private Const m_ERR_1000_ENUS As System.String ="我的英文(美国)错误消息,错误号为1000."

Private Const m_ERR_1000_ENUS As System.String = "My error message in English (U.S.A.) for error number 1000."

Private Const m_ERR_1000_ESES As System.String ="我的错误消息,西班牙语(西班牙) )错误号为1000."

Private Const m_ERR_1000_ESES As System.String = "My error message in Spanish (Spain) for error number 1000."

Private Const m_ERR_1001_ENUS As System.String ="我的英文(美国)错误消息,错误号为1001。 ;

Private Const m_ERR_1001_ESES As System.String ="我的错误信息在西班牙语(西班牙),错误号为1001."

Private Const m_ERR_1001_ESES As System.String = "My error message in Spanish (Spain) for error number 1001."

Private Const m_ERR_1002_ENUS As System.String ="我的英文(美国)错误消息,错误编号为1002。 ;

Private Const m_ERR_1002_ESES As System.String ="我的错误信息在西班牙语(西班牙),错误号为1002."

Private Const m_ERR_1002_ESES As System.String = "My error message in Spanish (Spain) for error number 1002."

Public Sub Main()

Public Sub Main()

    MsgBox(GetErrMsg(1001,1034))

    MsgBox(GetErrMsg(1001, 1034))

End Sub

'根据提供的错误号和系统区域设置返回错误消息字符串...

'Returns an error message string based on the supplied error number and system locale...

Friend Sub GetErrMsg(ByVal ErrNumber As System.Int32,ByVal Locale
As System.Int32)As System.String

Friend Sub GetErrMsg(ByVal ErrNumber As System.Int32, ByVal Locale As System.Int32) As System.String

    Dim strErrMbr As System.String'保留要选择的成员名称(常量)... 

    Dim strErrMbr As System.String 'Retains the name of the member (constant) to be selected... 

    Dim strLocale As System.String'保留语言环境标识符......

    Dim strLocale As System.String 'Retains the locale identifier...

   选择案例区域设置'确定指定的区域设置...

    Select Case Locale 'Determine the specified locale...

       案例1033'英语(美国)......

        Case 1033 'English (U.S.A.)…

            strLocale =" ENUS"

            strLocale = "ENUS"

       案例1034'西班牙语(西班牙)......

        Case 1034 'Spanish (Spain)…

            strLocale =" ESES"

            strLocale = "ESES"

   结束选择

    End Select

    strErrMbr = strErrMbr.Concat(" m_ERR_",ErrNumber," _",strLocale)'构建要选择的常量的名称...

    strErrMbr = strErrMbr.Concat("m_ERR_", ErrNumber, "_", strLocale) 'Build the name of the constant to be selected...

   

   

    '根据字符串的值选择要引用的常量...

    'Choose which constant to reference based on the string's value...

   返回MemberByName( strErrMbr )'引用相应的常量并返回其文字值(错误消息)...

    Return MemberByName(strErrMbr) 'Reference the appropriate constant and return its literal value (error message)...

End Sub

如果"strErrMbr"结束为" m_ERR_1001_ESES",然后声明" 返回MemberByName( strErrMbr )"将与" 返回
m_ERR_1001_ESES"等等。  MsgBox(MemberByName( strErrMbr ))和MsgBox( m_ERR_1001_ESES )将返回
相同的消息字符串: 
"我的错误西班牙语(西班牙)中错误号为1001的消息。"

If "strErrMbr" ends up as "m_ERR_1001_ESES", then the statement "Return MemberByName(strErrMbr)" would be identical to "Return m_ERR_1001_ESES", and so on.  MsgBox(MemberByName(strErrMbr)) and MsgBox(m_ERR_1001_ESES) would be equal and an return the same message string of: "My error message in Spanish (Spain) for error number 1001."

现在...在任何人发布100种不同的方式之前处理本地化 - 本地化不是我们想要实现的目标。正如我所说,我不能发布任何我们实际想要做的样本。 此外,我们目前拥有的是一个非常庞大的函数
,其中包含一个非常长的Select Case结构,其工作方式类似于:

Now... before anyone posts 100 different ways to handle localization - localization is NOT what we're trying to achieve. As I said, I can't post any sample of what we're actually trying to do.  Also, what we currently have is a really HUGE function with a very long Select Case structure that works something similar to:

选择案例 strErrMbr

   案例  " m_ERR_1000_ENUS"

    Case "m_ERR_1000_ENUS"

       返回 m_ERR_1000_ENUS

        Return m_ERR_1000_ENUS

   案例  " m_ERR_1000_ESES"

    Case "m_ERR_1000_ESES"

       返回 m_ERR_1000_ESES

        Return m_ERR_1000_ESES

   案例  " m_ERR_1001_ENUS"

    Case "m_ERR_1001_ENUS"

       返回 m_ERR_1001_ENUS

        Return m_ERR_1001_ENUS

   案例  " m_ERR_1001_ESES"

    Case "m_ERR_1001_ESES"

       返回 m_ERR_1001_ESES

        Return m_ERR_1001_ESES

结束选择

它运作良好,但是功能非常庞大。而且,每次我们在代码中添加一个新常量(及其字面值)时,我们还必须将它作为可能的返回项添加到函数内部。



 所以......为了减少我们的代码,因此,我们的源文件的大小是否有像"MemberByName()"这样的函数。存在吗? 如果没有,为什么不呢? Microsoft需要像现在一样创建一个适用于变量和常量的工具。 
或者,是否有人对创建这样的函数有任何想法?

It works very well, but is an extremely HUGE function. And, each time we add a new constant (along with its literal value) to our code, we have to also add it to inside the function as a possible return item.

 So... to lessen our code, and therefore, the size of our source files, is there a function like "MemberByName()" in existence?  If not, WHY NOT? Microsoft needs to create one, like now, that works for both variables and constants.  Or, does any one have any ideas as to going about creating such a function?

我们有一些约束。 一个。因为我们使用专有/军事混淆软件,所以使用字典或外部XML文件(或其他不可加密的外部/支持文件)不是选项,所以甚至不要浪费空间建议。 任何
解决方案都必须采用硬编码。 湾创建动态成员(动态常量和/或变量)也不是一种选择。



We have some constraints.  a. Because we use proprietary/military obfuscation software, using dictionaries or external XML files (or other non-encryptable external/support files) are NOT options, so don't even waste space suggesting either.  Any solutions must be hard-coded.  b. Creating dynamic members (constant and/or variables on the fly) is not an option either.

任何有一些人关于如何创建这样一个功能的想法,我们很乐意听取您的意见。 并且...... GO!

Anyone who has some ideas on how to create such a function, we'd love to hear from you.  And... GO!

推荐答案

鉴于您的限制,您可以使用反射并支付较小的管理费用。 您可以通过缓存类型实例或创建临时缓存值的内部字典来降低成本。

Given your limitations you can use reflection and pay the minor overhead cost.  You can mitigate the cost by caching the type instance or creating an internal dictionary of temporary cached values.

假设您保留(或重构)代码以获取strErrMbr字符串成员名称,您可以使用类似这样的代码来获取成员的名称:

Assuming you keep (or refactor) the code to get the strErrMbr string member name, you can use code something like this to get the value of the member by name:

Public Class MockExample
    Private Const m_ERR_1000_ENUS As System.String = "My error message in English (U.S.A.) for error number 1000."
    Private Const m_ERR_1000_ESES As System.String = "My error message in Spanish (Spain) for error number 1000."
    Private Const m_ERR_1001_ENUS As System.String = "My error message in English (U.S.A.) for error number 1001."
    Private Const m_ERR_1001_ESES As System.String = "My error message in Spanish (Spain) for error number 1001."
    Private Const m_ERR_1002_ENUS As System.String = "My error message in English (U.S.A.) for error number 1002."
    Private Const m_ERR_1002_ESES As System.String = "My error message in Spanish (Spain) for error number 1002."

    Private Shared thisType As Type = GetType(MockExample)

    Public Shared Function MemberByName(memberName As String) As String
        Dim info = thisType.GetField(memberName, Reflection.BindingFlags.Static Or Reflection.BindingFlags.NonPublic)
        Return info.GetValue(Nothing)
    End Function
End Class


请注意,GetField参数基于成员类型和同一类中存在的代码。 但如果需要,您可以反映其他类型或访问成员的不同类型或范围。

Note that the GetField parameters are based on the member type and the code existing within the same class.  But you could reflect some other type or access members of a different kind or scope if needed.


这篇关于变量和常量的CallByName()函数......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆