Finding the variable name passed to a function

2018-12-31 09:16发布

Let me use the following example to explain my question:

public string ExampleFunction(string Variable) {
    return something;
}

string WhatIsMyName = "Hello World"';
string Hello = ExampleFunction(WhatIsMyName);

When I pass the variable "WhatIsMyName" to the example function, I want to be able to get a string of the original variables name. Perhaps something like:

Variable.OriginalName.ToString()

Is there any way to do this?

标签: c#
16条回答
若你有天会懂
2楼-- · 2018-12-31 09:40

No. A reference to your string variable gets passed to the funcion--there isn't any inherent metadeta about it included. Even reflection wouldn't get you out of the woods here--working backwards from a single reference type doesn't get you enough info to do what you need to do.

Better go back to the drawing board on this one!

rp

查看更多
路过你的时光
3楼-- · 2018-12-31 09:42

GateKiller, what's wrong with my workaround? You could rewrite your function trivially to use it (I've taken the liberty to improve the function on the fly):

static string sMessages(Expression<Func<List<string>>> aMessages) {
    var messages = aMessages.Compile()();

    if (messages.Count == 0) {
        return "";
    }

    StringBuilder ret = new StringBuilder();
    string sType = ((MemberExpression)aMessages.Body).Member.Name;

    ret.AppendFormat("<p class=\"{0}\">", sType);
    foreach (string msg in messages) {
        ret.Append(msg);
        ret.Append("<br />");
    }
    ret.Append("</p>");
    return ret.ToString();
}

Call it like this:

var errors = new List<string>() { "Hi", "foo" };
var ret = sMessages(() => errors);
查看更多
刘海飞了
4楼-- · 2018-12-31 09:43

Thanks for all the responses. I guess I'll just have to go with what I'm doing now.

For those who wanted to know why I asked the above question. I have the following function:

string sMessages(ArrayList aMessages, String sType) {
    string sReturn = String.Empty;
    if (aMessages.Count > 0) {
        sReturn += "<p class=\"" + sType + "\">";
        for (int i = 0; i < aMessages.Count; i++) {
            sReturn += aMessages[i] + "<br />";
        }
        sReturn += "</p>";
    }
    return sReturn;
}

I send it an array of error messages and a css class which is then returned as a string for a webpage.

Every time I call this function, I have to define sType. Something like:

output += sMessages(aErrors, "errors");

As you can see, my variables is called aErrors and my css class is called errors. I was hoping my cold could figure out what class to use based on the variable name I sent it.

Again, thanks for all the responses.

查看更多
梦该遗忘
5楼-- · 2018-12-31 09:46

What you want isn't possible directly but you can use Expressions in C# 3.0:

public void ExampleFunction(Expression<Func<string, string>> f) {
    Console.WriteLine((f.Body as MemberExpression).Member.Name);
}

ExampleFunction(x => WhatIsMyName);

Note that this relies on unspecified behaviour and while it does work in Microsoft’s current C# and VB compilers, and in Mono’s C# compiler, there’s no guarantee that this won’t stop working in future versions.

查看更多
登录 后发表回答