From bc74a97b1468ba9b38e859693501f78c29a5c9db Mon Sep 17 00:00:00 2001 From: Mihail Mitrovanov Date: Wed, 7 May 2025 09:55:15 +0300 Subject: [PATCH] Event binding system update --- mROA.Cbor/CborSerializationToolkit.cs | 4 +--- mROA/Abstract/IEventBinder.cs | 14 +++++++++++++- mROA/Implementation/Backend/ContextRepository.cs | 10 ++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/mROA.Cbor/CborSerializationToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs index 672f38d..5fc81a5 100644 --- a/mROA.Cbor/CborSerializationToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -213,9 +213,7 @@ namespace mROA.Cbor if (obj is IShared) { - var interfaces = obj.GetType().GetInterfaces(); - var generic = interfaces.FirstOrDefault(i => typeof(IShared).IsAssignableFrom(i)); - var sharedShell = typeof(SharedObjectShellShell<>).MakeGenericType(generic); + var sharedShell = typeof(SharedObjectShellShell); var so = Activator.CreateInstance(sharedShell, obj, context) as ISharedObjectShell; diff --git a/mROA/Abstract/IEventBinder.cs b/mROA/Abstract/IEventBinder.cs index 737bfd2..51b11a5 100644 --- a/mROA/Abstract/IEventBinder.cs +++ b/mROA/Abstract/IEventBinder.cs @@ -1,8 +1,20 @@ namespace mROA.Abstract { - public interface IEventBinder + public interface IEventBinder : IEventBinder { public void BindEvents(T source, IEndPointContext context, IRepresentationModuleProducer representationModuleProducer, int index); + + void IEventBinder.BindEvents(object source, IEndPointContext context, IRepresentationModuleProducer representationModuleProducer, + int index) + { + BindEvents((T)source, context, representationModuleProducer, index); + } + } + + public interface IEventBinder + { + public void BindEvents(object source, IEndPointContext context, + IRepresentationModuleProducer representationModuleProducer, int index); } } \ No newline at end of file diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index 8b7e738..a18d15c 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -32,9 +32,15 @@ namespace mROA.Implementation.Backend public int ResisterObject(object o, IEndPointContext context) { var last = _storage.Place(o); + + var sharedType = typeof(IShared); + var interfaces = o.GetType().GetInterfaces(); + var generic = interfaces.Where(i => sharedType.IsAssignableFrom(i) && i != sharedType) + .Select(i => typeof(IEventBinder<>).MakeGenericType(i)); - var binder = EventBinders.OfType>().FirstOrDefault(); - binder?.BindEvents((T)o, context, _representationModuleProducer!, last); + var binders = EventBinders.Where(i => generic.Any(g => g.IsAssignableFrom(i.GetType()))); + foreach (var binder in binders) + ((IEventBinder)binder).BindEvents(o, context, _representationModuleProducer!, last); return last; }