C # C ++ equivalent mem_fun?

I want to do something like CY:

    class Container {
        //...
        public void ForEach(Action method) {
            foreach (MyClass myObj in sequence) myObj.method();
        }
    }

    //...
    containerObj.ForEach(MyClass.Method);

      

In C ++, I would use something like std :: mem_fun. How can I do this in C #?

0


source to share


2 answers


This should work in C # 3.0:



class Container 
{        
//...        
public void ForEach(Action<MyObj> method) 
{            
    foreach (MyClass myObj in sequence) method(myObj);        
}    
}   

//...    containerObj.ForEach( myobj => myObj.Method() );

      

+2


source


Assuming that:

    public void ForEach(Action method) {
        foreach (MyClass myObj in sequence) method(myObj);

      



not good for you, then I think you are stuck with reflection.

0


source







All Articles