protected override bool GetParameter <T>(IChannelHandlerContext context, object message, out T value) { if (typeof(ProudSession).IsAssignableFrom(typeof(T))) { var session = context.Channel.GetAttribute(ChannelAttributes.Session).Get(); value = DynamicCast <T> .From(session); return(true); } if (typeof(ProudServer).IsAssignableFrom(typeof(T))) { var server = context.Channel.GetAttribute(ChannelAttributes.Server).Get(); value = DynamicCast <T> .From(server); return(true); } if (typeof(RecvContext).IsAssignableFrom(typeof(T))) { value = DynamicCast <T> .From(message); return(true); } return(base.GetParameter(context, message, out value)); }
public static T ReadEnum <T>(this BinaryReader @this) where T : struct, IComparable, IConvertible { var type = typeof(T); if (!type.GetTypeInfo().IsEnum) { throw new ArgumentException("T is not an enum"); } var derivedType = Enum.GetUnderlyingType(type); switch (derivedType.GetTypeCode()) { case TypeCode.Byte: return(DynamicCast <T> .From(@this.ReadByte())); case TypeCode.SByte: return(DynamicCast <T> .From(@this.ReadSByte())); case TypeCode.Int16: return(DynamicCast <T> .From(@this.ReadInt16())); case TypeCode.Int32: return(DynamicCast <T> .From(@this.ReadInt32())); case TypeCode.Int64: return(DynamicCast <T> .From(@this.ReadInt64())); case TypeCode.UInt16: return(DynamicCast <T> .From(@this.ReadUInt16())); case TypeCode.UInt32: return(DynamicCast <T> .From(@this.ReadUInt32())); case TypeCode.UInt64: return(DynamicCast <T> .From(@this.ReadUInt64())); default: throw new NotSupportedException("Type is not supported"); } }
public static void WriteEnum <T>(this BinaryWriter @this, T value) where T : struct, IComparable, IConvertible { var type = value.GetType(); if (!type.GetTypeInfo().IsEnum) { throw new ArgumentException("T is not an enum"); } var derivedType = Enum.GetUnderlyingType(type); switch (derivedType.GetTypeCode()) { case TypeCode.Byte: @this.Write(DynamicCast <byte> .From(value)); break; case TypeCode.SByte: @this.Write(DynamicCast <sbyte> .From(value)); break; case TypeCode.Int16: @this.Write(DynamicCast <short> .From(value)); break; case TypeCode.UInt16: @this.Write(DynamicCast <ushort> .From(value)); break; case TypeCode.Int32: @this.Write(DynamicCast <int> .From(value)); break; case TypeCode.UInt32: @this.Write(DynamicCast <uint> .From(value)); break; case TypeCode.Int64: @this.Write(DynamicCast <long> .From(value)); break; case TypeCode.UInt64: @this.Write(DynamicCast <ulong> .From(value)); break; case TypeCode.Single: @this.Write(DynamicCast <float> .From(value)); break; case TypeCode.Double: @this.Write(DynamicCast <double> .From(value)); break; case TypeCode.Decimal: @this.Write(DynamicCast <decimal> .From(value)); break; default: throw new NotSupportedException("Type is not supported"); } }