Benjamin Benjamin - 1 month ago 5
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

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.