passing a method as a constructor's parameter - c#

While I was looking at some c# code I found a new class declaration which had passed a method as a constructor’s parameter.This is my first time I have seen something like this and it has no meaning for me .Although the whole application works fine I am still interested to know what is the main idea behind the scene.
var c = new List<TheClass>
{
new TheClass(TheMethod);
}
private void TheMethod()
{
//do somthing
}

I guess the TheClass constructor accepts an Action delegate:
public class TheClass
{
private readonly Action _action;
public TheClass(Action action)
{
_action = action;
}
}
This way TheClass can execute the provided action at a later time, and more than once.
For example:
public void DoAction()
{
_action();
}
You could also do:
var theClass = new TheClass(() => TheMethod());

There are many reasons that you might want to pass a method into the constructor of a class.
One of the most important is for so-called Dependency Injection, where want to inject a dependency into a class.
Suppose that a class needs to create an object. Normally, that class would do something like var item = new MyConcreteClass();.
However, that creates a strong dependency between the class creating the object and MyConcreteClass. This can make it harder to unit test or change implementations.
To circumvent this problem, you can do this:
Extract from MyConcreteClass an interface (say IMyInterface) which contains all the things you need to use from inside the class being written.
Pass to the class constructor a Func method which creates and returns a concrete class as IMyInterface.
Inside the class being written, call that Func to create the object rather than creating it directly using new.
The Func is therefore acting as a factory.
Code might look like this:
using System;
namespace Spacelabs.WcfDuplexDemo.Client
{
static class Program
{
static void Main()
{
var myClass = new MyClass(() => new MyConcreteClass());
myClass.DoSomething();
}
}
public interface IMyInterface
{
string MyMethod(int param);
}
public sealed class MyConcreteClass : IMyInterface
{
public string MyMethod(int param)
{
return param.ToString();
}
}
public sealed class MyClass
{
private readonly Func<IMyInterface> createMyInterface;
public MyClass(Func<IMyInterface> createMyInterface)
{
this.createMyInterface = createMyInterface;
}
public void DoSomething()
{
// Instead of var item = new MyConcreteClass(), we do the following:
var item = createMyInterface();
Console.WriteLine(item.MyMethod(12345));
}
}

The main idea is to defer execution of the method and control when it gets executed.
One use case for this is passing a method that creates and object so that the class controls when the object is created and it can control how many times it is created.

Are you unclear of why a method is being passed as an argument or specificly as an argument in a constructor?
CodeCaster already spoke short about Action delegates and if you're interested in storing methods in delegates like Action, I suggest you read up on Delegates here
I'd like to provide a short example of a class that contains an Action delegate: RelayCommand -> I've illustrated the use of this delegate with the MVVM pattern below:
class ViewModelDefault : INotifyPropertyChanged
{
public string TextProperty { get; set;}
public ICommand ButtonProperty
{
get {
RelayCommand relayCommand = new RelayCommand(ExecuteCommand);
return relayCommand;
}
}
private void ExecuteCommand()
{
HandlerClass handler = new HandlerClass();
handler.SaveTextInTextfile(TextboxProperty);
}
...
}
In the MVVM pattern, your main focus is to seperate data, logic and view, which is why using Delegate is perfect in this scenario.
You'd normally want to bind a property such as a string from the ViewModel to a View UI Element such as a TextBox.
Making use of delegates, allows you to bind a method (which would otherwise not be bindable to a UI element) through an Action property.
In the example above, the method ExecuteCommand is stored in an action field inside the RelayCommand object. This is bindable to a UI element, and will execute the method once it's requested.

Related

Method call that call method from other object

I was wondering if it's possible to otain such behaviour where method call of one object will call method of another object.
public class Example
{
public void DoSomething() { /*BASICALLY NOTHING*/ }
}
public class Engine
{
public void DoSomething() { Console.WriteLine("bleee"); }
static void Main()
{
Example e = new Example();
Engine eng = new Engine();
e.DoSomething = eng.DoSomething;
}
}
My Example object is exactly dummy object, but I would like to use this class as base class and build on top of it something more fancy.
So e.DoSomething() should call method from eng.DoSomething(). I can't use inheritance or pass Engine object to Example as argument.
Is it possible? How to achieve that? Is such approach used somewhere?
You can't do this in the way you describe, but you can do it with delegates.
public class Example
{
public Action DoSomething {get; set;}
}
public class Engine
{
public void DoSomething() { Console.WriteLine("bleee"); }
static void Main()
{
Example e = new Example();
Engine eng = new Engine();
e.DoSomething = eng.DoSomething;
}
}
Now you can say e.DoSomething() and it will call via the delegate by calling the getter and then calling the returned action.
Using reflection we can do the method call with same type info. But other types method is not possible. I think so.

Can I create a delegate in a class that will be passed into the constructor but not make it public?

I have a class like so.
public class Step<T>
{
public delegate void Act<T>();
Act<T> _act;
public Step(Act<T> action)
{
Contract.Requires(action != null);
_act = action;
}
public void PerformStep()
{
_act.Invoke();
}
}
My problem is as follows. I want the constructor of this class to be able to accept a delegate with the definition you see in the class. What I do not want is for that delegate to be a public member of my class. The alternative of this is to have a public delegate outside of the class but no other class but this one will need it. Is there a way I can make the delegate private?
If that solution is not possible is there an alternative? This step class is just a wrapper for delegates that are to be run sequentially. There is no need for a signature if I can have a more generic solution.
EDIT: More info for clarification.
The aim of this class it to be able to allow the user to do something like the following.
If I have a Class Car. This car will have methods like Drive, Stop, Turn etc. Using this Step Class the user of the Class should be able to create an instance of their class then do the following.
Car c = new Car();
Step<Car> step1 = new Step<Car>(c.TurnLeft);
Step<Car> step2 = new Step<Car>(c.Drive);
Step<Car> step3 = new Step<Car>(c.TurnRight);
//add steps to a collection
c.AddNewRoutine(collection of steps);
c.RunRoutine(identify which routine);
The steps will be added to a data structure that will allow them to be executed in order. The car class will contain a data structure that can hold these groups of steps. This then allows any class to be created and have the user set up steps for that class to take, without the class or main program having to hardcode the behaviour.
To finish. I need the step class to be as it is. I just do not want to allow public access to that delegate in any form as the only class who needs it is the Step class.
If other classes need to use the delegate then you need to make it public. Period.
Your only real option is to add a constructor that accepts some other kind of paramter(s) that the constructor can then turn into that delegate type.
Your example uses a custom delegate that takes no parameters and has no return value. For this the System.Action may be ideal. If your custom delegate needs to supply a return value, use System.Func instead.
http://msdn.microsoft.com/en-us/library/system.action.aspx
http://msdn.microsoft.com/en-us/library/bb534960.aspx
From the sample you provided it looks like what you want is something more like this:
public class Step<T>
{
Action _action;
public Step(Action action)
{
Contract.Requires(action != null);
_action = action;
}
public void PerformStep()
{
_action.Invoke();
}
}
Remove the delegate alltogether and use an Action:
private Action<T> _action;
public Step(Action<T> action)
{
_action = action;
}
Edit:
you can also have a simple System.Action with no type parameters
private Action _action;
public Step(Action action)
{
_action = action;
}
However, keep in mind that when doing this:
var c = new Car();
var step1 = new Step(c.TurnLeft);
your're tying the step1 to the actual instance c, you will not be able to reuse that step for another instance:
var c2 = new Car(); //There's no way to call step1 on this =(

Action<T> equivalent for properties

I have a class that instantiates two classes which implement interfaces. I want one class to notify another class that something is OK. I could do it with an Action and then use private variables in the class but wondered if there was a direct way of doing it with properties so that when a property's value changes it updates a property on another class.
For example:
public class MyClass
{
public ILogger Logger {get;set;}
public ILogic Logic {get;set;}
private Form MyWinform;
public void Setup()
{
MyWinform = new MyWinform();
MyWinform.StartBatch += Logger.CreateFile; //Create file when user presses start
//How can I set a property on ILogic to be AllOk once ILogger says so??
//I could use an Action so that once all is ok I call IDecidedAlOK in ILogger which
//can then assign a private bool variable inside the class
Logic.ItsOKMethodSoSetVariableToTrue = Logger.IDecidedAllOKMethod;
}
public void DataIn(string Value)
{
Logic.DataIn(Value);
}
public void CreateInstances()
{
Logger = new FileLogger();
Logic = new MyLogic();
}
}
public class MyLogic : ILogic
{
public void DataIn(string Value)
{
//I want to check that all is ok before I do anything
//if (!AllOK)
//return;
//Do stuff
}
}
Implement INotifyPropertyChanged interface and subscribe to PropertyChanged event
I feel like it might be a bit more conventional to have your ILogger interface expose something like a "FileCreated" or "Ready" event, and allow your application to handle that event in order to update the ILogic object (or do whatever else is necessary).
EDIT: my apologies, after re-reading the question, I think I misunderstood what you were asking for.
There isn't any "natural" object that does exactly what you're asking, but you could create an anonymous delegate (or lambda expression) for this purpose:
Action<bool> loggerResult = (value) => Logic.ItsOKMethodSoSetVariableToTrue = value;
A property internally consists of two private methods, a get_XXX and a set_XXX, so unless you want to fetch the MethodInfo of those methods and invoke them (which are again methods) you have no choice but to implement a method calling approach.
Subscribing to event (INotifyPropertyChanged or some custom one) is OK, so is the method to pass a lambda-setter, but in some cases it might be more convinient to use a shared context object (much like the shared memory concept):
class ConversationContext
{
public bool EverythingIsOK { get; set;}
}
This object is passed to all interested objects (ILogic and ILogger) and they operate directly on it, instead of some internal properties. If change notifications are required, Implement INotifyPropertyChanged on it.
One positive aspect of this approach is that you won't get tangled in repeatedly firing events triggering other events and so on. A single object will hold the current state and no recurrent updates are needed.
Again, this is just one of many options.

How to define a new class that inherits from Delegate

is there anyway to do something similar to what ive got bellow.
What im trying to do is to invoke a list of delegates at a specific point in time and keep track of them, and for the sake of keeping code clean, keep the delegates to be invoked in a list of some sort.
public interface IServiceStatusDelegate
{
object DynamicInvoke(object[] args)
}
public class ServiceStatusDelegate
: Delegate, IServiceStatusDelegate
{
}
public class MyServiceStatusCheckedDelegate
: ServiceStatusDelgate
{
}
public class MyServiceStatusChangedDelegate
: ServiceStatusDelgate
{
}
public class MyClass
{
public ServiceStatusDelgate[] listOfDelegatesToInvoke;
public void InvokeRequiredDelegates()
{
foreach(ServiceStatusDelegate delegateToInvoke in this.listOfDelegatesToInvoke)
delegateToInvoke.DynamicInvoke(new object[]{this, DateTime.Now});
}
}
You don't need a list of delegates... any delegate you create in c# is going to be multicast, so all you need is any delegate, and you can combine them with +. Just invoke it and all targets will be reached. For example:
Action target = null;
...
target += Method1;
...
target += Method2;
...
if(target != null) target(); // calls Method1 and Method2
This could (although it isn't necessary for it to stand) be implemented via an event which will make the convention very obvious the caller.

Add an event to a class method?

OK, I've tried but I just don't get it.
I have two classes logger and class1.
I have a method called logger.Write(string) and method called class1.Execute().
Now in my application I want to have logger.Write(class1.ToString()) run when class1.Execute() is called.
I presume you need to add delegates and events, but I just can't get my head around this, been scratching it for a couple hours.
One thing, is that the logger and the class are in different namespaces and I don't want to update the class code for either of them if possible.
Well you certainly can't do it without changing code in either class (assuming you also don't want to change everywhere that calls class1.Execute) - at least not without some deep code-weaving/instrumentation magic. However, you can fairly easily add an event in Class1:
public class Class1
{
// TODO: Think of a better name :)
public event EventHandler ExecuteCalled = delegate {};
public void Execute()
{
ExecuteCalled(this, EventArgs.Empty);
// Do your normal stuff
}
}
The delegate{} bit is just to make sure that there's always at least a no-op event handler registered - it means you don't need to check for nullity.
You'd then hook it up by writing:
Class1 class1 = new Class1();
Logger logger = new Logger();
class1.ExecuteCalled += (sender, args) => logger.Write(sender.ToString());
(This is assuming you're using C# 3 so you have lambda expressions available to you - let me know if that's not the case.)
If Class1 implements an interface (say IFoo), you might want to write an implementation of the interface which wraps another implementation, and just logs before each call:
public sealed class LoggingFoo : IFoo
{
private readonly IFoo original;
private readonly IFoo logger;
public LoggingFoo(IFoo original, Logger logger)
{
// TODO: Check arguments for nullity
this.original = original;
this.logger = logger;
}
// Implement IFoo
public void Execute()
{
logger.Write("Calling Execute on {0}", original);
original.Execute();
}
}
Then just use that wrapper around a "real" implementation wherever you currently just use the implementation.
Can you pass an object parameter for logger and then just call the ToString on that? The proper ToString method will be called. If you don't want to change anything in logger or class1, then you could write an extension method and call that instead of calling class1.Execute. This method would make the call to logger and then the call to class1.Execute.
public static ExecuteAndLog(this class1 obj)
{
logger.Write(obj.ToString());
obj.Execute();
}
And then you'd simply call obj.ExecuteAndLog();
You'll need to declare an EventHandler for Class1
public event EventHandler OnExecute;
and in your execute method:
public void Execute()
{
//...
if (OnExecute != null)
OnExecute(this, null);
}
And then when you use class1 elsewhere, that's where you put your event;
private Class1 class1 = new Class1();
class1.OnExecute += SomeMethodName;
public void SomeMethodName(sender obj, EventArgs e)
{
logger.Write(class1.ToString());
}
We can make custom EventHandlers for if you want more information there, but for barebones parameterless events this should work.

Categories

Resources