I want to provide objects within a class library the ability to “output” messages without worrying about how it is being output. The class library could be used in a console app, a WinForm or WPF windows application, or a web page.
I’d originally decided that I would use a delegate to handle this. I implemented this using classes but the compiler did not like the delegate when I attempted to put the delegate into an interface that each of these objects inherent from.
I found that I could move the delegate out of the interface and then was able to compile but I'm not sure that this accomplishes what I am trying to do.. It also seems like a kludge so I wanted to ask if any of you have different ideas to accomplish this…
The Interface:
namespace Test
{
using System;
using System.Xml.Linq;
public interface IAction
{
DisplayMessageDelegate DisplayMessage(string message);
void Execute();
XElement Serialize(XName elementName);
}
public delegate void DisplayMessageDelegate(string message);
}
From there, I'm not sure how to implement this behavior: (BTW, I know that this code will not compile...)
public class ActionClass1 : IAction
{
// Other methods not shown...
void Execute()
{
if (this.DisplayMessage != null)
{
this.DisplayMessage(“Hello”);
}
}
}
public class ConsoleClass
{
ActionClass1 class1 = new ActionClass1();
class1.DisplayMessage = { x => Console.WriteLine(x); };
}
public class WinFormClass
{
ActionClass1 class1 = new ActionClass1();
Class1.DisplayMessage = {x => DisplayTextBox.Text = x; };
}