C# delegate dictionary add - c#

I want to create a method like this:
private static void AddOrAppend<K>(this Dictionary<K, MulticastDelegate> firstList, K key, MulticastDelegate newFunc)
{
if (!firstList.ContainsKey(key))
{
firstList.Add(key, newFunc);
}
else
{
firstList[key] += newFunc; // this line fails
}
}
But this fails because it says you can't add multicast delegates. Is there something I'm missing? I thought the delegate keyword was just shorthand for a class which inherits from MulticastDelegate.

firstList[key] = (MulticastDelegate)Delegate.Combine(firstList[key],newFunc);
with test:
var data = new Dictionary<int, MulticastDelegate>();
Action action1 = () => Console.WriteLine("abc");
Action action2 = () => Console.WriteLine("def");
data.AddOrAppend(1, action1);
data.AddOrAppend(1, action2);
data[1].DynamicInvoke();
(which works)
But tbh, Just use Delegate in place of MulticastDelegate; this is largely a hangover from something that never really worked. Or better; a specific type of delegate (perhaps Action).

Related

C# generic delegate that accept function without arguments

I've created a generic delegate and I want to assign to it a function without any arguments.
Is it possible?
Here is what I tried so far:
class Program
{
public delegate void TemplateDel<T>(T item);
public static void fWriteLetters(char[] p_cLetters)
{
for (int i = 0; i < p_cLetters.Length; i++)
Console.WriteLine(p_cLetters[i]);
}
void fNoArg()
{
Console.WriteLine("No arguments!");
}
static void Main(string[] args)
{
TemplateDel<char[]> l_dWriteLeters = new TemplateDel<char[]>(fWriteLetters);
TemplateDel<void> l_dNoArg = new TemplateDel<void>(fWriteLetters);
}
}
the last line of code doesn't compile...
No it is not possible.void is only valid in return types.You can't use it as a type in other contexts. (except the unsafe context)
You need add another overload for your delegate.
public delegate void TemplateDel();
Or simply use an Action.
As Selman22 notes in the other answer:
No it is not possible.
But there is another way, use a lambda to throw away the argument:
TemplateDel<bool> l_dNoArg = new TemplateDel<bool>(_ => fWriteLetters);
(Using _ as the identifier here matches F#'s wildcard – ignore this argument – syntax.)
While not helpful here, this kind of wrapper is helpful when arguments are not interested in are passed and saves writing an extra member just to ignore or re-arrange arguments.

Assigning code to a variable

Is it possible to make a variable, and assign a line of code to it, such as:
ButtonClicked = (MessageBox.Show("Hello, World!"));
... so when I use the variable, it will execute the line of code.
You could assign it to an Action like this:
var ButtonClicked = new Action(() => MessageBox.Show("hi"));
Then call it:
ButtonClicked();
For completeness (in regards to the various comments)...
As Erik stated, you could execute multiple lines of code:
var ButtonClicked = new Action(() =>
{
MessageBox.Show("hi");
MessageBox.Show("something else"); // something more useful than another popup ;)
});
As Tim stated, you could omit the Action keyword
Action ButtonClicked = () => MessageBox.Show("hi");
Action ButtonClicked = () =>
{
// multiple lines of code
};
To address KRyan's comment, regarding the empty parentheses, that represents the list of parameters you want to be able to send to the Action (in this case, none).
If, for instance, you wanted to specify the message to show, you could add "message" as a parameter (note that I changed Action to Action<string> in order to specify a single string parameter):
Action<string> ButtonClicked = (message) => MessageBox.Show(message);
ButtonClicked("hello world!");
In your case, you want to use a delegate.
Let's see how a delegate works and how we can get to an easier form by understanding its concept:
// Create a normal function
void OnButtonClick()
{
MessageBox.Show("Hello World!");
}
// Now we create a delegate called ButtonClick
delegate void ButtonClick();
You see, the delegate takes the form of a normal function but without any arguments (It could take any amount of arguments just like any other method, but for the sake of simplicity, it doesn't).
Now, let's use what we have; we will define the delegate just as we define any other variable:
ButtonClick ButtonClicked = new ButtonClick(OnButtonClick);
We basically created a new variable called ButtonClicked, that has a type of ButtonClick (which is a delegate) and that when used, will execute the method in the OnButtonClick() method.
To use it we simply call: ButtonClicked();
So the whole code would be:
delegate void ButtonClick();
void OnButtonClick()
{
MessageBox.Show("Hello World!");
}
void Foo()
{
ButtonClick ButtonClicked = new ButtonClick(OnButtonClick);
ButtonClicked(); // Execute the function.
}
From here, we can move to lambda expressions and see how they could be useful in your situation:
There are many delegates already defined by .NET libraries, with some like Action, which do not accept any parameter and does no return a value. It is defined as public delegate void Action();
You can always use it to your needs instead of the need of defining a new delegate every time. In the previous context for example, you could had just written
Action ButtonClicked = new Action(OnButtonClick);
ButtonClicked();
which would had done the same.
Now that you saw different ways of how to use delegates, let's use our first lambda expression. Lambda expressions are anonymous functions; so, they are normal functions but without a name. They are of those forms:
x => DoSomethingWithX(x);
(x) => DoSomethingWithX(x);
(x,y) => DoSometingWithXY(x,y);
() => Console.WriteLine("I do not have parameters!");
In our case, we do not have any parameters so we will use the last expression. We can use this just as the OnButtonClick function, but we get the advantage of not having a named function. We can instead do something like this:
Action ButtonClicked = new Action( () => MessageBox.Show("Hello World!") );
or even easier,
Action ButtonClicked = () => MessageBox.Show("Hello World!");
then simply call ButtonClicked(); Of course you can also have multi-lines of code, but I do not want to confuse you more. It would look like this though:
Action ButtonClicked = () =>
{
MessageBox.Show("Hello World!");
};
ButtonClicked();
You could also play around, for example, you can execute a function like this:
new Action(() => MessageBox.Show("Hello World!"))();
Sorry for the long post, hope it was not too confusing :)
EDIT: I forgot to mention that an alternate form which, even though not often used, could make lambda expressions easier to understand:
new Action(delegate() {
Console.WriteLine("I am parameterless");
})();
Also, using generics:
// Defines a delegate that has one parameter of type string. You could pass as many parameters as you want.
new Action<string>(delegate(string x) {
Console.WriteLine(x);
})("I am a string parameter!");
In turn you could use lambda expressions, but you do not need (but might in some cases) to define the type of the parameter, for example, the code above could simply be written as:
new Action<string>(x => {
Console.WriteLine(x);
})("I am a string parameter!");
or:
new Action<string>(x => Console.WriteLine(x))("I am a string parameter!");
EDIT2:
Action<string> is a representation of public void delegate Action(string obj);
Action<string,string> is a representation of public void delegate Action(string obj, string obj2);
In general, Action<T> is a representation of public void delegate Action<T>(T obj);
EDIT3:
I know the post has been here for a while, but I think this is really cool to not mention:
You can do this, which is mostly related to your question:
dynamic aFunction = (Func<string, DialogResult>)MessageBox.Show;
aFunction("Hello, world!");
or simply:
Func<string, DialogResult> aFunction = MessageBox.Show;
aFunction("Hello, world!");
The Lazy class is specifically designed to represent a value that won't be computed until you ask for it. You construct it by providing a method that defines how it should be constructed, but it will handle executing that method no more than once (even in the face of multiple threads requesting the value) and simply returning the already constructed value for any additional requests:
var foo = new Lazy<DialogResult>(()=>MessageBox.Show("Hello, World!"));
var result = foo.Value;
The way I'm reading your question, this is in the context of GUI controls?
If this is in WPF, take a look at the "right" way to handle commands from controls:
http://msdn.microsoft.com/en-us/library/ms752308(v=vs.110).aspx
...but that can be a pain and overkill. For a simpler general case, you might be looking for an event handler, like:
myButton.Click += (o, e) => MessageBox.Show("Hello, World!");
That event handler can be handled a variety of ways. The above example uses an anonymous function, but you could also do:
Action<object, RoutedEventArgs> sayHello = (o, e) => MessageBox.Show("Hello, World");
myButton.Click += new RoutedEventHandler(sayHello);
...just like you were asking, with a function (or here, "Action", since it returns void) assigned as a variable.
You can assign C# code to a variable, compiling it at runtime and run the code:
Write your code:
// Assign C# code to the code variable.
string code = #"
using System;
namespace First
{
public class Program
{
public static void Main()
{
" +
"Console.WriteLine(\"Hello, world!\");"
+ #"
}
}
}
";
Create the provider and parameters of the compiler:
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
Define parameters of the compiler:
// Reference to System.Drawing library
parameters.ReferencedAssemblies.Add("System.Drawing.dll");
// True - memory generation, false - external file generation
parameters.GenerateInMemory = true;
// True - exe file generation, false - dll file generation
parameters.GenerateExecutable = true;
Compile assembly:
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
Check errors:
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
Get assembly, type and the Main method:
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("First.Program");
MethodInfo main = program.GetMethod("Main");
Run it:
main.Invoke(null, null);
Reference:
http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime

Explanation of success/failure terms with C# Actions?

I found the method below in a Windows Phone 7 C# sample. In it you can see the terms success and failure used inside the method. I tried Go To Definition with either term and Visual Studio did not jump to a definition for either term. I tried searching Google using the terms "Action", "success", "failure", "C#", and "parameter" and did not find anything useful. Are success and failure in this context macros or something similar? Where can I get an explanation of what they do and how to use them? Note, the tooltip help when hovered over failure shows "parameter Action<string> failure".
public void SendAsync(string userName, string message, Action success, Action<string> failure)
{
if (socket.Connected) {
var formattedMessage = string.Format("{0};{1};{2};{3};{4}",
SocketCommands.TEXT, this.DeviceNameAndId, userName, message, DateTime.Now);
var buffer = Encoding.UTF8.GetBytes(formattedMessage);
var args = new SocketAsyncEventArgs();
args.RemoteEndPoint = this.IPEndPoint;
args.SetBuffer(buffer, 0, buffer.Length);
args.Completed += (__, e) => {
Deployment.Current.Dispatcher.BeginInvoke(() => {
if (e.SocketError != SocketError.Success) {
failure("Your message can't be sent.");
}
else {
success();
}
});
};
socket.SendAsync(args);
}
}
They are delegates that are being used as "callback functions". Basically, they are functions that are provided to another function that can be called inside that function. Perhaps a smaller sample would make more sense:
static void PerformCheck(bool logic, Action ifTrue, Action ifFalse)
{
if (logic)
ifTrue(); // if logic is true, call the ifTrue delegate
else
ifFalse(); // if logic is false, call the ifFalse delegate
}
False is printed in the below example, because 1 == 2 evaluates to false. So, logic is false within the PerformCheck method.. so it calls the ifFalse delegate. As you can see, ifFalse prints to the Console:
PerformCheck(1 == 2,
ifTrue: () => Console.WriteLine("Yep, its true"),
ifFalse: () => Console.WriteLine("Nope. False."));
Whereas this one will print true.. because 1 == 1 evaluates to true. So it calls ifTrue:
PerformCheck(1 == 1,
ifTrue: () => Console.WriteLine("Yep, its true"),
ifFalse: () => Console.WriteLine("Nope. False."));
You can think of Action (and also Func) as variables that hold other methods.
You can pass, assign and basically do anything to an Action that you would any other variable, but you can also call it like a method.
Say you have two methods in your code:
public void Main(){
Action doWork;
doWork = WorkMethod;
doWork();
}
private void WorkMethod(){
//do something here
}
You assign WorkMethod to the action like you would do any assignment to the variable. Then, you can call doWork as though it were a method. It isn't particularly useful in this example, but you can probably see how all the benefits of standard variables apply.
You use an Action and a Func in pretty much the same way. The only real difference is that an Action represents a void and a Func requires a return type.
You can also use generics. For example Action<int> respresents a method with the signature
void methodName(int arg){}
Action<int, string> would be
void methodName(int arg1, string arg2){}
Func is similar, Func<int> would be:
int methodName(){}
Func<string, int> would be:
int methodName(string arg){}
It's important to remember that the last type in the Func definition is the return type, even though it appears first in the actual method signature.

Dispatcher.BeginInvoke: Cannot convert lambda to System.Delegate

I'm trying to call System.Windows.Threading.Dispatcher.BeginInvoke. The signature of the method is this:
BeginInvoke(Delegate method, params object[] args)
I'm trying to pass it a Lambda instead of having to create a Delegate.
_dispatcher.BeginInvoke((sender) => { DoSomething(); }, new object[] { this } );
It's giving me a compiler error saying that I
can't convert the lambda to a System.Delegate.
The signature of the delegate takes an object as a parameter and returns void. My lambda matches this, yet it's not working. What am I missing?
Shorter:
_dispatcher.BeginInvoke((Action)(() => DoSomething()));
Since the method takes a System.Delegate, you need to give it a specific type of delegate, declared as such. This can be done via a cast or a creation of the specified delegate via new DelegateType as follows:
_dispatcher.BeginInvoke(
new Action<MyClass>((sender) => { DoSomething(); }),
new object[] { this }
);
Also, as SLaks points out, Dispatcher.BeginInvoke takes a params array, so you can just write:
_dispatcher.BeginInvoke(
new Action<MyClass>((sender) => { DoSomething(); }),
this
);
Or, if DoSomething is a method on this object itself:
_dispatcher.BeginInvoke(new Action(this.DoSomething));
Using Inline Lambda...
Dispatcher.BeginInvoke((Action)(()=>{
//Write Code Here
}));
If you reference System.Windows.Presentation.dll from your project and add using System.Windows.Threading then you can access an extension method that allows you to use the lambda syntax.
using System.Windows.Threading;
...
Dispatcher.BeginInvoke(() =>
{
});
We create extension methods for this. E.g.
public static void BeginInvoke(this Control control, Action action)
=> control.BeginInvoke(action);
Now we can call it from within a form: this.BeginInvoke(() => { ... }).

How To Verify My Func<T> Is Invoked In Test Method

My generic class takes a Func<T> parameter in constructor, and I want to test one of its methods which basically invoke the constructor parameter.
I use Moq and in my test code is something like this:
[Fact]
public void Construct_EnsureInvokeFunction()
{
_objectToTest=new TypeToTest<T>(It.IsAny<Func<T>>());
_objectToTest.Construct();
//here I want to ensure that the passed parameter is invoked
//however since I can't mock Func<T>, I dont know how to verify
//whether Invoke method of Func<T> is triggered
}
One workaround that I can think is to wrap my Func<T> inside a new interface and create a method to wrap it's Invoke method, and then use Moq to mock the interface. However it doesn't seem effective.
Am I missing something? Any idea will be appreciated.
Thanks,
Anton
You can create a simple fake Func<T> closure with side-effects and verify those side-effects. A good one is incrementing a local variable because it also lets you assert that it was not called more than it should. Something like this:
int counter = 0;
Func<T> f = () => { counter++; return fakeT; }
var objectToTest = new TypeToTest<T>(f);
objectToTest.Construct();
Assert.Equal(1, counter);
You can wrap the Func<T> in an anonymous method that sets a local variable:
bool wasInvoked = false;
Func<T> testFunc = () => { var ret = funcToTest(); wasInvoked = true; return ret; }
// Test something with testFunc instead of funcToTest
...
Assert.IsTrue(wasInvoked);

Categories