//========================================================================== private static void ConvertColors(Brush brush) { if (brush != null) { SolidColorBrush solidBrush = null; GradientBrush gradientBrush = null; if (DynamicCast.Cast(brush, out solidBrush)) { solidBrush.Color = ConvertColor(solidBrush.Color); } else if (DynamicCast.Cast(brush, out gradientBrush)) { GradientStopCollection stopColl = gradientBrush.GradientStops; foreach (GradientStop stop in stopColl) { stop.Color = ConvertColor(stop.Color); } } //else if (brush is DrawingBrush) //{ // ConvertColors((brush as DrawingBrush).Drawing); //} else { throw new NotSupportedException(); } } }
public static bool Or(dynamic a, dynamic b) { bool resultA = DynamicCast.ConvertToBoolean(a); bool resultB = DynamicCast.ConvertToBoolean(b); return(resultA || resultB); }
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 bool Less(dynamic a, dynamic b) { double resultA = DynamicCast.ConvertToDouble(a); double resultB = DynamicCast.ConvertToDouble(b); return(resultA < resultB); }
public static bool GreaterOrEquals(dynamic a, dynamic b) { double resultA = DynamicCast.ConvertToDouble(a); double resultB = DynamicCast.ConvertToDouble(b); return(resultA >= resultB); }
public static bool Not(dynamic a) { #if !NET_STANDARD_2_0 bool resultA = DynamicCast.ConvertToBoolean(a); return(!resultA); #else throw new WrongApiCompatibilityLevelException(); #endif }
public static bool Or(dynamic a, dynamic b) { #if !NET_STANDARD_2_0 bool resultA = DynamicCast.ConvertToBoolean(a); bool resultB = DynamicCast.ConvertToBoolean(b); return(resultA || resultB); #else throw new WrongApiCompatibilityLevelException(); #endif }
public static bool GreaterOrEquals(dynamic a, dynamic b) { #if !NET_STANDARD_2_0 double resultA = DynamicCast.ConvertToDouble(a); double resultB = DynamicCast.ConvertToDouble(b); return(resultA >= resultB); #else throw new WrongApiCompatibilityLevelException(); #endif }
public new static bool Equals(dynamic a, dynamic b) { dynamic resultA = a == null ? 0 : a; dynamic resultB = b == null ? 0 : b; if (resultA.GetType() != resultB.GetType()) { DynamicCast.CastValue(a, b, ref resultA, ref resultB); } return(resultA == resultB); }
public new static bool Equals(dynamic a, dynamic b) { #if !NET_STANDARD_2_0 dynamic resultA = a == null ? 0 : a; dynamic resultB = b == null ? 0 : b; if (resultA.GetType() != resultB.GetType()) { DynamicCast.CastValue(a, b, ref resultA, ref resultB); } return(resultA == resultB); #else throw new WrongApiCompatibilityLevelException(); #endif }
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 Geometry CreateGeometry(SvgPathElement element) { PathGeometry geometry = new PathGeometry(); string fillRule = element.GetPropertyValue("fill-rule"); string clipRule = element.GetAttribute("clip-rule"); if (!string.IsNullOrWhiteSpace(clipRule) && string.Equals(clipRule, "evenodd") || string.Equals(clipRule, "nonzero")) { fillRule = clipRule; } if (fillRule == "evenodd") { geometry.FillRule = FillRule.EvenOdd; } else if (fillRule == "nonzero") { geometry.FillRule = FillRule.Nonzero; } SvgPointF initPoint = new SvgPointF(0, 0); SvgPointF lastPoint = new SvgPointF(0, 0); ISvgPathSeg segment = null; SvgPathSegMoveto pathMoveTo = null; SvgPathSegLineto pathLineTo = null; SvgPathSegCurveto pathCurveTo = null; SvgPathSegArc pathArc = null; ISvgPathSegList segments = element.PathSegList; int nElems = segments.NumberOfItems; PathFigure pathFigure = null; for (int i = 0; i < nElems; i++) { segment = segments.GetItem(i); if (DynamicCast.Cast(segment, out pathMoveTo)) { if (pathFigure != null) { pathFigure.IsClosed = false; pathFigure.IsFilled = true; geometry.Figures.Add(pathFigure); pathFigure = null; } lastPoint = initPoint = pathMoveTo.AbsXY; pathFigure = new PathFigure(); pathFigure.StartPoint = new Point(initPoint.ValueX, initPoint.ValueY); } else if (DynamicCast.Cast(segment, out pathLineTo)) { SvgPointF p = pathLineTo.AbsXY; pathFigure.Segments.Add(new LineSegment(new Point(p.ValueX, p.ValueY), true)); lastPoint = p; } else if (DynamicCast.Cast(segment, out pathCurveTo)) { SvgPointF xy = pathCurveTo.AbsXY; SvgPointF x1y1 = pathCurveTo.CubicX1Y1; SvgPointF x2y2 = pathCurveTo.CubicX2Y2; pathFigure.Segments.Add(new BezierSegment(new Point(x1y1.ValueX, x1y1.ValueY), new Point(x2y2.ValueX, x2y2.ValueY), new Point(xy.ValueX, xy.ValueY), true)); lastPoint = xy; } else if (DynamicCast.Cast(segment, out pathArc)) { SvgPointF p = pathArc.AbsXY; if (lastPoint.Equals(p)) { // If the endpoints (x, y) and (x0, y0) are identical, then this // is equivalent to omitting the elliptical arc segment entirely. } else if (pathArc.R1.Equals(0) || pathArc.R2.Equals(0)) { // Ensure radii are valid pathFigure.Segments.Add(new LineSegment(new Point(p.ValueX, p.ValueY), true)); } else { CalculatedArcValues calcValues = pathArc.GetCalculatedArcValues(); pathFigure.Segments.Add(new ArcSegment(new Point(p.ValueX, p.ValueY), new Size(pathArc.R1, pathArc.R2), pathArc.Angle, pathArc.LargeArcFlag, pathArc.SweepFlag ? SweepDirection.Clockwise : SweepDirection.Counterclockwise, true)); } lastPoint = p; } else if (segment is SvgPathSegClosePath) { if (pathFigure != null) { pathFigure.IsClosed = true; pathFigure.IsFilled = true; geometry.Figures.Add(pathFigure); pathFigure = null; } lastPoint = initPoint; } } if (pathFigure != null) { pathFigure.IsClosed = false; pathFigure.IsFilled = true; geometry.Figures.Add(pathFigure); } return(geometry); }
public static bool Not(dynamic a) { bool resultA = DynamicCast.ConvertToBoolean(a); return(!resultA); }
public static GraphicsPath CreatePath(SvgPathElement element) { GraphicsPath gp = new GraphicsPath(); SvgPointF initPoint = new SvgPointF(0, 0); SvgPointF lastPoint = new SvgPointF(0, 0); ISvgPathSeg segment = null; SvgPathSegMoveto pathMoveTo = null; SvgPathSegLineto pathLineTo = null; SvgPathSegCurveto pathCurveTo = null; SvgPathSegArc pathArc = null; ISvgPathSegList segments = element.PathSegList; int nElems = segments.NumberOfItems; for (int i = 0; i < nElems; i++) { segment = segments.GetItem(i); if (DynamicCast.Cast(segment, out pathMoveTo)) { //SvgPathSegMoveto seg = (SvgPathSegMoveto)segment; gp.StartFigure(); lastPoint = initPoint = pathMoveTo.AbsXY; } else if (DynamicCast.Cast(segment, out pathLineTo)) { //SvgPathSegLineto seg = (SvgPathSegLineto)segment; SvgPointF p = pathLineTo.AbsXY; gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y); lastPoint = p; } else if (DynamicCast.Cast(segment, out pathCurveTo)) { // SvgPathSegCurveto seg = (SvgPathSegCurveto)segment; SvgPointF xy = pathCurveTo.AbsXY; SvgPointF x1y1 = pathCurveTo.CubicX1Y1; SvgPointF x2y2 = pathCurveTo.CubicX2Y2; gp.AddBezier(lastPoint.X, lastPoint.Y, x1y1.X, x1y1.Y, x2y2.X, x2y2.Y, xy.X, xy.Y); lastPoint = xy; } else if (DynamicCast.Cast(segment, out pathArc)) { //SvgPathSegArc seg = (SvgPathSegArc)segment; SvgPointF p = pathArc.AbsXY; if (lastPoint.Equals(p)) { // If the endpoints (x, y) and (x0, y0) are identical, then this // is equivalent to omitting the elliptical arc segment entirely. } else if (pathArc.R1 == 0 || pathArc.R2 == 0) { // Ensure radii are valid gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y); } else { CalculatedArcValues calcValues = pathArc.GetCalculatedArcValues(); GraphicsPath gp2 = new GraphicsPath(); gp2.StartFigure(); gp2.AddArc((float)(calcValues.Cx - calcValues.CorrRx), (float)(calcValues.Cy - calcValues.CorrRy), (float)calcValues.CorrRx * 2, (float)calcValues.CorrRy * 2, (float)calcValues.AngleStart, (float)calcValues.AngleExtent); Matrix matrix = new Matrix(); matrix.Translate(-(float)calcValues.Cx, -(float)calcValues.Cy); gp2.Transform(matrix); matrix = new Matrix(); matrix.Rotate((float)pathArc.Angle); gp2.Transform(matrix); matrix = new Matrix(); matrix.Translate((float)calcValues.Cx, (float)calcValues.Cy); gp2.Transform(matrix); gp.AddPath(gp2, true); } lastPoint = p; } else if (segment is SvgPathSegClosePath) { gp.CloseFigure(); lastPoint = initPoint; } } string fillRule = element.GetPropertyValue("fill-rule"); if (fillRule == "evenodd") { gp.FillMode = FillMode.Alternate; } else { gp.FillMode = FillMode.Winding; } return(gp); }
public ITypeConverter TryGetConverter(Type sourceType, Type destinationType, bool findIntermediateConverter = true, int depth = 1) { // first check if a custom converter is registered // then try other conversions if (converters.TryGetValue(Tuple.Create(sourceType, destinationType), out ITypeConverter converter)) { return(converter); } else if (sourceType == destinationType || destinationType.IsAssignableFrom(sourceType)) { return(AssignmentConverter(sourceType, destinationType)); } else if (typeof(IConvertible).IsAssignableFrom(sourceType) && typeof(IConvertible).IsAssignableFrom(destinationType)) { return(new TypeConverter(sourceType, destinationType, x => Convert.ChangeType(x, destinationType, CultureInfo.InvariantCulture))); } if (sourceType == typeof(object)) { return(new TypeConverter(sourceType, destinationType, value => { var valueType = value.GetType(); var innerConverter = TryGetConverter(valueType, destinationType); if (innerConverter == null) { throw new Exception(string.Format("Converter from type {0} to type {1} not found.", valueType, destinationType)); } return innerConverter.Convert(value); })); } else if (sourceType.IsArray && destinationType.IsArray) { var sourceElementType = sourceType.GetElementType(); var destinationElementType = destinationType.GetElementType(); if (sourceElementType != typeof(object) && destinationElementType != typeof(object)) { converter = TryGetConverter(sourceElementType, destinationElementType, findIntermediateConverter, depth); if (converter != null) { return(new TypeConverter(sourceType, destinationType, CreateArrayConverter(destinationElementType, converter))); } } else { return(new TypeConverter(sourceElementType, destinationElementType, source => { var sourceArray = (Array)source; var destinationArray = Array.CreateInstance(destinationElementType, sourceArray.Length); sourceArray.CopyTo(destinationArray, 0); return destinationArray; })); } } else if (sourceType.IsArray && (destinationType.HasGenericTypeBase(typeof(ISequence <>)) || destinationType == typeof(ISequence))) { Type destinationGenericType = destinationType; if (destinationType.GetTypeInfo().IsGenericType) { destinationGenericType = destinationType.GetGenericTypeDefinition(); } Func <Type, Type, ITypeConverter> converterFactory; if (this.converterFactories.TryGetValue(Tuple.Create(typeof(Array), destinationGenericType), out converterFactory)) { return(converterFactory.Invoke(sourceType, destinationType)); } } else if (sourceType.HasGenericTypeBase(typeof(ISequence <>)) && destinationType.IsArray) { Func <Type, Type, ITypeConverter> converterFactory; if (this.converterFactories.TryGetValue(Tuple.Create(typeof(ISequence <>), typeof(Array)), out converterFactory)) { return(converterFactory.Invoke(sourceType, destinationType)); } } else if (sourceType.GetTypeInfo().IsGenericType || destinationType.GetTypeInfo().IsGenericType) { Type sourceGenericType = sourceType; if (sourceType.GetTypeInfo().IsGenericType) { sourceGenericType = sourceType.GetGenericTypeDefinition(); } Type destinationGenericType = destinationType; if (destinationType.GetTypeInfo().IsGenericType) { destinationGenericType = destinationType.GetGenericTypeDefinition(); } var typeTuple = Tuple.Create(sourceGenericType, destinationGenericType); Func <Type, Type, ITypeConverter> converterFactory; if (this.converterFactories.TryGetValue(typeTuple, out converterFactory)) { return(converterFactory.Invoke(sourceType, destinationType)); } // check if * -> destination type converter exists if (this.converterFactories.TryGetValue(Tuple.Create <Type, Type>(null, destinationGenericType ?? destinationType), out converterFactory)) { converter = converterFactory.Invoke(sourceType, destinationType); if (converter != null) { return(converter); } } } if (sourceType == typeof(object[])) { return(new TypeConverter(sourceType, destinationType, value => { var sourceArray = (Array)value; if (sourceArray.Length == 0) { if (destinationType.IsArray || destinationType.HasGenericTypeBase(typeof(IEnumerable <>))) { var underlyingDestinationType = destinationType.GetElementType() ?? destinationType.GetGenericArguments().First(); return Array.CreateInstance(underlyingDestinationType, 0); } else if (!destinationType.GetTypeInfo().IsInterface) { return Activator.CreateInstance(destinationType); } else { throw new Exception("Cannot estimate destinationType due to empty array."); } } var valueType = sourceArray.GetValue(0).GetType(); var innerConverter = TryGetConverter(valueType.MakeArrayType(), destinationType); if (innerConverter == null) { throw new Exception(string.Format("Converter from type {0} to type {1} not found.", valueType.MakeArrayType(), destinationType)); } return innerConverter.Convert(DynamicCast.DynamicCastTo(value, innerConverter.SourceType)); })); } if (destinationType != typeof(string) && findIntermediateConverter) { if (converterList.TryGetValue(sourceType, out List <Type> list)) { var intermediateConverter2 = list.Select(x => TryGetConverter(x, destinationType, false, depth)).FirstOrDefault(); if (intermediateConverter2 != null) { var intermediateConverter1 = this.TryGetConverter(sourceType, intermediateConverter2.SourceType, false, depth); return(new TypeConverter(sourceType, intermediateConverter1.DestinationType, destinationType, x => intermediateConverter2.Convert(intermediateConverter1.Convert(x)))); } } } // check base class converter if (depth > 0) { // filter object out, because it will match for every type.. var sourceBaseTypes = new[] { sourceType, sourceType.GetTypeInfo().BaseType } .Concat(sourceType.GetInterfaces()) .Where(x => x != null && x != typeof(object)) .ToList(); var destinationBaseTypes = new[] { destinationType.GetTypeInfo().BaseType } .Concat(destinationType.GetInterfaces()) .Where(x => x != null && x != typeof(object)) .ToList(); destinationBaseTypes.Sort(new TypeSorter()); // check if a simple conversion exists foreach (var sourceBaseType in sourceBaseTypes) { converter = TryGetConverter(sourceBaseType, destinationType, findIntermediateConverter, depth - 1); if (converter != null) { return(converter); } } foreach (var sourceBaseType in sourceBaseTypes) { foreach (var destinationBaseType in destinationBaseTypes) { var intermediateConverter2 = TryGetConverter(destinationBaseType, destinationType, false, depth - 1); if (intermediateConverter2 != null) { var intermediateConverter1 = TryGetConverter(sourceBaseType, destinationBaseType, false, depth - 1); if (intermediateConverter1 != null) { return(new TypeConverter(sourceType, destinationBaseType, destinationType, x => intermediateConverter2.Convert(intermediateConverter1.Convert(x)))); } } } } } if (destinationType == typeof(string) && depth > 0) { return(ToStringConverter(sourceType, destinationType)); } return(null); }
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"); } }