C# Convert method to Action<object>

Posted on 2011-04-18
Last Modified: 2012-05-11
If I have a method such as:

public void DoSomething(object parm) { }

then within this class I can then do this:

void DefineMethod()
    Action<object> someMethod = DoSomething;

But how do I set 'someMethod' in classB when 'DoSomething()' is in another classA? Essentially I need to convert the method from ClassA into an Action<object> in classB.

I know I can simply create DefineMethod() with a parameter of type Action<object> to solve the problem. But that is too simple a use case - assume that I want to create an Action<object> for every method in Class A with one call to DefineMethod() in classB (and that call will come from a method in ClassA itself). Essentially I want to loop over the methods in ClassA. I could retrieve methodinfo[] using .GetMethods(), by passing the classA type as a parameter. But I don't know how to convert the method info data to Active<object>.
Question by:esc_toe_account
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    There needs to be some common area that has both objects in scope. Then you can define the appropriate public members to assign from one to the other.

    For example:

    Class Definitions
    class ClassA
    	public void DoSomething(object parm) { }
    class ClassB
    	public void DefineMethod(Action<object> DoSomethingMethod)
    	    Action<object> someMethod = DoSomethingMethod;

    Open in new window

    Scope Example
    class MainClass
    	public static void Main (string[] args)
    		ClassA cA = new ClassA();
    		ClassB cB = new ClassB();

    Open in new window


    Author Comment

    Thanks for responding but I am not looking for a way to pass the method as a parameter. I was concerned that my rushed posting was not clear. Let me restate it in another way.

    Within a method in ClassB I want to 'discover' all the methods in ClassA and for each discovered method I want to cast the method as an Action<Object>, The implied caveat on the solution is that I do not pass the method names from ClassA in the parameter list of the call to the method in ClassB. (However, I can pass other pertinent info such as 'this' or class type info).

    Accepted Solution

    I have found the answer for creating an Action delegate from methodinfo (and the appropriate instance)

    Action<object> action = delegate(object xyz) { methodinfo.Invoke(classInstance, parameters); }

    where of-course parameters only contains one parameter because this is an Action.

    Author Closing Comment

    No one else answered the question

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now