Benjamin Benjamin - 1 year ago 76
C# Question

Get the name of the first argument in an extension method?

string thing = "etc";
thing = thing.GetName();
//now thing == "thing"

Is this even possible?

public static string GetName(this object obj)
return ... POOF! //should == "thing"

Answer Source

No. At the point you're using it, the "name" would be "obj" - This could be retrieved (with debugging symbols in place) via MethodBase.GetCurrentMethod().GetParameters()[0].Name.

However, you can't retrieve the variable name from the calling method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download