Welcome back.
In this video we're going to see how we can invoke a multicast delegate safely.
Therefore we're going to create a method called Invoke Safely.
So static void invoke safely.
And what I'm going to say is I will get the log handler as well as a message that I want to display.
So string message.
All right.
And this method here, what it should do is it will create a temporary log handler.
So temp log handler which will be the log handler that is passed to this method.
So this one right here.
Okay.
And then I'm going to say if temp log handler is not zero then use the temp log handler to send the
message or to display the message.
So what it will just do is it will just call the log handler.
Okay.
So now what we can do is instead of calling the log handler here, we're going to say invoke safely.
And then we're passing the log handler as well as the message.
And our message is going to be after revoking log to file for example okay.
So this is just a um well, a, uh, safer way of doing it.
So let's comment this out.
And now we are invoking safely and let's run it and we see console log still works fine.
Okay.
But that doesn't solve our problem or our error that we have here.
Or another error.
But our warning.
So how do I know which items are inside of a multicast delegate.
Which methods are in there?
Well, there is this method called invocation list.
So if we wanted to make sure that the invocation is run even more safely, we can do it using a try
and catch.
So after we created our logger let's do it here.
This is where we are invoking the multicast delegate.
So instead of doing it unsafely, let's do it safely.
And in order to do that we can use for each here and we can say log handler handler in.
And here log handler dot get invocation list okay.
So this get invocation list will give us the items inside of the delegate.
So it will return a delegate array.
You see that here.
Delegate square brackets.
So I can say try handler and then whatever we want to say.
So event occurred with error handling.
And in case we do get an error that's where we're going to catch the exception.
We're going to say exception caught with whatever the message is.
So plus X message okay.
So that's how we can make sure that the items inside of the log handler are only going to be invoked
once, that there is actually something to invoke, which is basically this line here.
So instead of using this line, we could have used this for each loop where we get where we invoke with
every single item that is the text that we want to pass.
Okay.
So with this for each we go through every single logged item.
So in this instance we have two items in the invocation list okay.
We have this log two file as well as log two console.
So now what we could do is we could say only if log to file is in the log handler.
Let's delete it.
So in order to make that sure we can do the following.
We can create another method static bool which I'm going to call is method in delegate.
And here we can pass the log handler as well as the log handler method.
So log handler method.
Okay so this looks a little weird.
But basically what's happening is we're comparing the log handler itself with whatever method is of
type log handler Okay.
Because log handler if we look at it is our delegate.
So it is interestingly enough we're comparing the method that is of type of that delegate as well as
the delegate itself, which can have multiple methods assigned to it.
So that's what this method will do is it will check if first of all is the log handler null?
If that's the case, let's return false and in the else block.
Basically, we don't need an else block because otherwise like if this runs then we get out of this
method anyways because we already returned something.
But otherwise we're going to say for each var d in log handler, get invocation list.
If d is equal equal to method, then return true and otherwise if that didn't happen.
So if through the entire list it didn't happen, then return false.
So now here we can cast that into a delegate if we really wanted to make sure like so delegate method.
And then we have it in a really clear way because method is of type delegate.
But we're really making sure that we cast it into the right format.
So we're comparing D which is a delegate because this get invocation list is returning a delegate array.
So every single item in the delegate array is going to be a delegate.
So this could be delegate.
We could call it delegate.
But it's not a good name because delegate is already an internal name inside of C sharp.
So we cannot use that.
But we can call it D.
And then we're comparing the method which we then cast into a delegate and we compare those two.
And if they are the same, then we know that our delegate that we wanted to erase is inside of the multicast
delegate.
So then what we can say is before we delete this, we can check if logger dot log to file.
And let me actually put that.
So if is how we we call it is method in delegate we pass the logger first.
So our how do we call it log handler.
So we pass our log handler and we pass the method which is logger dot two file.
So if that's the case if this is true then we can say log handler get rid of log to file.
So what we can say here to make it a little clearer we can say log to file method removed.
And in the else block we can say log to file method not found.
So log to file method not found.
Okay.
So let's say we never actually added the log to file method in this case.
So let's run it and we should see it says log two file not found.
So log to file method not found.
And now let's run it again.
But this time it is in there.
And we see log two file method removed.
And then it says console after removing log two file okay.
So this was a little something.
You see the warnings still don't disappear.
But we actually made sure that all is good.
So now you could really make sure if log handler not equal null only then run this invoke safely.
Even though our invoke safely is already doing that okay, okay, I'd say this was a little bit of a
side note that you've seen how you can manage delegates and which methods are assigned to a multicast
delegate.
So that's it for this video.
See you in the next one.