示例#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);
        }
示例#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);
    }
示例#3
0
        public void DiscriminatedUnion_AllowNulls()
        {
            var du = DiscriminatedUnion <object, object> .CreateB(null);

            Assert.Equal(DiscriminatedUnionOption.B, du.Option);
            Assert.Equal(null, du.B);
        }
示例#4
0
        public void DiscriminatedUnion_CreateB()
        {
            var du = DiscriminatedUnion <string, string> .CreateB("foo");

            Assert.Equal(DiscriminatedUnionOption.B, du.Option);
            Assert.Equal("foo", du.B);
        }
示例#5
0
        public void AccessInvalidOption_A()
        {
            var du = DiscriminatedUnion <string, string> .CreateB("foo");

            Assert.Throws <InvalidOperationException>(() => du.A.ToString());
        }
        public void AccessInvalidOption_A()
        {
            var du = DiscriminatedUnion <string, string> .CreateB("foo");

            du.A.ToString();
        }