Search code examples
c#function-handle

Get the handle of a member of a class that has no instantiation yet


This question is connected (but NOT duplicate) of this one. Consider that I am fairly new to C#. I would like, if possible, to take the handle of a member of an object that has not been instantiated jet, in order to use it later on. Following previous question, I would like to do something like

List<Action<YourClass>> lst = new List<Action<YourClass>>;

lst.Add(x => x.Member1);
lst.Add(x => x.Member2);

Member1 and Member2 are not supposed to be static member, as their value depend on the state of the object they are member of. Basically I want the handle of their "name", so that I can use it later on when the objects are instantiated. I was thinking about an approach based on string which value is the member name, but maybe there is a better way? Thanks.


Solution

  • I found the solution thanks to Henk comment:

    Func<myObj, Vector> getVect = new Func<myObj, Vector> 
    getVect= (myObj => myObj.objVector);
    

    where objVector is NOT a method, but a member of the myObj. I call getVect in this way:

    Vector a= getVect(someObj)