public UInt32EnumFormatter() { Func <UInt32, UInt32> identity = Identity; this.serializeCast = (T x) => { return(CastTo <UInt32> .From <T>(x)); }; this.deserializeCast = (UInt32 x) => { return(CastTo <T> .From <UInt32>(x)); }; }
public Int64EnumFormatter() { Func <Int64, Int64> identity = Identity; this.serializeCast = (T x) => { return(CastTo <Int64> .From <T>(x)); }; this.deserializeCast = (Int64 x) => { return(CastTo <T> .From <Int64>(x)); }; }
public ByteEnumFormatter() { Func <Byte, Byte> identity = Identity; this.serializeCast = (T x) => { return(CastTo <Byte> .From <T>(x)); }; this.deserializeCast = (Byte x) => { return(CastTo <T> .From <Byte>(x)); }; }
public NullableUInt64EnumFormatter() { Func <UInt64, UInt64> identity = Identity; this.serializeCast = (T x) => { return(CastTo <UInt64> .From <T>(x)); }; this.deserializeCast = (UInt64 x) => { return(CastTo <T> .From <UInt64>(x)); }; }
public NullableInt16EnumFormatter() { Func <Int16, Int16> identity = Identity; this.serializeCast = (T x) => { return(CastTo <Int16> .From <T>(x)); }; this.deserializeCast = (Int16 x) => { return(CastTo <T> .From <Int16>(x)); }; }
/// <summary> /// Returns all of the set flags /// </summary> public IEnumerator <FlagType> GetEnumerator() { if (ActiveFlags <= 0) { yield break; } int FlagNum = 0; for (int i = 0; i < Size; i++) { uint mask = unchecked ((uint)-1); for (int j = 0; j < CHUNK_SIZE; j++) { mask <<= 1; if ((Get_Chunk_Data(i) & mask) != 0) { yield return(CastTo <FlagType> .From(FlagNum)); } FlagNum++; } } yield break; }
public NullableSByteEnumFormatter() { Func <SByte, SByte> identity = Identity; this.serializeCast = (T x) => { return(CastTo <SByte> .From <T>(x)); }; this.deserializeCast = (SByte x) => { return(CastTo <T> .From <SByte>(x)); }; }
private T ApplyConversion() { T value; if (this.converter is null) { if (typeof(T) == typeof(string) && this.option.OptionType == CommandOptionType.SingleValue) { value = CastTo <T> .From(this.option.Value()); } else if (typeof(T).IsAssignableFrom(typeof(List <T>)) && this.option.OptionType == CommandOptionType.MultipleValue) { value = CastTo <T> .From(this.option.Values); } else if (typeof(T).IsAssignableFrom(typeof(bool)) && this.option.OptionType == CommandOptionType.NoValue) { value = CastTo <T> .From(this.option.HasValue()); } else { throw new FormatException($"You cannot bind directly from the option {this.option.LongName} with type {this.option.OptionType} option to a property of type {typeof(T)}, without providing a suitable converter."); } } else { if (this.converter is Func <string, T> stringConverter && this.option.OptionType == CommandOptionType.SingleValue) { value = stringConverter(this.option.Value()); }
public void TestCastReferenceTypes() { var from = new Tuple <ulong>(0x0123456789abcdef); var to = new Tuple <uint, uint>(0x89abcdef, 0x01234567); Assert.Throws <TypeInitializationException>(() => CastTo <Tuple <uint, uint> > .From(@from)); }
public static T Evaluate <T>(this BindingExpression bindingExpression) { bindingExpression.ThrowIfNull(nameof(bindingExpression)); object?resolvedSource = bindingExpression.ResolvedSource; if (resolvedSource is null) { throw new ArgumentException( "Failed to evalute binding expression. Source is not resolved.", nameof(bindingExpression) ); } Type resolvedType = resolvedSource.GetType(); PropertyInfo?prop = resolvedType.GetProperty( bindingExpression.ResolvedSourcePropertyName ); if (prop is null) { throw new ArgumentException( "Failed to evalute binding expression.", nameof(bindingExpression) ); } return(CastTo <T> .From(prop.GetValue(bindingExpression.ResolvedSource))); }
private CastDefinition(TIn inputValue, CastTo <TIn, TOut> castMethod, int id) { From = typeof(TIn); To = typeof(TOut); Id = id; InputValue = inputValue; Cast = castMethod; }
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is TSource v) { return(Convert(v)); } return(Convert(CastTo <TSource> .From(value))); }
public void TestCastValueTypes() { int updateOrdinal = (int)AddUpdateRemove.Update; int removeOrdinal = (int)AddUpdateRemove.Remove; Assert.AreEqual(AddUpdateRemove.Update, CastTo <AddUpdateRemove> .From(updateOrdinal)); Assert.AreEqual(AddUpdateRemove.Remove, CastTo <AddUpdateRemove> .From(removeOrdinal)); }
/// <summary> /// Retreives this value as the requested type if possible /// </summary> public T AsEnum <T>() { if (Type != EAttributeType.Enumerated) { throw new Exception(ExceptionMessages.ERROR_Attribute_Type_Mismatch); } return(CastTo <T> .From(Value)); }
public void Allocate <TEnum>(TEnum T) { _data = new TValue[CastTo <int> .From(T)]; for (int i = 0; i < _data.Length; ++i) { _data[i] = default(TValue); } }
public TEnum[] ReadUInt8Array <TEnum>(int length) where TEnum : Enum { TEnum[] data = new TEnum[length]; for (int i = 0; i < length; i++) { data[i] = CastTo <TEnum> .From(this.ReadUInt8()); } return(data); }
public TValue Get <TEnum>(TEnum T) { try { return(_data[CastTo <int> .From(T)]); } catch (Exception ex) { ex.ToString(); return(default(TValue)); } }
static void Main(string[] args) { object o = (int)1234; var v = CastTo <byte> .From(o); //int r = o as int; Expression <Func <object, int> > e = T => (int)T; ParameterExpression p = Expression.Parameter(typeof(object), "source"); var u = Expression.Unbox(p, typeof(int)); var c = Expression.ConvertChecked(u, typeof(int)); var l = Expression.Lambda <Func <object, int> >(c, p).Compile(); //int v = l(o); //Expression.Unbox(p, }
public TEnum ReadUInt16 <TEnum>() => CastTo <TEnum> .From(this.ReadUInt16());
public TEnum ReadInt16 <TEnum>() where TEnum : Enum => CastTo <TEnum> .From(this.ReadInt16());
public void Set <TEnum>(TEnum Tenum, TValue Tvalue) { _data[CastTo <int> .From(Tenum)] = Tvalue; }
internal override bool Equals(TEnum lhs, TEnum rhs) { return(CastTo <long> .From <TEnum>(lhs) == CastTo <long> .From <TEnum>(rhs)); }
internal override TEnum Deserialize(ISettingSerializer serializer) { long persistedValue = serializer.Deserialize(this, CastTo <long> .From <TEnum>(DefaultValue)); return(CastTo <TEnum> .From <long>(persistedValue)); }
public CastDefinition(TIn inputValue, CastTo <TIn, TOut> castMethod, Type expectedException, [CallerLineNumber] int id = 0) : this(inputValue, castMethod, id) { ExpectedException = expectedException; IsValid = false; }
public CastDefinition(TIn inputValue, CastTo <TIn, TOut> castMethod, TOut expectedResult, [CallerLineNumber] int id = 0) : this(inputValue, castMethod, id) { ExpectedResult = expectedResult; IsValid = true; }
public TEnum ReadInt16 <TEnum>() where TEnum : Enum { return(CastTo <TEnum> .From(this.ReadInt16())); }
public TEnum ReadUInt16 <TEnum>() { return(CastTo <TEnum> .From(this.ReadUInt16())); }
public static EnumType FromInt(int t) { return(CastTo <EnumType> .From(t)); }
public static int ToInt(EnumType t) { return(CastTo <int> .From(t)); }
internal NumericRangeTermsEnum(NumericRangeQuery <T> outerInstance, TermsEnum tenum) : base(tenum) { this.outerInstance = outerInstance; switch (this.outerInstance.dataType) { case NumericType.INT64: case NumericType.DOUBLE: { // lower long minBound; if (this.outerInstance.dataType == NumericType.INT64) { minBound = (!this.outerInstance.min.HasValue) ? long.MinValue : CastTo <long> .From(this.outerInstance.min.Value); } else { if (Debugging.AssertsEnabled) { Debugging.Assert(this.outerInstance.dataType == NumericType.DOUBLE); } minBound = (!this.outerInstance.min.HasValue) ? INT64_NEGATIVE_INFINITY : NumericUtils.DoubleToSortableInt64(CastTo <double> .From(this.outerInstance.min.Value)); } if (!this.outerInstance.minInclusive && this.outerInstance.min != null) { if (minBound == long.MaxValue) { break; } minBound++; } // upper long maxBound; if (this.outerInstance.dataType == NumericType.INT64) { maxBound = (!this.outerInstance.max.HasValue) ? long.MaxValue : CastTo <long> .From(this.outerInstance.max.Value); } else { if (Debugging.AssertsEnabled) { Debugging.Assert(this.outerInstance.dataType == NumericType.DOUBLE); } maxBound = (!this.outerInstance.max.HasValue) ? INT64_POSITIVE_INFINITY : NumericUtils.DoubleToSortableInt64(CastTo <double> .From(this.outerInstance.max.Value)); } if (!this.outerInstance.maxInclusive && this.outerInstance.max != null) { if (maxBound == long.MinValue) { break; } maxBound--; } NumericUtils.SplitInt64Range(new Int64RangeBuilderAnonymousClass(this), this.outerInstance.precisionStep, minBound, maxBound); break; } case NumericType.INT32: case NumericType.SINGLE: { // lower int minBound; if (this.outerInstance.dataType == NumericType.INT32) { minBound = (!this.outerInstance.min.HasValue) ? int.MinValue : CastTo <int> .From(this.outerInstance.min.Value); } else { if (Debugging.AssertsEnabled) { Debugging.Assert(this.outerInstance.dataType == NumericType.SINGLE); } minBound = (!this.outerInstance.min.HasValue) ? INT32_NEGATIVE_INFINITY : NumericUtils.SingleToSortableInt32(CastTo <float> .From(this.outerInstance.min.Value)); } if (!this.outerInstance.minInclusive && this.outerInstance.min != null) { if (minBound == int.MaxValue) { break; } minBound++; } // upper int maxBound; if (this.outerInstance.dataType == NumericType.INT32) { maxBound = (!this.outerInstance.max.HasValue) ? int.MaxValue : CastTo <int> .From(this.outerInstance.max.Value); } else { if (Debugging.AssertsEnabled) { Debugging.Assert(this.outerInstance.dataType == NumericType.SINGLE); } maxBound = (!this.outerInstance.max.HasValue) ? INT32_POSITIVE_INFINITY : NumericUtils.SingleToSortableInt32(CastTo <float> .From(this.outerInstance.max.Value)); } if (!this.outerInstance.maxInclusive && this.outerInstance.max != null) { if (maxBound == int.MinValue) { break; } maxBound--; } NumericUtils.SplitInt32Range(new Int32RangeBuilderAnonymousClass(this), this.outerInstance.precisionStep, minBound, maxBound); break; } default: // should never happen throw new ArgumentException("Invalid NumericType"); } termComp = Comparer; }