0

假设我有以下变量:

Public Shared x As String = "100"
Public Shared y As String = "text"
Public Shared z As String = "something"

我得到了一个功能:

Function giveVar(ByVal varName As String) As String 

    Return varName
End Function

但这自然不是我想要的。我想要的是我的函数giveVar返回持有的变量的值giveVar。例如,我打电话给giveVar("x")我希望我的函数返回"100"

当然,这可以通过 a 来完成,Select Case但这不是我喜欢做的。有人有什么建议吗?甚至可以根据字符串调用值吗?

[编辑:]

Namespace i18n
    public NotInheritable Class Settings
        Public Shared LanguageCode As String
        Public Shared HideAllLocalizedText As Boolean
    End Class

Public NotInheritable Class i18n
        Private Sub New()

        End Sub

        Public Shared Function t(ByVal varName As String) As String
            Select Case Settings.LanguageCode
                Case "en"
                    Return en(varName)
                Case Else
                    Return nl(varName)
            End Select
        End Function

        Private Shared Function en(ByVal varName As String) As String
            Dim ASP_0344 As String = "Cancel"
            Dim ASP_0807 As String = "Click"
            Dim ASP_0808 As String = "here"
            Dim ASP_0812 As String = "Welcome at the login screen..."

            ' These are examples there is a whole bigger list               

            Return CallByName(Me, varName, vbGet)
        End Function

        Private Shared Function nl(ByVal varName As String) As String
            Dim ASP_0344 As String = "Annuleren"
            Dim ASP_0807 As String = "Klik"
            Dim ASP_0808 As String = "hier"
            Dim ASP_0812 As String = "Welkom op het inlogscherm..."

            Return CallByName(Me, varName, vbGet)
        End Function

    End Class
End Namespace

我认为这到目前为止有效,但我在CallByName(Me, varName, vbGet)at上收到以下错误Me"me is only valid within an instance method"

4

5 回答 5

3

您所问的最简单的实现可能是Dictionary

例子

// c#
var values = new Dictionary<string, string>();
values["x"] = "100";
values["y"] = "text";
values["z"] = "something";

public string GiveVar( string name )
{
   return values[name];
}
'vb.net
Dim values As New Dictionary(Of String, String)
values.Add("x", "100")
values.Add("y", "text")
values.Add("z", "something")

Function giveVar(ByVal varName As String) As String 
    Return values(varName)
End Function

此时,您实际上并不需要一个函数来获取该值。您可以只使用字典上的索引器。

如果直到运行时才知道成员的名称,则可以使用反射反射会带来(有时是巨大的)性能损失,并可能导致代码脆弱/不直观,但是——正确使用——是一个强大的工具。

Expando 对象也可用于创建类似的代码。通常这不是正确的路径,除非您使用 expando 对象来改进结构较少的数据。

于 2012-04-12T08:28:05.167 回答
2

是的,您可以使用反射来做到这一点:

Imports System.Reflection

Public Class Test

  Public Shared x As String = "100"
  Public Shared y As String = "text"
  Public Shared z As String = "something"

  Function giveVar(ByVal varName As String) As String

    Dim pi As FieldInfo = Me.GetType().GetField(varName)

    Return (pi.GetValue(Me)).ToString()

  End Function

End Class

测试它:

Module Module1

  Sub Main()

    Dim t As New Test

    Console.WriteLine("x: " & t.giveVar("x"))
    Console.WriteLine("y: " & t.giveVar("y"))
    Console.WriteLine("z: " & t.giveVar("z"))

    Console.ReadKey()

  End Sub

End Module

您需要导入 System.Reflection 命名空间才能使其工作,还需要在定义 giveVar() 函数的类中定义变量,否则您需要将拥有变量的对象作为参数传递和替换 Me.GetType() 调用。

最后,这不适用于局部变量。

于 2012-04-12T08:35:09.937 回答
1

尝试

CallByName (Me, "x", VbGet)
于 2012-04-12T08:35:55.910 回答
0

将您的字符串放入 a 中Hashtable,您可以轻松地通过键检索它们。您仍然可以拥有使用硬编码键查找的公共属性。

于 2012-04-12T08:28:01.050 回答
0

通用解决方案怎么样

在c#中

using System.Reflection;

public class MyClass
{
    public static string x = "1";
    public static int y = 2;

    public static bool TryGetValue<T>(string variableName, out T value)
    {
        try
        {
            var myType = typeof(MyClass);
            var fieldInfo = myType.GetField(variableName);
            value = (T)fieldInfo.GetValue(myType);
            return true;
        }
        catch
        {
            value = default(T);
            return false;
        }
    }
}

你可以这样使用

string xValue;
if (MyClass.TryGetValue("x", xValue))
{
    // it worked
}
else
{
    // oops, x is not there or it will not cast to string
}

int yValue;
if (MyClass.TryGetValue("y", yValue))
{
    // it worked
}
else
{
    // oops, x is not there or it will not cast to int
}

但请记住,与

string valueX = MyClass.x;
int valueY = MyClass.y;

代码更短,您可以在编译时进行类型检查。

于 2012-04-12T09:03:41.123 回答