mirror of
				https://github.com/esiur/esiur-dotnet.git
				synced 2025-10-30 23:51:34 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			583 lines
		
	
	
		
			22 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			583 lines
		
	
	
		
			22 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| /*
 | |
|  
 | |
| Copyright (c) 2017 Ahmed Kh. Zamil
 | |
| 
 | |
| Permission is hereby granted, free of charge, to any person obtaining a copy
 | |
| of this software and associated documentation files (the "Software"), to deal
 | |
| in the Software without restriction, including without limitation the rights
 | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | |
| copies of the Software, and to permit persons to whom the Software is
 | |
| furnished to do so, subject to the following conditions:
 | |
| 
 | |
| The above copyright notice and this permission notice shall be included in all
 | |
| copies or substantial portions of the Software.
 | |
| 
 | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | |
| SOFTWARE.
 | |
| 
 | |
| */
 | |
| 
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using Esiur.Core;
 | |
| using Esiur.Net.IIP;
 | |
| using Esiur.Resource;
 | |
| using System.Linq;
 | |
| using System.Reflection;
 | |
| using System.Runtime.CompilerServices;
 | |
| using System.Collections;
 | |
| 
 | |
| namespace Esiur.Data;
 | |
| 
 | |
| #nullable enable
 | |
| 
 | |
| public static class Codec
 | |
| {
 | |
| 
 | |
|     //delegate AsyncReply AsyncParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence);
 | |
| 
 | |
|     delegate object AsyncParser(ParsedTDU tdu, DistributedConnection connection, uint[] requestSequence);
 | |
|     delegate object SyncParser(ParsedTDU tdu, Warehouse warehouse);
 | |
| 
 | |
|     static AsyncParser[][] FixedAsyncParsers = new AsyncParser[][]
 | |
|     {
 | |
|         new AsyncParser[]{
 | |
|             DataDeserializer.NullParserAsync,
 | |
|             DataDeserializer.BooleanFalseParserAsync,
 | |
|             DataDeserializer.BooleanTrueParserAsync,
 | |
|             DataDeserializer.NotModifiedParserAsync,
 | |
|         },
 | |
|         new AsyncParser[]{
 | |
|             DataDeserializer.UInt8ParserAsync,
 | |
|             DataDeserializer.Int8ParserAsync,
 | |
|             DataDeserializer.Char8ParserAsync,
 | |
|             DataDeserializer.LocalResourceParser8Async,
 | |
|             DataDeserializer.ResourceParser8Async,
 | |
|         },
 | |
|         new AsyncParser[]{
 | |
|             DataDeserializer.UInt16ParserAsync,
 | |
|             DataDeserializer.Int16ParserAsync,
 | |
|             DataDeserializer.Char16ParserAsync,
 | |
|             DataDeserializer.LocalResourceParser16Async,
 | |
|             DataDeserializer.ResourceParser16Async,
 | |
|         },
 | |
|         new AsyncParser[]{
 | |
|             DataDeserializer.UInt32ParserAsync,
 | |
|             DataDeserializer.Int32ParserAsync,
 | |
|             DataDeserializer.Float32ParserAsync,
 | |
|             DataDeserializer.LocalResourceParser32Async,
 | |
|             DataDeserializer.ResourceParser32Async,
 | |
|         },
 | |
|         new AsyncParser[]{
 | |
|             DataDeserializer.UInt64ParserAsync,
 | |
|             DataDeserializer.Int64ParserAsync,
 | |
|             DataDeserializer.Float64ParserAsync,
 | |
|             DataDeserializer.DateTimeParserAsync,
 | |
|         },
 | |
|         new AsyncParser[]
 | |
|         {
 | |
|             DataDeserializer.UInt128ParserAsync, // uint 128
 | |
|             DataDeserializer.Int128ParserAsync, // int 128
 | |
|             DataDeserializer.Decimal128ParserAsync,
 | |
|             DataDeserializer.UUIDParserAsync
 | |
|         }
 | |
|     };
 | |
| 
 | |
|     static AsyncParser[] DynamicAsyncParsers = new AsyncParser[]
 | |
|     {
 | |
|         DataDeserializer.RawDataParserAsync,
 | |
|         DataDeserializer.StringParserAsync,
 | |
|         DataDeserializer.ListParserAsync,
 | |
|         DataDeserializer.ResourceListParserAsync,
 | |
|         DataDeserializer.RecordListParserAsync,
 | |
|     };
 | |
| 
 | |
|     static AsyncParser[] TypedAsyncParsers = new AsyncParser[]
 | |
|     {
 | |
|         DataDeserializer.RecordParserAsync,
 | |
|         DataDeserializer.TypedListParserAsync,
 | |
|         DataDeserializer.TypedMapParserAsync,
 | |
|         DataDeserializer.TupleParserAsync,
 | |
|         DataDeserializer.EnumParserAsync,
 | |
|         DataDeserializer.ConstantParserAsync,
 | |
|     };
 | |
| 
 | |
|     static AsyncParser[] ExtendedAsyncParsers = new AsyncParser[]
 | |
|     {
 | |
| 
 | |
|     };
 | |
| 
 | |
|     static SyncParser[][] FixedParsers = new SyncParser[][]
 | |
| {
 | |
|         new SyncParser[]{
 | |
|             DataDeserializer.NullParser,
 | |
|             DataDeserializer.BooleanFalseParser,
 | |
|             DataDeserializer.BooleanTrueParser,
 | |
|             DataDeserializer.NotModifiedParser,
 | |
|         },
 | |
|         new SyncParser[]{
 | |
|             DataDeserializer.UInt8Parser,
 | |
|             DataDeserializer.Int8Parser,
 | |
|             DataDeserializer.Char8Parser,
 | |
|             DataDeserializer.LocalResourceParser8,
 | |
|             DataDeserializer.ResourceParser8,
 | |
|         },
 | |
|         new SyncParser[]{
 | |
|             DataDeserializer.UInt16Parser,
 | |
|             DataDeserializer.Int16Parser,
 | |
|             DataDeserializer.Char16Parser,
 | |
|             DataDeserializer.LocalResourceParser16,
 | |
|             DataDeserializer.ResourceParser16,
 | |
|         },
 | |
|         new SyncParser[]{
 | |
|             DataDeserializer.UInt32Parser,
 | |
|             DataDeserializer.Int32Parser,
 | |
|             DataDeserializer.Float32Parser,
 | |
|             DataDeserializer.LocalResourceParser32,
 | |
|             DataDeserializer.ResourceParser32,
 | |
|         },
 | |
|         new SyncParser[]{
 | |
|             DataDeserializer.UInt64Parser,
 | |
|             DataDeserializer.Int64Parser,
 | |
|             DataDeserializer.Float64Parser,
 | |
|             DataDeserializer.DateTimeParser,
 | |
|         },
 | |
|         new SyncParser[]
 | |
|         {
 | |
|             DataDeserializer.UInt128Parser, // uint 128
 | |
|             DataDeserializer.Int128Parser, // int 128
 | |
|             DataDeserializer.Decimal128Parser,
 | |
|             DataDeserializer.UUIDParser
 | |
|         }
 | |
| };
 | |
| 
 | |
|     static SyncParser[] DynamicParsers = new SyncParser[]
 | |
|     {
 | |
|         DataDeserializer.RawDataParser,
 | |
|         DataDeserializer.StringParser,
 | |
|         DataDeserializer.ListParser,
 | |
|         DataDeserializer.ResourceListParser,
 | |
|         DataDeserializer.RecordListParser,
 | |
|     };
 | |
| 
 | |
|     static SyncParser[] TypedParsers = new SyncParser[]
 | |
|     {
 | |
|         DataDeserializer.RecordParser,
 | |
|         DataDeserializer.TypedListParser,
 | |
|         DataDeserializer.TypedMapParser,
 | |
|         DataDeserializer.TupleParser,
 | |
|         DataDeserializer.EnumParser,
 | |
|         DataDeserializer.ConstantParser,
 | |
|     };
 | |
| 
 | |
|     static SyncParser[] ExtendedParsers = new SyncParser[]
 | |
|     {
 | |
| 
 | |
|     };
 | |
| 
 | |
|     /// <summary>
 | |
|     /// Parse a value
 | |
|     /// </summary>
 | |
|     /// <param name="data">Bytes array</param>
 | |
|     /// <param name="offset">Zero-indexed offset.</param>
 | |
|     /// <param name="size">Output the number of bytes parsed</param>
 | |
|     /// <param name="connection">DistributedConnection is required in case a structure in the array holds items at the other end.</param>
 | |
|     /// <param name="dataType">DataType, in case the data is not prepended with DataType</param>
 | |
|     /// <returns>Value</returns>
 | |
|     public static (uint, object) ParseAsync(byte[] data, uint offset, DistributedConnection connection, uint[] requestSequence)
 | |
|     {
 | |
| 
 | |
|         var tdu = ParsedTDU.Parse(data, offset, (uint)data.Length);
 | |
| 
 | |
|         if (tdu.Class == TDUClass.Invalid)
 | |
|             throw new NullReferenceException("DataType can't be parsed.");
 | |
| 
 | |
|         if (tdu.Class == TDUClass.Fixed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, FixedAsyncParsers[tdu.Exponent][tdu.Index](tdu, connection, requestSequence));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Dynamic)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, DynamicAsyncParsers[tdu.Index](tdu, connection, requestSequence));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Typed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, TypedAsyncParsers[tdu.Index](tdu, connection, requestSequence));
 | |
|         }
 | |
|         else // if (tt.Class == TDUClass.Extension)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, ExtendedAsyncParsers[tdu.Index](tdu, connection, requestSequence));
 | |
| 
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public static (uint, object) ParseAsync(ParsedTDU tdu, DistributedConnection connection, uint[] requestSequence)
 | |
|     {
 | |
|         if (tdu.Class == TDUClass.Invalid)
 | |
|             throw new NullReferenceException("DataType can't be parsed.");
 | |
| 
 | |
|         if (tdu.Class == TDUClass.Fixed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, FixedAsyncParsers[tdu.Exponent][tdu.Index](tdu, connection, requestSequence));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Dynamic)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, DynamicAsyncParsers[tdu.Index](tdu, connection, requestSequence));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Typed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, TypedAsyncParsers[tdu.Index](tdu, connection, requestSequence));
 | |
|         }
 | |
|         else // if (tt.Class == TDUClass.Extension)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, ExtendedAsyncParsers[tdu.Index](tdu, connection, requestSequence));
 | |
| 
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public static (uint, object) ParseSync(ParsedTDU tdu, Warehouse warehouse)
 | |
|     {
 | |
|         if (tdu.Class == TDUClass.Fixed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, FixedParsers[tdu.Exponent][tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Dynamic)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, DynamicParsers[tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Typed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, TypedParsers[tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|         else // Extension
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, ExtendedParsers[tdu.Index](tdu, warehouse));
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
|     public static (uint, object) ParseSync(byte[] data, uint offset, Warehouse warehouse)
 | |
|     {
 | |
|         var tdu = ParsedTDU.Parse(data, offset, (uint)data.Length);
 | |
| 
 | |
|         if (tdu.Class == TDUClass.Invalid)
 | |
|             throw new NullReferenceException("DataType can't be parsed.");
 | |
| 
 | |
| 
 | |
|         if (tdu.Class == TDUClass.Fixed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, FixedParsers[tdu.Exponent][tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Dynamic)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, DynamicParsers[tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|         else if (tdu.Class == TDUClass.Typed)
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, TypedParsers[tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             return ((uint)tdu.TotalLength, ExtendedParsers[tdu.Index](tdu, warehouse));
 | |
|         }
 | |
|     }
 | |
|     /// <summary>
 | |
|     /// Check if a resource is local to a given connection.
 | |
|     /// </summary>
 | |
|     /// <param name="resource">Resource to check.</param>
 | |
|     /// <param name="connection">DistributedConnection to check if the resource is local to it.</param>
 | |
|     /// <returns>True, if the resource owner is the given connection, otherwise False.</returns>
 | |
|     public static bool IsLocalResource(IResource resource, DistributedConnection connection)
 | |
|     {
 | |
|         if (resource == null)
 | |
|             throw new NullReferenceException("Resource is null.");
 | |
| 
 | |
|         if (resource is DistributedResource)
 | |
|             if (((DistributedResource)(resource)).DistributedResourceConnection == connection)
 | |
|                 return true;
 | |
| 
 | |
|         return false;
 | |
|     }
 | |
| 
 | |
|     public delegate TDU Composer(object value, Warehouse warehouse, DistributedConnection connection);
 | |
| 
 | |
|     public static Dictionary<Type, Composer> Composers = new Dictionary<Type, Composer>()
 | |
|     {
 | |
|         // Fixed
 | |
|         [typeof(bool)] = DataSerializer.BoolComposer,
 | |
|         [typeof(bool?)] = DataSerializer.BoolComposer,
 | |
|         [typeof(NotModified)] = DataSerializer.NotModifiedComposer,
 | |
|         [typeof(byte)] = DataSerializer.UInt8Composer,
 | |
|         [typeof(byte?)] = DataSerializer.UInt8Composer,
 | |
|         [typeof(sbyte)] = DataSerializer.Int8Composer,
 | |
|         [typeof(sbyte?)] = DataSerializer.Int8Composer,
 | |
|         [typeof(char)] = DataSerializer.Char16Composer,
 | |
|         [typeof(char?)] = DataSerializer.Char16Composer,
 | |
|         [typeof(short)] = DataSerializer.Int16Composer,
 | |
|         [typeof(short?)] = DataSerializer.Int16Composer,
 | |
|         [typeof(ushort)] = DataSerializer.UInt16Composer,
 | |
|         [typeof(ushort?)] = DataSerializer.UInt16Composer,
 | |
|         [typeof(int)] = DataSerializer.Int32Composer,
 | |
|         [typeof(int?)] = DataSerializer.Int32Composer,
 | |
|         [typeof(uint)] = DataSerializer.UInt32Composer,
 | |
|         [typeof(uint?)] = DataSerializer.UInt32Composer,
 | |
|         [typeof(float)] = DataSerializer.Float32Composer,
 | |
|         [typeof(float?)] = DataSerializer.Float32Composer,
 | |
|         [typeof(long)] = DataSerializer.Int64Composer,
 | |
|         [typeof(long?)] = DataSerializer.Int64Composer,
 | |
|         [typeof(ulong)] = DataSerializer.UInt64Composer,
 | |
|         [typeof(ulong?)] = DataSerializer.UInt64Composer,
 | |
|         [typeof(double)] = DataSerializer.Float64Composer,
 | |
|         [typeof(double?)] = DataSerializer.Float64Composer,
 | |
|         [typeof(DateTime)] = DataSerializer.DateTimeComposer,
 | |
|         [typeof(DateTime?)] = DataSerializer.DateTimeComposer,
 | |
|         [typeof(decimal)] = DataSerializer.Decimal128Composer,
 | |
|         [typeof(decimal?)] = DataSerializer.Decimal128Composer,
 | |
|         [typeof(byte[])] = DataSerializer.RawDataComposerFromArray,
 | |
|         //[typeof(byte?[])] = DataSerializer.RawDataComposerFromArray,
 | |
|         [typeof(List<byte>)] = DataSerializer.RawDataComposerFromList,
 | |
|         //[typeof(List<byte?>)] = DataSerializer.RawDataComposerFromList,
 | |
|         [typeof(string)] = DataSerializer.StringComposer,
 | |
|         [typeof(UUID)] = DataSerializer.UUIDComposer,
 | |
|         // Special
 | |
|         [typeof(object[])] = DataSerializer.ListComposer,
 | |
|         [typeof(List<object>)] = DataSerializer.ListComposer,
 | |
|         [typeof(VarList<object>)] = DataSerializer.ListComposer,
 | |
|         [typeof(IResource[])] = DataSerializer.ResourceListComposer,
 | |
|         [typeof(IResource?[])] = DataSerializer.ResourceListComposer,
 | |
|         [typeof(List<IResource>)] = DataSerializer.ResourceListComposer,
 | |
|         [typeof(List<IResource?>)] = DataSerializer.ResourceListComposer,
 | |
|         [typeof(VarList<IResource>)] = DataSerializer.ResourceListComposer,
 | |
|         [typeof(VarList<IResource?>)] = DataSerializer.ResourceListComposer,
 | |
|         [typeof(IRecord[])] = DataSerializer.RecordListComposer,
 | |
|         [typeof(IRecord?[])] = DataSerializer.RecordListComposer,
 | |
|         [typeof(List<IRecord>)] = DataSerializer.RecordListComposer,
 | |
|         [typeof(List<IRecord?>)] = DataSerializer.RecordListComposer,
 | |
|         [typeof(VarList<IRecord>)] = DataSerializer.RecordListComposer,
 | |
|         [typeof(VarList<IRecord?>)] = DataSerializer.RecordListComposer,
 | |
|         [typeof(Map<object, object>)] = DataSerializer.MapComposer,
 | |
|         [typeof(Map<object?, object>)] = DataSerializer.MapComposer,
 | |
|         [typeof(Map<object, object?>)] = DataSerializer.MapComposer,
 | |
|         [typeof(Map<object?, object?>)] = DataSerializer.MapComposer,
 | |
|         [typeof(PropertyValue[])] = DataSerializer.PropertyValueArrayComposer
 | |
|         // Typed
 | |
|         // [typeof(bool[])] = (value, con) => DataSerializer.TypedListComposer((IEnumerable)value, typeof(bool), con),
 | |
|         // [typeof(bool?[])] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
 | |
|         // [typeof(List<bool>)] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
 | |
|         // [typeof(List<bool?>)] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
 | |
| 
 | |
|         // [typeof(byte?[])] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
 | |
|         // [typeof(List<bool?>)] = (value, con) => (TransmissionDataUnitIdentifier.TypedList, new byte[] { (byte)value }),
 | |
| 
 | |
|     };
 | |
| 
 | |
| 
 | |
|     internal static TDU
 | |
|         ComposeInternal(object valueOrSource, Warehouse warehouse, DistributedConnection connection)
 | |
|     {
 | |
|         if (valueOrSource == null)
 | |
|             return new TDU(TDUIdentifier.Null, null, 0);
 | |
| 
 | |
|         var type = valueOrSource.GetType();
 | |
| 
 | |
|         if (type.IsGenericType)
 | |
|         {
 | |
| 
 | |
|             var genericType = type.GetGenericTypeDefinition();
 | |
|             if (genericType == typeof(PropertyContext<>))
 | |
|             {
 | |
|                 valueOrSource = ((IPropertyContext)valueOrSource).GetValue(connection);
 | |
|             }
 | |
|             else if (genericType == typeof(Func<>))
 | |
|             {
 | |
|                 var args = genericType.GetGenericArguments();
 | |
|                 if (args.Length == 2 && args[0] == typeof(DistributedConnection))
 | |
|                 {
 | |
|                     //Func<DistributedConnection, DistributedConnection> a;
 | |
|                     //a.Invoke()
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         if (valueOrSource is IUserType)
 | |
|             valueOrSource = ((IUserType)valueOrSource).Get();
 | |
| 
 | |
|         if (valueOrSource == null)
 | |
|             return new TDU(TDUIdentifier.Null, null, 0);
 | |
| 
 | |
| 
 | |
|         type = valueOrSource.GetType();
 | |
| 
 | |
| 
 | |
|         if (Composers.ContainsKey(type))
 | |
|         {
 | |
|             return Composers[type](valueOrSource, warehouse, connection);
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             if (Codec.ImplementsInterface(type, typeof(IResource)))
 | |
|             {
 | |
|                 return DataSerializer.ResourceComposer(valueOrSource, warehouse, connection);
 | |
|             }
 | |
|             else if (Codec.ImplementsInterface(type, typeof(IRecord)))
 | |
|             {
 | |
|                 return DataSerializer.RecordComposer(valueOrSource, warehouse, connection);
 | |
|             }
 | |
|             else if (type.IsGenericType)
 | |
|             {
 | |
|                 var genericType = type.GetGenericTypeDefinition();
 | |
|                 if (genericType == typeof(List<>)
 | |
|                     || genericType == typeof(VarList<>)
 | |
|                     || genericType == typeof(IList<>))
 | |
|                 {
 | |
|                     var args = type.GetGenericArguments();
 | |
|                     //if (Composers.ContainsKey(args[0]))
 | |
|                     //{
 | |
|                     return DataSerializer.TypedListComposer((IEnumerable)valueOrSource, args[0], warehouse, connection);
 | |
|                     //}
 | |
|                 }
 | |
|                 else if (genericType == typeof(Map<,>))
 | |
|                 {
 | |
|                     var args = type.GetGenericArguments();
 | |
| 
 | |
|                     return DataSerializer.TypedMapComposer(valueOrSource, args[0], args[1], warehouse, connection);
 | |
| 
 | |
|                 }
 | |
|                 else if (genericType == typeof(Dictionary<,>))
 | |
|                 {
 | |
|                     var args = type.GetGenericArguments();
 | |
| 
 | |
|                     return DataSerializer.TypedDictionaryComposer(valueOrSource, args[0], args[1], warehouse, connection);
 | |
| 
 | |
|                 }
 | |
| 
 | |
|                 else if (genericType == typeof(ValueTuple<,>)
 | |
|                       || genericType == typeof(ValueTuple<,,>)
 | |
|                       || genericType == typeof(ValueTuple<,,,>)
 | |
|                       || genericType == typeof(ValueTuple<,,,,>)
 | |
|                       || genericType == typeof(ValueTuple<,,,,,>)
 | |
|                       || genericType == typeof(ValueTuple<,,,,,,>)
 | |
|                   )
 | |
|                 {
 | |
|                     return DataSerializer.TupleComposer(valueOrSource, warehouse, connection);
 | |
|                 }
 | |
|             }
 | |
|             else if (type.IsArray)
 | |
|             {
 | |
|                 var elementType = type.GetElementType();
 | |
| 
 | |
|                 //if (Composers.ContainsKey(elementType))
 | |
|                 //{
 | |
|                 return DataSerializer.TypedListComposer((IEnumerable)valueOrSource, elementType, warehouse, connection);
 | |
| 
 | |
|                 //}
 | |
|             }
 | |
|             else if (type.IsEnum)
 | |
|             {
 | |
|                 return DataSerializer.EnumComposer(valueOrSource, warehouse, connection);
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         return new TDU(TDUIdentifier.Null, null, 0);
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
|     /// <summary>
 | |
|     /// Compose a variable
 | |
|     /// </summary>
 | |
|     /// <param name="value">Value to compose.</param>
 | |
|     /// <param name="connection">DistributedConnection is required to check locality.</param>
 | |
|     /// <param name="prependType">If True, prepend the DataType at the beginning of the output.</param>
 | |
|     /// <returns>Array of bytes in the network byte order.</returns>
 | |
|     public static byte[] Compose(object valueOrSource, Warehouse warehouse, DistributedConnection connection)//, bool prependType = true)
 | |
|     {
 | |
|         var tdu = ComposeInternal(valueOrSource, warehouse, connection);
 | |
|         return tdu.Composed;
 | |
|     }
 | |
| 
 | |
|     public static bool IsAnonymous(Type type)
 | |
|     {
 | |
|         // Detect anonymous types
 | |
|         var info = type.GetTypeInfo();
 | |
|         var hasCompilerGeneratedAttribute = info.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Count() > 0;
 | |
|         var nameContainsAnonymousType = type.FullName.Contains("AnonymousType");
 | |
|         return hasCompilerGeneratedAttribute && nameContainsAnonymousType;
 | |
|     }
 | |
| 
 | |
| 
 | |
|     public static Type? GetGenericType(Type type, Type ifaceType, int argument = 0)
 | |
|     {
 | |
|         if (ifaceType.IsAssignableFrom(type))
 | |
|         {
 | |
|             var col = type.GetInterfaces().Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == ifaceType)
 | |
|                                        .FirstOrDefault()?
 | |
|                                        .GetGenericArguments()
 | |
|                                        .FirstOrDefault() ?? null;
 | |
| 
 | |
|             return col;
 | |
|         }
 | |
|         else
 | |
|             return null;
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// Check if a type implements an interface
 | |
|     /// </summary>
 | |
|     /// <param name="type">Sub-class type.</param>
 | |
|     /// <param name="iface">Super-interface type.</param>
 | |
|     /// <returns>True, if <paramref name="type"/> implements <paramref name="iface"/>.</returns>
 | |
|     public static bool ImplementsInterface(Type type, Type iface)
 | |
|     {
 | |
|         while (type != null)
 | |
|         {
 | |
|             if (type == iface)
 | |
|                 return true;
 | |
| 
 | |
| #if NETSTANDARD
 | |
|             if (type.GetTypeInfo().GetInterfaces().Contains(iface))
 | |
|                 return true;
 | |
| 
 | |
|             type = type.GetTypeInfo().BaseType;
 | |
| #else
 | |
|                 if (type.GetInterfaces().Contains(iface))
 | |
|                     return true;
 | |
|                 type = type.BaseType;
 | |
| #endif
 | |
|         }
 | |
| 
 | |
|         return false;
 | |
|     }
 | |
| 
 | |
|     public static bool InheritsClass(Type type, Type parent)
 | |
|         => type.IsSubclassOf(parent);
 | |
| 
 | |
|     /// <summary>
 | |
|     /// Check if a type inherits another type.
 | |
|     /// </summary>
 | |
|     /// <param name="childType">Child type.</param>
 | |
|     /// <param name="parentType">Parent type.</param>
 | |
|     /// <returns>True, if <paramref name="childType"/> inherits <paramref name="parentType"/>.</returns>
 | |
|     private static bool HasParentType(Type childType, Type parentType)
 | |
|     {
 | |
|         while (childType != null)
 | |
|         {
 | |
|             if (childType == parentType)
 | |
|                 return true;
 | |
| #if NETSTANDARD
 | |
|             childType = childType.GetTypeInfo().BaseType;
 | |
| #else
 | |
|                 childType = childType.BaseType;
 | |
| #endif
 | |
|         }
 | |
| 
 | |
|         return false;
 | |
|     }
 | |
| }
 |