Exemplo n.º 1
0
        public static string ToString(ActivityWithResult activity)
        {
            if (activity == null)
            {
                return(null);
            }

            if (activity.GetType().IsConstructedGenericTypeOf(typeof(Literal <>)) && CSharpConvert.IsCodePrimitive(activity.ResultType))
            {
                return(CSharpConvert.ToString(((dynamic)activity).Value));
            }

            var serializableExpression = activity as IValueSerializableExpression;

            if (serializableExpression != null && serializableExpression.CanConvertToString(null))
            {
                return(serializableExpression.ConvertToString(null));
            }

            var textExpression = activity as ITextExpression;

            if (textExpression != null)
            {
                return(textExpression.ExpressionText);
            }

            return(null);
        }
        internal static Argument MorphArgument(ModelItem originalValue, Type targetType)
        {
            Argument           morphed            = null;
            Argument           original           = (Argument)originalValue.GetCurrentValue();
            ActivityWithResult originalExpression = original.Expression;

            if (originalExpression != null)
            {
                Type expressionType        = originalExpression.GetType();
                Type expressionGenericType = expressionType.IsGenericType ? expressionType.GetGenericTypeDefinition() : null;

                if (expressionGenericType != null)
                {
                    bool isLocation = ExpressionHelper.IsGenericLocationExpressionType(originalExpression);
                    ActivityWithResult morphedExpression;
                    EditingContext     context = originalValue.GetEditingContext();
                    morphed = Argument.Create(targetType, original.Direction);
                    if (ExpressionHelper.TryMorphExpression(originalExpression, isLocation, targetType,
                                                            context, out morphedExpression))
                    {
                        morphed.Expression = morphedExpression;
                    }
                    //[....]
                }
            }
            return(morphed);
        }
Exemplo n.º 3
0
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string errorMessage = string.Empty;

            if (owner.Expression != value)
            {
                if (value != null)
                {
                    ActivityWithResult expression = (value as ModelItem).GetCurrentValue() as ActivityWithResult;
                    if (expression == null)
                    {
                        errorMessage = string.Format(CultureInfo.CurrentUICulture, SR.ExpressionTypeDonnotMatch,
                                                     expression.GetType().FullName, typeof(ActivityWithResult).FullName);
                    }
                    else if ((owner.UseLocationExpression) && (!ExpressionHelper.IsGenericLocationExpressionType(expression)))
                    {
                        errorMessage = string.Format(CultureInfo.CurrentUICulture, SR.ExpressionTypeDonnotMatch,
                                                     expression.GetType().FullName, typeof(Activity <Location>).FullName);
                    }
                    else if ((!owner.UseLocationExpression && (owner.ExpressionType != null) && (expression.ResultType != owner.ExpressionType)) ||
                             (owner.UseLocationExpression && (owner.ExpressionType != null) && (expression.ResultType != typeof(Location <>).MakeGenericType(owner.ExpressionType))))
                    {
                        errorMessage = string.Format(CultureInfo.CurrentUICulture, SR.ExpressionTypeDonnotMatch,
                                                     expression.GetType().FullName, typeof(Activity <>).MakeGenericType(owner.ExpressionType).FullName);
                    }
                }
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                //Disable ToolTip on inner editor if it has
                ToolTipService.SetIsEnabled(owner.Editor, false);
                owner.ToolTip = errorMessage;
                return(new ValidationResult(false, errorMessage));
            }
            else
            {
                ToolTipService.SetIsEnabled(owner.Editor, true);
                owner.ToolTip = null;
                return(new ValidationResult(true, null));
            }
        }
Exemplo n.º 4
0
        public override bool TryInferReturnType(ActivityWithResult expression, EditingContext context, out Type returnType)
        {
            bool succeeded = false;

            Fx.Assert(expression.GetType().GetGenericTypeDefinition() == typeof(VisualBasicValue <>) ||
                      expression.GetType().GetGenericTypeDefinition() == typeof(VisualBasicReference <>), "VisualBasicExpressionMorphHelper should only apply to VB expression.");

            SourceExpressionException compileError;
            VisualBasicSettings       settings;

            if (ExpressionHelper.IsGenericLocationExpressionType(expression))
            {
                VisualBasicDesignerHelper.RecompileVisualBasicReference(
                    expression,
                    out returnType,
                    out compileError,
                    out settings);
            }
            else
            {
                VisualBasicDesignerHelper.RecompileVisualBasicValue(
                    expression,
                    out returnType,
                    out compileError,
                    out settings);
            }
            if (compileError == null)
            {
                succeeded = true;
                if (settings != null)
                {
                    //merge with import designer
                    foreach (VisualBasicImportReference reference in settings.ImportReferences)
                    {
                        ImportDesigner.AddImport(reference.Import, context);
                    }
                }
            }
            return(succeeded);
        }
 public override bool TryInferReturnType(ActivityWithResult expression, EditingContext context, out Type returnType)
 {
     bool succeeded = false;
     Fx.Assert(expression.GetType().GetGenericTypeDefinition() == typeof(VisualBasicValue<>) ||
         expression.GetType().GetGenericTypeDefinition() == typeof(VisualBasicReference<>), "VisualBasicExpressionMorphHelper should only apply to VB expression.");
                     
     SourceExpressionException compileError;
     VisualBasicSettings settings;
     if (ExpressionHelper.IsGenericLocationExpressionType(expression))
     {
         VisualBasicDesignerHelper.RecompileVisualBasicReference(
                 expression,
                 out returnType,
                 out compileError,
                 out settings);
     }
     else
     {
         VisualBasicDesignerHelper.RecompileVisualBasicValue(
              expression,
              out returnType,
              out compileError,
              out settings);
     }
     if (compileError == null)
     {
         succeeded = true;                    
         if (settings != null)
         {
             //merge with import designer
             foreach (VisualBasicImportReference reference in settings.ImportReferences)
             {
                 ImportDesigner.AddImport(reference.Import, context);
             }
         }
     }
     return succeeded;
 }
        internal static bool TryMorphExpression(ActivityWithResult originalExpression, bool isLocation, Type targetType,
                                                EditingContext context, out ActivityWithResult morphedExpression)
        {
            bool succeeded = false;

            morphedExpression = null;
            if (originalExpression != null)
            {
                Type resultType = originalExpression.ResultType;
                if ((isLocation) && (ExpressionHelper.IsGenericLocationExpressionType(originalExpression) && (targetType == resultType.GetGenericArguments()[0])) ||
                    (!isLocation) && (resultType == targetType))
                {
                    //no need to morph
                    succeeded         = true;
                    morphedExpression = originalExpression;
                }
                else
                {
                    Type expressionType = originalExpression.GetType();
                    if (expressionType.IsGenericType)
                    {
                        expressionType = expressionType.GetGenericTypeDefinition();
                    }

                    ExpressionMorphHelperAttribute morphHelperAttribute = ExtensibilityAccessor.GetAttribute <ExpressionMorphHelperAttribute>(expressionType);
                    if (morphHelperAttribute != null)
                    {
                        ExpressionMorphHelper morphHelper = Activator.CreateInstance(morphHelperAttribute.ExpressionMorphHelperType) as ExpressionMorphHelper;
                        if (morphHelper != null)
                        {
                            succeeded = morphHelper.TryMorphExpression(originalExpression, isLocation, targetType, context, out morphedExpression);
                            if (succeeded && morphedExpression != null)
                            {
                                string editorName = ExpressionActivityEditor.GetExpressionActivityEditor(originalExpression);
                                if (!string.IsNullOrWhiteSpace(editorName))
                                {
                                    ExpressionActivityEditor.SetExpressionActivityEditor(morphedExpression, editorName);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                succeeded = true;
            }
            return(succeeded);
        }
        // this method may has side effect, it may modify model.
        internal static bool TryInferReturnType(ActivityWithResult expression, EditingContext context, out Type returnType)
        {
            bool succeeded = false;

            returnType = null;
            Type expressionType = expression.GetType();

            if (expressionType.IsGenericType)
            {
                expressionType = expressionType.GetGenericTypeDefinition();
                ExpressionMorphHelperAttribute morphHelperAttribute = ExtensibilityAccessor.GetAttribute <ExpressionMorphHelperAttribute>(expressionType);
                if (morphHelperAttribute != null)
                {
                    ExpressionMorphHelper morphHelper = Activator.CreateInstance(morphHelperAttribute.ExpressionMorphHelperType) as ExpressionMorphHelper;
                    if (morphHelper != null)
                    {
                        succeeded = morphHelper.TryInferReturnType(expression, context, out returnType);
                    }
                }
            }
            return(succeeded);
        }
 // this method may has side effect, it may modify model.
 internal static bool TryInferReturnType(ActivityWithResult expression, EditingContext context, out Type returnType)
 {
     bool succeeded = false;
     returnType = null;
     Type expressionType = expression.GetType();
     if (expressionType.IsGenericType)
     {
         expressionType = expressionType.GetGenericTypeDefinition();
         ExpressionMorphHelperAttribute morphHelperAttribute = ExtensibilityAccessor.GetAttribute<ExpressionMorphHelperAttribute>(expressionType);
         if (morphHelperAttribute != null)
         {
             ExpressionMorphHelper morphHelper = Activator.CreateInstance(morphHelperAttribute.ExpressionMorphHelperType) as ExpressionMorphHelper;
             if (morphHelper != null)
             {
                 succeeded = morphHelper.TryInferReturnType(expression, context, out returnType);
             }
         }
     }
     return succeeded;   
 }
        internal static bool TryMorphExpression(ActivityWithResult originalExpression, bool isLocation, Type targetType, 
            EditingContext context, out ActivityWithResult morphedExpression)
        {
            bool succeeded = false;            
            morphedExpression = null;
            if (originalExpression != null)
            {
                Type resultType = originalExpression.ResultType;
                if ((isLocation) && (ExpressionHelper.IsGenericLocationExpressionType(originalExpression) && (targetType == resultType.GetGenericArguments()[0])) ||
                    (!isLocation) && (resultType == targetType))
                {
                    //no need to morph
                    succeeded = true;
                    morphedExpression = originalExpression;
                }
                else
                {
                    Type expressionType = originalExpression.GetType();
                    if (expressionType.IsGenericType)
                    {
                        expressionType = expressionType.GetGenericTypeDefinition();
                    }

                    ExpressionMorphHelperAttribute morphHelperAttribute = ExtensibilityAccessor.GetAttribute<ExpressionMorphHelperAttribute>(expressionType);
                    if (morphHelperAttribute != null)
                    {
                        ExpressionMorphHelper morphHelper = Activator.CreateInstance(morphHelperAttribute.ExpressionMorphHelperType) as ExpressionMorphHelper;
                        if (morphHelper != null)
                        {
                            succeeded = morphHelper.TryMorphExpression(originalExpression, isLocation, targetType, context, out morphedExpression);
                            if (succeeded && morphedExpression != null)
                            {
                                string editorName = ExpressionActivityEditor.GetExpressionActivityEditor(originalExpression);
                                if (!string.IsNullOrWhiteSpace(editorName))
                                {
                                    ExpressionActivityEditor.SetExpressionActivityEditor(morphedExpression, editorName);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                succeeded = true;
            }
            return succeeded;
        }