protected override object Convert(object @object, IType from, IType to) { if (!viewTypePredicate(to)) { throw new CannotConvertException(@object, to); } return(from.Cast(@object, to)); }
public IConstraint FormulateFieldConstraints(IQuery query, OMQueryClause clause) { try { IConstraint cons = null; string[] str = clause.Fieldname.Split('.'); IQuery q = AddAsDescends(query, str); IType type = Db4oClient.TypeResolver.Resolve(clause.FieldType); switch (type.DisplayName) { case BusinessConstants.DATETIME: { IConstraint c1 = null, c2 = null; DateTimeFormatInfo dateTimeFormatterProvider = DateTimeFormatInfo.CurrentInfo.Clone() as DateTimeFormatInfo; dateTimeFormatterProvider.ShortDatePattern = "MM/dd/yyyy hh:mm:ss tt"; DateTime dt = DateTime.Parse(clause.Value.Trim(), dateTimeFormatterProvider); DateTime dt1 = dt.AddDays(-1); DateTime dt2 = dt.AddDays(1); if (clause.Operator.Equals(BusinessConstants.CONDITION_EQUALS)) { cons = q.Constrain(dt2).Smaller().And(q.Constrain(dt1).Greater()); } else if (clause.Operator.Equals(BusinessConstants.CONDITION_GREATERTHAN)) { c1 = q.Constrain(dt2).Greater(); c2 = q.Constrain(dt2.AddDays(1)).Smaller().And(q.Constrain(dt).Greater()); cons = c1.Or(c2); c1 = null; c2 = null; } else if (clause.Operator.Equals(BusinessConstants.CONDITION_LESSTHAN)) { c1 = q.Constrain(dt1).Smaller(); c2 = q.Constrain(dt).Smaller().And(q.Constrain(dt1.AddDays(-1)).Greater()); cons = c1.Or(c2); } break; } default: cons = q.Constrain(type.Cast(clause.Value)); break; } return(cons); } catch (Exception oEx) { LoggingHelper.HandleException(oEx); return(null); } }
public void Cannot_cast_to_a_real_type() { IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") ; Assert.Throws <InvalidCastException>(() => testing.Cast(testing.CreateInstance(), type.of <string>())); }
public static void SetObject(IReflectField rfield, object containingObject, IType fieldType, string newValue) { try { rfield.Set(containingObject, fieldType.Cast(newValue)); } catch (Exception oEx) { Db4oClient.Client.Rollback(); LoggingHelper.HandleException(oEx); } }
public void Casts_a_virtual_object_to_object() { IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") ; var instance = testing.CreateInstance(); Assert.AreSame(instance, testing.Cast(instance, type.of <object>())); }
public object ReturnCastObject(string classname, object data) { if (null == data && "null" == data.ToString()) { return(false); } IType objectType = dbInteraction.GetType(classname); if (objectType == null) { return(false); } return(objectType.Cast(data)); }
public bool CheckIfObjectCanBeCasted(string classname, object data) { if (null == data && "null" == data.ToString()) { return(false); } IType objectType = dbInteraction.GetType(classname); if (objectType == null) { return(false); } objectType.Cast(data); return(true); }
public void Cannot_cast_to_a_virtual_type_that_is_not_in_assignable_types() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") ; Assert.Throws <InvalidCastException>(() => testing.Cast(testing.CreateInstance(), virtualInterface)); }
public void Cannot_cast_a_real_object() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") .AssignableTypes.Add(virtualInterface) ; Assert.Throws <InvalidCastException>(() => testing.Cast("string", virtualInterface)); }
private static IEnumerable <IType> GetAllTypesIfUnion(IType type, ITypeChecker typeChecker, HashSet <IType> types) { if ((type.Flags & TypeFlags.Union) != TypeFlags.None) { foreach (var unionType in type.Cast <IUnionType>().Types) { types.AddRange(GetAllTypesIfUnion(unionType, typeChecker, types)); } } else { types.Add(type); } return(types); }
public static bool ValidateDataType(IType objectType, object data) { if (null == data && "null"==data.ToString() ) return false ; try { objectType.Cast(data); return true; } catch (Exception oEx) { LoggingHelper.ShowMessage(oEx); } return false; }
private static object SetField(string attribName, object subObject, object newValue) { try { IReflectClass rclass = DataLayerCommon.ReflectClassFor(subObject); if (rclass == null) { return(null); } IReflectField rfield = DataLayerCommon.GetDeclaredFieldInHeirarchy(rclass, attribName); if (rfield == null) { return(null); } if (rfield is GenericVirtualField || rfield.IsStatic()) { return(null); } IType fieldType = Db4oClient.TypeResolver.Resolve(rfield.GetFieldType()); if (!fieldType.IsEditable) { if (!fieldType.IsCollection && !fieldType.IsArray) { subObject = rfield.Get(subObject); Db4oClient.Client.Ext().Activate(subObject, 2); return(subObject); } } else if (subObject != null) { rfield.Set(subObject, fieldType.Cast(newValue)); return(subObject); } return(null); } catch (Exception oEx) { LoggingHelper.HandleException(oEx); return(null); } }
public void Casts_a_virtual_object_to_its_assignable_type() { var virtualInterface = BuildRoutine.VirtualType().FromBasic() .Name.Set("IVirtual") .Namespace.Set("Routine") .IsInterface.Set(true); IType testing = BuildRoutine.VirtualType().FromBasic() .DefaultInstanceId.Set("Id") .Name.Set("Virtual") .Namespace.Set("Routine") .AssignableTypes.Add(virtualInterface) ; var instance = testing.CreateInstance(); Assert.AreSame(instance, testing.Cast(instance, virtualInterface)); }
public static bool ValidateDataType(IType objectType, object data) { if (null == data && "null" == data.ToString()) { return(false); } try { objectType.Cast(data); return(true); } catch (Exception oEx) { LoggingHelper.ShowMessage(oEx); } return(false); }
/// <summary> /// Creates a string representation of a type to display in hover. /// </summary> private static string CreateStringFromType([CanBeNull] IType type) { if (type == null) { return(string.Empty); } var declaration = type?.Symbol?.Declarations?.FirstOrDefault(); if (declaration != null) { // Handle a few function types uniquely, such as function declarations // and function types. // For those, just return the display string BuildXL already provides. if (declaration.Kind == SyntaxKind.FunctionDeclaration) { return(declaration.Cast <IFunctionDeclaration>().ToDisplayString()); } if (declaration.Kind == SyntaxKind.FunctionType) { // Not all "function types" can be cast to IFunctionTypeNode or IFunctionOrConstructorTypeNode. // so we will use As to just attempt the cast. var functionOrConstructorTypeNode = declaration.As <IFunctionOrConstructorTypeNode>(); if (functionOrConstructorTypeNode != null) { return(functionOrConstructorTypeNode.ToDisplayString()); } } } // For a intrinsic type (bool, string, etc.) just return the name. if ((type.Flags & TypeFlags.Intrinsic) != TypeFlags.None) { return(type.Cast <IIntrinsicType>().IntrinsicName); } // For a string literal, return the text for it. if ((type.Flags & TypeFlags.StringLiteral) != TypeFlags.None) { return(string.Format(BuildXL.Ide.LanguageServer.Strings.StringLiteralHoverFormat, type.Cast <IStringLiteralType>().Text)); } // For a union, return a string composed of all of its members. // (member | member) if ((type.Flags & TypeFlags.UnionOrIntersection) != TypeFlags.None) { var unionStrings = new List <string>(); foreach (var member in type.Cast <IUnionOrIntersectionType>().Types) { unionStrings.Add(CreateStringFromType(member)); } return(string.Format( BuildXL.Ide.LanguageServer.Strings.HoverUnionFormat, unionStrings.Aggregate((currentAggregation, nextString) => string.Format(BuildXL.Ide.LanguageServer.Strings.HoverInnerUnionFormat, currentAggregation, nextString)))); } // For a tuple, similar to a union, return a string composed of all of its elements // [element, element] if ((type.Flags & TypeFlags.Tuple) != TypeFlags.None) { var tupleStrings = new List <string>(); foreach (var member in type.Cast <ITupleType>().ElementTypes) { tupleStrings.Add(CreateStringFromType(member)); } return(string.Format( BuildXL.Ide.LanguageServer.Strings.HoverTupleTypeFormat, tupleStrings.Aggregate((currentAggregation, nextString) => string.Format(BuildXL.Ide.LanguageServer.Strings.HoverInnerTupleTypeFormat, currentAggregation, nextString)))); } // For an interface, just return the name. // We end up here if we are building a type string (say for a union) // that includes an interface in the union. // Otherwise we would have just returned a hover object for the interface // declaration above. if ((type.Flags & TypeFlags.Interface) != TypeFlags.None) { return(type.Cast <IInterfaceType>().Symbol?.Name ?? string.Empty); } // For enums, just return the name of the enum. if ((type.Flags & TypeFlags.Enum) != TypeFlags.None) { return(type.Symbol?.Name ?? string.Empty); } // Type reference refers to a type that has type arguments (such as Map, or Array) // So, for those we will create Symbol<Argument, Argument> if ((type.Flags & TypeFlags.Reference) != TypeFlags.None) { var typeAsString = declaration?.Name?.GetFormattedText() ?? string.Empty; var typeReference = type.Cast <ITypeReference>(); if (typeReference.TypeArguments?.Count > 0) { var typeArgumentStrings = typeReference.TypeArguments.Select(typeArgument => CreateStringFromType(typeArgument)); return(string.Format( BuildXL.Ide.LanguageServer.Strings.HoverTypeArgumentsTypeFormat, typeAsString, typeArgumentStrings.Aggregate((currentAggregation, nextString) => string.Format(BuildXL.Ide.LanguageServer.Strings.HoverTypeArgumentsInnerFormat, currentAggregation, nextString)))); } } return(string.Empty); }