Exemple #1
0
        internal static DataStoreEventArgs CreateDataStoreEventArgs <T>(ushort startAddress, ModbusDataType modbusDataType, IEnumerable <T> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (!(typeof(T) == typeof(bool) || typeof(T) == typeof(ushort)))
            {
                throw new ArgumentException("Generic type T should be of type bool or ushort");
            }

            var eventArgs = new DataStoreEventArgs(startAddress, modbusDataType);

            if (typeof(T) == typeof(bool))
            {
                eventArgs.Data = DiscriminatedUnion <ReadOnlyCollection <bool>, ReadOnlyCollection <ushort> > .CreateA(data.Cast <bool>().ToReadOnly());
            }
            else
            {
                eventArgs.Data = DiscriminatedUnion <ReadOnlyCollection <bool>, ReadOnlyCollection <ushort> > .CreateB(data.Cast <ushort>().ToReadOnly());
            }

            return(eventArgs);
        }
Exemple #2
0
    internal static DataStoreEventArgs CreateDataStoreEventArgs <T>(ushort startAddress, ModbusDataType modbusDataType, IEnumerable <T> data)
    {
        if (data == null)
        {
            throw new ArgumentNullException(nameof(data));
        }

        DataStoreEventArgs eventArgs;

        if (typeof(T) == typeof(bool))
        {
            ReadOnlyCollection <bool>?a = new(data.Cast <bool>().ToArray());

            eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
            {
                Data = DiscriminatedUnion <ReadOnlyCollection <bool>, ReadOnlyCollection <ushort> > .CreateA(a)
            };
        }
        else if (typeof(T) == typeof(ushort))
        {
            ReadOnlyCollection <ushort>?b = new(data.Cast <ushort>().ToArray());

            eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
            {
                Data = DiscriminatedUnion <ReadOnlyCollection <bool>, ReadOnlyCollection <ushort> > .CreateB(b)
            };
        }
        else
        {
            throw new ArgumentException("Generic type T should be of type bool or ushort");
        }

        return(eventArgs);
    }
Exemple #3
0
        public void DiscriminatedUnion_CreateA()
        {
            var du = DiscriminatedUnion <string, string> .CreateA("foo");

            Assert.Equal(DiscriminatedUnionOption.A, du.Option);
            Assert.Equal("foo", du.A);
        }
Exemple #4
0
        public void DiscriminatedUnion_ToString()
        {
            var du = DiscriminatedUnion <string, string> .CreateA("foo");

            Assert.Equal(du.ToString(), "foo");
        }
Exemple #5
0
        public void AccessInvalidOption_B()
        {
            var du = DiscriminatedUnion <string, string> .CreateA("foo");

            Assert.Throws <InvalidOperationException>(() => du.B.ToString());
        }
        public void AccessInvalidOption_B()
        {
            var du = DiscriminatedUnion <string, string> .CreateA("foo");

            du.B.ToString();
        }