MorphHelpers.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Core.Presentation / System / Activities / Core / Presentation / MorphHelpers.cs / 1305376 / MorphHelpers.cs

                            //---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------

namespace System.Activities.Core.Presentation 
{
    using System; 
    using System.Activities.Presentation.Model; 
    using System.Activities.Presentation.Hosting;
    using System.ComponentModel; 
    using Microsoft.VisualBasic.Activities;
    using System.Runtime;
    using System.Linq;
    using System.Activities.Expressions; 
    using System.Activities.Presentation.View;
    using System.Collections.Generic; 
    using System.Activities.ExpressionParser; 
    using System.Diagnostics;
    using System.Globalization; 
    using System.Diagnostics.CodeAnalysis;
    using System.Reflection;
    using System.IO;
    using System.Activities.Presentation; 

    static class MorphHelpers 
    { 

        public static object ArgumentMorphHelper(ModelItem originalValue, ModelProperty newModelProperty) 
        {
            Type expressionTypeArgument = newModelProperty.PropertyType.GetGenericArguments()[0];

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

            if (original.Expression != null)
            { 
                Type expressionType = original.Expression.GetType();
                Type expressionGenericType = expressionType.IsGenericType ? expressionType.GetGenericTypeDefinition() : null; 
 
                if (expressionGenericType != null)
                { 
                    string expressionText = ExpressionHelper.GetExpressionString(original.Expression);
                    ActivityWithResult morphedExpression = ExpressionHelper.CreateExpression(targetType, expressionText,
                        expressionGenericType == typeof(VisualBasicReference<>), null);
                    morphed = Argument.Create(targetType, original.Direction); 
                    morphed.Expression = morphedExpression;
                } 
            } 
            return morphed;
        } 

        public static object ActivityActionMorphHelper(ModelItem originalValue, ModelProperty newModelProperty)
        {
            Fx.Assert(newModelProperty.PropertyType.GetGenericArguments().Count() == 1, "This should only be applied for ActivityAction"); 
            Type activityActionTypeArgument = newModelProperty.PropertyType.GetGenericArguments()[0];
            Type activityActionType = typeof(ActivityAction<>).MakeGenericType(activityActionTypeArgument); 
            object activityAction = Activator.CreateInstance(activityActionType); 
            ModelItem morphed = ModelFactory.CreateItem(originalValue.GetEditingContext(), activityAction);
 
            ModelItem originalActivityActionArgument = originalValue.Properties[PropertyNames.ActionArgument].Value;
            if (originalActivityActionArgument != null)
            {
                Type variableType = typeof(DelegateInArgument<>).MakeGenericType(activityActionTypeArgument); 
                DelegateInArgument iterationDelegateArgument = (DelegateInArgument)Activator.CreateInstance(variableType);
                iterationDelegateArgument.Name = (string)originalActivityActionArgument.Properties[PropertyNames.NameProperty].Value.GetCurrentValue(); 
                morphed.Properties[PropertyNames.ActionArgument].SetValue(iterationDelegateArgument); 
            }
 
            ModelItem originalActivityActionHandler = originalValue.Properties[PropertyNames.ActionHandler].Value;
            if (originalActivityActionHandler != null)
            {
                morphed.Properties[PropertyNames.ActionHandler].SetValue(originalActivityActionHandler); 
                originalValue.Properties[PropertyNames.ActionHandler].SetValue(null);
            } 
 
            return morphed;
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK