<commit_before>﻿namespace NuPattern
{
    using Castle.DynamicProxy;
    using NuPattern.Properties;
    using NuPattern.Proxy;
    using NuPattern.Schema;
    using System;
    using System.Linq;

    internal class SmartCast
    {
        private static readonly ProxyGenerator generator = new ProxyGenerator();

        public object Cast(IComponent component, Type targetType)
        {
            Guard.NotNull(() => component, component);
            Guard.NotNull(() => targetType, targetType);

            if (!targetType.IsInterface)
                throw new ArgumentException(Strings.SmartCast.InterfaceRequired);

            if (component.Schema == null)
                throw new ArgumentException(Strings.SmartCast.SchemaRequired(component));

            var propertyName = "_" + targetType.AssemblyQualifiedName;
            var proxyProperty = component.Properties.FirstOrDefault(p => p.Name == propertyName);
            if (proxyProperty == null)
            {
                var containerSchema = component.Schema as IContainerSchema;
                if (containerSchema != null)
                {
                    if (!IsCompatibleContainer(containerSchema, targetType))
                        return null;
                }
                else if (!IsCompatibleComponent(component.Schema, targetType))
                {
                    return null;
                }

                proxyProperty = component.CreateProperty(propertyName);
                // Try to expose the most specific interface of the received component.
                var componentType = component is IProduct ? typeof(IProduct) :
                    (component is IElement ? typeof(IElement) :
                    (component is ICollection ? typeof(ICollection) : typeof(IComponent)));

                proxyProperty.Value = generator.CreateInterfaceProxyWithTarget(
                    componentType, new[] { targetType }, component, new Interceptor(component, Behaviors.Default));
            }

            return proxyProperty.Value;
        }
<commit_msg>Renamed cached smart cast proxy property to start with $, otherwise it would be serialized.<commit_after>﻿namespace NuPattern
{
    using Castle.DynamicProxy;
    using NuPattern.Properties;
    using NuPattern.Proxy;
    using NuPattern.Schema;
    using System;
    using System.Linq;

    internal class SmartCast
    {
        private static readonly ProxyGenerator generator = new ProxyGenerator();

        public object Cast(IComponent component, Type targetType)
        {
            Guard.NotNull(() => component, component);
            Guard.NotNull(() => targetType, targetType);

            if (!targetType.IsInterface)
                throw new ArgumentException(Strings.SmartCast.InterfaceRequired);

            if (component.Schema == null)
                throw new ArgumentException(Strings.SmartCast.SchemaRequired(component));

            var propertyName = "$" + targetType.AssemblyQualifiedName;
            // NOTE: we cache the proxy instance for performance.
            var proxyProperty = component.Properties.FirstOrDefault(p => p.Name == propertyName);
            if (proxyProperty == null)
            {
                var containerSchema = component.Schema as IContainerSchema;
                if (containerSchema != null)
                {
                    if (!IsCompatibleContainer(containerSchema, targetType))
                        return null;
                }
                else if (!IsCompatibleComponent(component.Schema, targetType))
                {
                    return null;
                }

                proxyProperty = component.CreateProperty(propertyName);
                // Try to expose the most specific interface of the received component.
                var componentType = component is IProduct ? typeof(IProduct) :
                    (component is IElement ? typeof(IElement) :
                    (component is ICollection ? typeof(ICollection) : typeof(IComponent)));

                proxyProperty.Value = generator.CreateInterfaceProxyWithTarget(
                    componentType, new[] { targetType }, component, new Interceptor(component, Behaviors.Default));
            }

            return proxyProperty.Value;
        }

{'fraction_non_alphanumeric': 0.07695895522388059, 'fraction_numerical': 0.0, 'mean_word_length': 1.9702216066481995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7273400', 'n_tokens_mistral': 962, 'n_tokens_neox': 910, 'n_words': 253}