FuncTypeConverter.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 / System.Activities / System / Activities / XamlIntegration / FuncTypeConverter.cs / 1305376 / FuncTypeConverter.cs

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

namespace System.Activities.XamlIntegration 
{
    using System; 
    using System.ComponentModel; 
    using System.Globalization;
    using System.Reflection; 
    using System.Runtime;
    using System.Windows.Markup;
    using System.Xaml;
 
    public class FuncDeferringLoader : XamlDeferringLoader
    { 
        public override object Load(XamlReader xamlReader, IServiceProvider context) 
        {
            IXamlObjectWriterFactory objectWriterFactory = context.GetService(typeof(IXamlObjectWriterFactory)) as IXamlObjectWriterFactory; 
            IProvideValueTarget provideValueService = context.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            Type propertyType = null;
            // 
            // IProvideValueTarget.TargetProperty can return DP, Attached Property or MemberInfo for clr property
            // In this case it should always be a regular clr property here - we are always targeting Activity.Body. 
            PropertyInfo propertyInfo = provideValueService.TargetProperty as PropertyInfo; 

            if (propertyInfo != null) 
            {
                propertyType = propertyInfo.PropertyType;
            }
 
            object instance = Activator.CreateInstance(
                typeof(FuncFactory<>).MakeGenericType(propertyType.GetGenericArguments()), 
                objectWriterFactory, 
                xamlReader);
 
            return Delegate.CreateDelegate(propertyType, instance, instance.GetType().GetMethod("Evaluate"));
        }

        public override XamlReader Save(object value, IServiceProvider serviceProvider) 
        {
            throw FxTrace.Exception.AsError(new NotSupportedException(SR.SavingActivityToXamlNotSupported)); 
        } 

        abstract class FuncFactory 
        {
            public XamlNodeList Nodes
            {
                get; 
                set;
            } 
        } 

        class FuncFactory : FuncFactory 
        {
            IXamlObjectWriterFactory objectWriterFactory;

            public FuncFactory(IXamlObjectWriterFactory objectWriterFactory, XamlReader reader) 
            {
                this.objectWriterFactory = objectWriterFactory; 
                this.Nodes = new XamlNodeList(reader.SchemaContext); 
                XamlServices.Transform(reader, this.Nodes.Writer);
            } 

            public T Evaluate()
            {
                XamlObjectWriter writer = this.objectWriterFactory.GetXamlObjectWriter(new XamlObjectWriterSettings()); 
                XamlServices.Transform(this.Nodes.GetReader(), writer);
                return (T)writer.Result; 
            } 
        }
    } 
}



// 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