Interface enforces event, anything enforce handlers? - c#

Given:
public delegate void MyHandler(object sender, EventArgs<SomeArgs> e)
public interface MyInterface
{
event MyHandler TheEvent;
}
class MyClass:MyInterface
{
public event MyHandler TheEvent;
}
class TheProblemClass:FrameworkElement //, IPossibleInterface?
{
public MyClass Instance1{get;set;}
public MyClass Instance2{get;set;}
//You used an instance of MyClass, now you must handle Instance1.TheEvent
//You used another instance of MyClass, now you must handle must handle Instance2.TheEvent
//...for any instance of MyClass, MUST handle TheEvent
}
Ok, so I intend for other devs to create their own classes using instances of MyClass. Any way to enforce the scope that declares an instance of MyClass to also handle the event?

Related

Declare a static event in an interface and raise in the implementation

public interface IProgress
{
public static event Action<IProgress> EvtSpawned;
}
public class PlayerSingle : MonoBehaviour, IProgress
{
private void Start()
{
IProgress.EvtSpawned?.Invoke(this);
}
}
I basically want to be able to have some classes listen to whenever an instance is created that implements IProgress. I can subscribe to the event above, but it seems there is no way for me to raise it, it just throws an error even when using within a class that implements IPgrogress.
For now, I just use static events in classes that implement it one by one.
Events can be invoked only by the "owners" i.e. IProgress interface in this case. Not sure why do you require such structure (the provided description is not enough for me) but if you really need to you can declare an invoke method on the interface to delegate the event invocation:
public interface IProgress
{
public static event Action<IProgress> EvtSpawned;
public static void Invoke(IProgress inv) => EvtSpawned?.Invoke(inv);
}
public class PlayerSingle : IProgress
{
private void Start()
{
IProgress.Invoke(this);
}
}

C# Call function from another class

I think my question is best descirbed by a code snippet:
class A
{
public void FunctionToBeCalled();
}
class B
{
public void FunctionToBeCalledAfter();
}
Now, after a FunctionToBeCalledAfter() call, FunctionToBeCalled() needs to "know" it must be called. B cannot have an A member, but A can have a B member. Is there any way this can be implemented in C#?
Why i need this:
Class A is Application level on OSI stack. Classes B and C(unmentioned before) are Transport Level. C makes calls to FunctionToBeCalledAfter, and after this FunctionToBeCalled needs to be called. But sincer A is a higher level, B and C cannot depend(have a member A), i don't know how to call FunctionToBeCalled.
I see 2 ways to accomplish this, one easier but (arguably) less elegant, one a little more involved but (arguably) more elegant
The less elegant solution: Singleton
A Singleton pattern enforces that there can only ever be one instance of a class at any given time, this seems to line up with your description of A (which from here on out I'll call Foo, and I'll be calling B Bar). So let's implement it:
public class Foo
{
private static Foo _instance;
public static Foo Instance => _instance ?? (_instance = new Foo());
// Private constructor so no one else can instantiate Foo
private Foo() { }
public void FunctionToBeCalled() { /* your code here */ }
}
public class Bar
{
public void FunctionToBeCalledAfter()
{
// Your existing code here
Foo.Instance.FunctionToBeCalled();
}
}
Now, the problem here is if your requirements ever change and you need multiple Foos, that'll be quite a refactor to implement it. Another (larger) downside is that we explicitly reference (i.e depend on) Foo, which isn't great and a problem if Bar is inside a project/ library that cannot directly reference Foo. Luckily solution 2 fixes those problems:
The more elegant solution: Events
public class Foo
{
// We don't need Foo to be a singleton anymore
public void FunctionToBeCalled() { /* Your code here */ }
}
public class Bar
{
public delegate void FunctionToBeCalledAfterEventHandler();
public event FunctionToBecalledAfterEventHandler FunctionToBeCalledAfterEvent;
public void FunctionToBeCalledAfter()
{
// Your existing code here
OnFunctionToBeCalledAfterEvent(); // Fire the event
}
private void OnFunctionToBeCalledAfterEvent()
{
FunctionToBeCalledEvent?.Invoke();
}
}
Now, everywhere where you're creating an instance of Bar you need to have a reference to Foo and subscribe to the event like so:
// foo = instance of class Foo
var bar = new Bar();
// The compiler is smart enough to find out that 'FunctionToBeCalledAfterEvent'
// has the same signature as 'FunctionToBeCalledAfterEvent' and can call it directly
// If this just so happens to not be case, see second way to subscribe to events
bar.FunctionToBeCalledAfterEvent += foo.FunctionToBeCalled;
// Or
bar.FunctionToBeCalledAfterEvent += () => foo.FunctionToBeCalled();
Events are great
Class B can have an event that other parties can handle. At the end of B.FunctionToBeCalledAfter this event would be invoked. Anyone who registered for this event would then be notified. Usual boilerplate code involves one virtual method that invokes one event. It's the standard way of adding events. If there is no need for additional data in the event then EventArgs is used. If additional data is needed then you could replace EventArgs with EventArgs<YourData>, or as an alternative, introduce a class XxxArgs derived from EventArgs with this additional data.
Class B
{
public event EventHandler FinishedFunctionToBeCalledAfter;
protected virtual void OnFinishedFunctionToBeCalledAfter(EventArgs e)
{
EventHandler handler = FinishedFunctionToBeCalledAfter;
handler?.Invoke(this, e);
}
public void FunctionToBeCalledAfter()
{
...
OnFinishedFunctionToBeCalledAfter(EventArgs.Empty);
}
}
Now when class A gets a hold of an object of class B it would add its event handler to it:
class A
{
public void FunctionToBeCalled();
public void FinishedFunctionToBeCalledAfter(object source, EventArgs e);
public void IntroduceObject(B b)
{
b.FinishedFunctionToBeCalledAfter += FinishedFunctionToBeCalledAfter;
}
}
When this object b of class B should end its life class A must know about it so that it can remove its event handler:
b.FinishedFunctionToBeCalledAfter -= FinishedFunctionToBeCalledAfter;

Opennetcf.IOC event subscription and inheritance

Class1 has event with attribute [EventPublication("event1")].
Class2 and Class3 inherits from Class1.
I want to subscribe Method1 to event in object from Class2 and Method2 to event in object from Class3 using [EventSubscription].
But in the derived classes there is the same EventPublication name of the event. So how to distinguish events in derived classes? Is it possible?
EDIT:
Maybe I misunderstand some obvious things about IoC or I try to complicate simple solution...
I will try to clarify my question. Here is some code:
class BasePresenter
{
[EventPublication("event")]
public event Action action;
public void Run()
{
someAction();
if (action != null)
action();
}
protected virtual void someAction()
{
}
}
class Presenter1 : BasePresenter
{
protected override void someAction()
{
}
}
class Presenter2 : BasePresenter
{
protected override void someAction()
{
}
}
class AnotherClass
{
[EventSubscription("event", ThreadOption.Caller)]
public void action1()
{
System.Windows.Forms.MessageBox.Show("Presenter1 started");
}
[EventSubscription("event", ThreadOption.Caller)]
public void action2()
{
System.Windows.Forms.MessageBox.Show("Presenter2 started");
}
}
There is action1() and action2() methods in Another class. I would like to fire action1() when instance of Presenter1 Run() method is called and fire action2() when instance of Presenter2 Run() method is called. But calling Run() method will fire both methods action1 and action2.
I'm not certain I understand the question. There are two ends to the event aggregation, a Publisher and a Subscriber. They are "connected" by the string event name you use in the attribute and nothing else.
A subscription can be done in the same class as the publication, though it's not clear to me why you'd ever do that, just have the base class call a virtual method that the derived classes implement and you're done.
If you want to use events and you want to know if the event source instance is not the receiver instance, just check the event's source input parameter against this, something along these lines:
[EventSubscription("myevent")]
public void OnEvent(object sender, EventArgs a)
{
if(sender.Equals(this)) return;
// do stuff here - the event came from another class instance
}

Call Event Explicitly

Is that possible somehow to trigger an event which belongs another class in C#, such:
class foo
{
public delegate void myEvntHandler();
public event myEvntHandler onTesting;
.
.
.
}
class Main
{
public static void Main()
{
foo obj = new foo();
...
obj.onTesting.Invoke();
}
}
on this sample I mean: obj.onTesting.Invoke();
No you can't invoke it directly from another class. That's the point of events (Encapsulation).
You need a helper method
class foo
{
public delegate void myEvntHandler();
public event myEvntHandler onTesting;
public void RaiseEvent()
{
if(onTesting !=null)
onTesting ();
}
}
Then call RaiseEvent method instead
class Main
{
public static void Main()
{
foo obj = new foo();
...
obj.RaiseEvent();
}
}
If you need to invoke it the way you did, just remove the event keyword and use a delegate. Which doesn't prevent you form doing so.(I don't recommend it)
No. The whole purpose of events is to wrap a delegate while explicitly prohibiting all access to it other than adding/removing an event handler. The event keyword is there specifically to prevent any class other than the class that declares the event from invoking it.
Short answer: No.
Longer answer: under the hood there no such thing as an "event" unlike delegates which are real objects. event is just a convenience for two methods (to add and remove handlers) and a private hidden field of type myEvntHandler.
Logically it makes no sense to raise an event from outside the class: The whole point of an event is that it is raised by the class when the class detects some trigger.
If you just want it to raise an event in order to test another class that has added a handler then the correct way to go is to:
move the event to an interface
implement the interface in your real class
create a test class that also implements the interface and add your "RaiseEvent" method to that.
Inject the interface into your unit under test

C#: Is there way to ensure an object is only accessible through its events?

Is there a way to create some sort of interface that only allows the object to be accessible through events?
Can't you just define an interface with only events in it?
For instance:
interface IExample
{
event EventHandler Event1;
event EventHandler Event2;
}
class Obj : IExample
{
public event EventHandler Event1;
public event EventHandler Event2;
}
Usage:
IExample obj = new Obj();
obj.Event1 += Event1_Handler;
obj.Event2 += Event2_Handler;
Without further information, the best answer I have is that you would simply need to make sure that all of the members properties, functions, etc) are declared as private, except for the events, which would be public.
Although I have to admit, I'm stumped as to how this would eve be useful, and what would trigger a event if it's only accessible to it's events. It's like saying can you create a phone that you can't call, but can only hear the ring (the IncomingCall event).
A setup like this would expose only events to a client using the assembly:
interface ISomething {
event EventHandler MyEvent;
}
internal class MyClass : ISomething {
...
}
public ClassFactory {
public ISomething GetClass(){ // factory method
return new MyClass();
}
}
Or, if you need to restrict the use of this class in its own library as well you can do this:
public class MyClass : ISomething {
private MyClass(){} // private constructor
public ISomething GetClass(){ // factory method
return new MyClass();
}
}
Something like this may be combined with a singleton object if you just need to get its events as well, which can make sense if you simply want have a generic way to subscribe to that object's status events for example.
Be aware that any object to which a caller has access can have any of it's fields accessed through reflection.
If your question is focused on preventing people from accidentally invoking your object incorrectly, Matt B.'s answer is great.
If your question is focused on making it impossible for someone to maliciously access private fields of your object, that's not possible.

Categories

Resources