-5

我想告诉我们我创建的字符串的值,并在我的代码中写出该值。在这个例子中,我不知道字符串“属性”的值:

public void EditUser(Int32 user_no, String attribut, String change)
{
    tmpUser = GetUser(user_no);

    if (attribut.Equals("username"))
    {
        tmpUser.username = change;
    }
    else if (attribut.Equals("mail"))
    {
        tmpUser.mail = change;
    }
    else
    {
        tmpUser.password = change;
    }
}

我知道我不能这样做:

tmpUser.attribut = 改变;

有没有办法做到这一点并避免使用 if else 语句。

4

1 回答 1

-1

不,不是那样的。您可以做的最接近的事情是使用从类Reflection中获取正确的方法重载。而它。WriteLineConsoleInvoke

例如:

var method= typeof(Console)
                 .GetMethod("WriteLine",
                            BindingFlags.Public | BindingFlags.Static,
                            null,
                            new [] { typeof(string) },
                            null);

method.Invoke(null, new object[] { "Hello" });
于 2015-01-30T22:50:00.117 回答