コード例 #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="parserContext"></param>
 /// <param name="kind"></param>
 /// <param name="castTypeName"></param>
 /// <param name="expression"></param>
 public CastExpression(IParserLocation parserContext, CoercionKind kind, string castTypeName, IExpression expression)
 {
     Location     = parserContext;
     Kind         = kind;
     CastTypeName = castTypeName;
     Expression   = expression;
 }
コード例 #2
0
 /// <summary>
 /// Creates a coercion rule
 /// </summary>
 /// <param name="kind"></param>
 /// <param name="originalType"></param>
 /// <param name="castingType"></param>
 /// <param name="cast"></param>
 public CoercionRule(CoercionKind kind, Type originalType, Type castingType, Func <object, object> cast)
 {
     Kind         = kind;
     OriginalType = originalType;
     CastingType  = castingType;
     Cast         = cast;
 }
コード例 #3
0
        /// <summary>
        /// Adds a new coercion rule. Trys to avoid cyclic implicit casts chains.
        /// </summary>
        /// <typeparam name="TOriginalType"></typeparam>
        /// <typeparam name="TCastingType"></typeparam>
        /// <param name="kind"></param>
        /// <param name="cast"></param>
        public void AddCoercionRule <TOriginalType, TCastingType>(CoercionKind kind, Func <TOriginalType, TCastingType> cast)
        {
            var original = typeof(TOriginalType);
            var casting  = typeof(TCastingType);

            if (GetTypeByNative(original) == null)
            {
                throw new UnknownTypeException(original);
            }
            if (GetTypeByNative(casting) == null)
            {
                throw new UnknownTypeException(casting);
            }
            var rule = new CoercionRule(kind, original, casting, a => cast((TOriginalType)a));

            if (allCoercionRules.TryGetEdge(original, casting, out TaggedEdge <Type, CoercionRule> existingEdge))
            {
                throw new InvalidOperationException("Such a rule does already exist!");
            }
            var edge = new TaggedEdge <System.Type, CoercionRule>(original, casting, rule);

            if (kind == CoercionKind.Implicit)
            {
                implicitCoercionRules.AddVertex(original);
                implicitCoercionRules.AddVertex(casting);
                implicitCoercionRules.AddEdge(edge);
                if (implicitCoercionRules.StronglyConnectedComponents(out IDictionary <Type, int> components) != implicitCoercionRules.Vertices.Count())
                {
                    implicitCoercionRules.RemoveEdge(edge);
                    throw new InvalidOperationException("This action would create an implicit conversion cycle!");
                }
            }
            allCoercionRules.AddVertex(original);
            allCoercionRules.AddVertex(casting);
            allCoercionRules.AddEdge(edge);
            Debug.WriteLine($"- added coercion rule from '{original.Name}' to '{casting.Name}'");
        }
コード例 #4
0
 /// <summary>
 /// Adds a coercion rule.
 /// </summary>
 /// <typeparam name="TOriginalType"></typeparam>
 /// <typeparam name="TCastingType"></typeparam>
 /// <param name="kind"></param>
 /// <param name="cast"></param>
 public void AddCoercionRule <TOriginalType, TCastingType>(CoercionKind kind, Func <TOriginalType, TCastingType> cast)
 {
     typeSystem.AddCoercionRule(kind, cast);
 }