Search code examples
.netobservablereactive-programming

Observable.FromEvent custom event transform won't compile with error "Method name expected"


I've created the error in a .net core and a full up fw v4.7.2. With whatever version c# and the reactive extensions that go with them.

using System.Reactive.Linq;
namespace ObservableTest
{
    internal class Program
    {
        public delegate void _test(int vs, byte bs);
        
        internal class _TestParamList { public int vs1; public byte bs1; }
        
        static void Main(string[] args)
        {
            var obs_test = Observable.FromEvent<_test, _TestParamList>(
    OnNext => new _test((vs, bs)
        => OnNext(new _TestParamList { vs1 = a, bs1 = b }),
        h => _test += h,
        h => _test -= h));

        }
    }
}

The above code won't compile and gives the error "Method name expected" starting with (vs,bs) and going to the last h in the listing.

I am trying to implement a transform on an event I can't rewrite that has two arrays, an int, and a date time value.

I got the idea from this answer: Convert Custom EventHandler to IObservable


Solution

  • In the link I provided that I used as a model for my solution I missed seeing the last two lines of the FromEvent declaration as follows:

    handler => axCZKEM.OnAttTransactionEx += handler,
    handler => axCZKEM.OnAttTransactionEx -= handler);
    

    the axCZKEM object is the event source.

    In the system I am trying to construct that source already exists but I didn't create that in the example in my OP. To correct that problem I borrowed an example of a manufactured event source from here: How to use Observable.FromEvent instead of FromEventPattern and avoid string literal event names

    The above link is a good source of general information on Events and FromEvent method.

    See class Foo for a pretend event source.

    I rewrote the transform a little to get the code below. I didn't have to put a new on the event source within the FromEvent method and needed to write an "Event Handler" called ShowParamList to prove to myself that I could access the various values supplied by the event.

    using System;
    using System.Reactive.Linq;
    
    namespace ObservableTest
    {
        internal class Program
        {
            public delegate void EventTest(int vs, string bs);
            internal class TestParamList { public int vs1; public string bs1; }
            static void Main(string[] args)
            {
                var EventSource = new TestEventSource();
                var obs_test = Observable.FromEvent<EventTest, TestParamList>(
                    OnNext => (int vs, string bs) => OnNext(new TestParamList { vs1 = vs, bs1 = bs }),
                    h => EventSource.MyEvent += h,
                    h => EventSource.MyEvent -= h);
    
                obs_test.Subscribe(x => ShowParamList(x));
                EventSource.RaiseEvent(1, "first");
                EventSource.RaiseEvent(2, "second");
    
                Console.ReadKey();
            }
            static void ShowParamList(TestParamList t)
            {
                Console.WriteLine("Int: " + t.vs1.ToString() + " String: " + t.bs1.ToString());
            }
            public class TestEventSource
            {
                private EventTest delegateChain;
                public event EventTest MyEvent
                {
                    add
                    {
                        delegateChain += value;
                    }
                    remove
                    {
                        delegateChain -= value;
                    }
                }
                public void RaiseEvent(int x, string y)
                {
                    var temp = delegateChain;
                    if (temp != null)
                    {
                        delegateChain(x, y);
                    }
                }
            }
        }
    }