Is it possible to retrieve the field name, where the returned object is assigned to using the StackTrace
object and reflection?
In this example, the CreateFooObject
method needs to know the name of the field to which the returned object is assigned to ("Foo"). The previous stack frame is obviously .cctor
, however is there a way to get the actual field in question?
public static readonly object Foo = CreateFooObject();
public static object CreateFooObject()
{
StackFrame[] stack = new StackTrace().GetFrames();
// get name of field: "Foo"
string fieldName = ...;
return ...;
}
Aucun commentaire:
Enregistrer un commentaire