public static object CastTo(object source, Type to) { if (source != null) { if (source.GetType().CanBeTreatedAsType(to)) { return(source); // for efficiency } source = UnboxTo(source, to); if (source != null) { Cast cast = getImplicitCast(source.GetType(), to); if (cast == null) { var message = "Cannot cast from '{0}' to '{1}'".FormatWith(Typecasts.ReadableFhirPathName(source), Typecasts.ReadableTypeName(to)); throw new InvalidCastException(message); } return(cast(source)); } } //if source == null, or unboxed source == null.... if (to == typeof(IEnumerable <ITypedElement>)) { return(ElementNode.EmptyList); } if (to.IsNullable()) { return(null); } else { throw new InvalidCastException("Cannot cast a null value to non-nullable type '{0}'".FormatWith(to.Name)); } }
public static Invokee Wrap <A, B, R>(Func <A, B, R> func, bool propNull) { return((ctx, args) => { var focus = args.First()(ctx, InvokeeFactory.EmptyArgs); if (propNull && !focus.Any()) { return FhirValueList.Empty; } if (typeof(B) != typeof(EvaluationContext)) { var newCtx = ctx.Nest(focus); var argA = args.Skip(1).First()(newCtx, InvokeeFactory.EmptyArgs); if (propNull && !argA.Any()) { return FhirValueList.Empty; } return Typecasts.CastTo <IEnumerable <IElementNavigator> >(func(Typecasts.CastTo <A>(focus), Typecasts.CastTo <B>(argA))); } else { B lastPar = (B)(object)ctx.EvaluationContext; return Typecasts.CastTo <IEnumerable <IElementNavigator> >(func(Typecasts.CastTo <A>(focus), lastPar)); } }); }
public static Invokee Wrap <A, B, C, D, R>(Func <A, B, C, D, R> func, bool propNull) { return((ctx, args) => { var focus = args.First()(ctx, InvokeeFactory.EmptyArgs); if (propNull && !focus.Any()) { return ElementNode.EmptyList; } var newCtx = ctx.Nest(focus); var argA = args.Skip(1).First()(newCtx, InvokeeFactory.EmptyArgs); if (propNull && !argA.Any()) { return ElementNode.EmptyList; } var argB = args.Skip(2).First()(newCtx, InvokeeFactory.EmptyArgs); if (propNull && !argB.Any()) { return ElementNode.EmptyList; } if (typeof(D) != typeof(EvaluationContext)) { var argC = args.Skip(3).First()(newCtx, InvokeeFactory.EmptyArgs); if (propNull && !argC.Any()) { return ElementNode.EmptyList; } return Typecasts.CastTo <IEnumerable <ITypedElement> >(func(Typecasts.CastTo <A>(focus), Typecasts.CastTo <B>(argA), Typecasts.CastTo <C>(argB), Typecasts.CastTo <D>(argC))); } else { D lastPar = (D)(object)ctx.EvaluationContext; return Typecasts.CastTo <IEnumerable <ITypedElement> >(func(Typecasts.CastTo <A>(focus), Typecasts.CastTo <B>(argA), Typecasts.CastTo <C>(argB), lastPar)); } }); }
public static object CastTo(object source, Type to) { if (source != null) { if (source.GetType().CanBeTreatedAsType(to)) { return(source); // for efficiency } source = Unbox(source, to); if (source != null) { Cast cast = getImplicitCast(source.GetType(), to); if (cast == null) { //TODO: Spell out why a little bit more explicit than... throw new InvalidCastException("Cannot cast from '{0}' to '{1}'".FormatWith(Typecasts.ReadableFhirPathName(source.GetType()), Typecasts.ReadableFhirPathName(to))); } return(cast(source)); } } //if source == null, or unboxed source == null.... if (to == typeof(IEnumerable <ITypedElement>)) { return(FhirValueList.Empty); } if (to.IsNullable()) { return(null); } else { throw new InvalidCastException("Cannot cast a null value to non-nullable type '{0}'".FormatWith(to.Name)); } }
//public bool Matches(string functionName, IEnumerable<Type> argumentTypes) //{ // return functionName == Name && argumentTypes.Count() == ArgumentTypes.Count() && // argumentTypes.Zip(ArgumentTypes, (call, sig) => Typecasts.CanCastTo(call,sig)).All(r => r == true); //} public bool DynamicMatches(string functionName, IEnumerable <object> arguments) { return(functionName == Name && arguments.Count() == ArgumentTypes.Count() && arguments.Zip(ArgumentTypes, (call, sig) => Typecasts.CanCastTo(call, sig)).All(r => r == true)); }
public static Invokee Wrap <A, B, C, R>(Func <A, B, C, R> func, bool propNull) { return((ctx, args) => { var focus = args.First()(ctx, InvokeeFactory.EmptyArgs); if (propNull && !focus.Any()) { return FhirValueList.Empty; } var newCtx = ctx.Nest(focus); var argA = args.Skip(1).First()(newCtx, InvokeeFactory.EmptyArgs); if (propNull && !argA.Any()) { return FhirValueList.Empty; } var argB = args.Skip(2).First()(newCtx, InvokeeFactory.EmptyArgs); if (propNull && !argB.Any()) { return FhirValueList.Empty; } return Typecasts.CastTo <IEnumerable <IElementNavigator> >(func(Typecasts.CastTo <A>(focus), Typecasts.CastTo <B>(argA), Typecasts.CastTo <C>(argB))); }); }