示例#1
0
        /// <summary>
        /// Is overridden to define how the type reference is translated by this strategy.
        /// </summary>
        /// <param name="referencedType">Type reference that should be tranlated.</param>
        /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
        /// <returns>Result of the translation.</returns>
        protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
        {
            var map = _configuration.CustomTypeMaps.FirstOrDefault(m => m.MapsType(referencedType));

            // Support for custom maps of generic types.
            // We do not delegate to the GenericTypeTranslationStrategy here to avoid any coupling between those two.
            // Dealing with generic types in mapped types looks similar, but is a different concern.
            if (referencedType.IsGenericType)
            {
                var translatedTypeArguments = referencedType.GetGenericArguments().Select(translator.Translate);
                var referencedTypeName      = $"{map.Name}<{ string.Join(", ", translatedTypeArguments.Select(x => x.ReferencedTypeName)) }>";

                return(new TypeReferenceTranslationResult(referencedTypeName,
                                                          map.CreateImportDependency()
                                                          .Merge(translatedTypeArguments.Aggregate(CodeDependencies.Empty, (x, n) => x.Merge(n.Dependencies)))));
            }

            // Non-generic: Just translate with the name defined in the map.
            return(new TypeReferenceTranslationResult(map.Name, map.CreateImportDependency()));
        }
        /// <summary>
        /// Is overridden to define how the type reference is translated by this strategy.
        /// </summary>
        /// <param name="referencedType">Type reference that should be tranlated.</param>
        /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
        /// <returns>Result of the translation.</returns>
        protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
        {
            var translatedTypeArguments = referencedType.GetGenericArguments().Select(translator.Translate);
            var referencedTypeName      = referencedType.GetNameWithoutGenericTypeParameters()
                                          + $"<{ string.Join(", ", translatedTypeArguments.Select(x => x.ReferencedTypeName)) }>";

            return(new TypeReferenceTranslationResult(referencedTypeName,
                                                      CodeDependencies.FromCodeFragments(new[] { CodeFragmentId.ForClrType(referencedType.GetGenericTypeDefinition()) })
                                                      .Merge(translatedTypeArguments.Aggregate(CodeDependencies.Empty, (x, n) => x.Merge(n.Dependencies)))));
        }
示例#3
0
 /// <summary>
 /// Creates a <see cref="CustomMappedTranslationStrategy"/>.
 /// </summary>
 /// <param name="configurationSource">Source for the configuration that should be used.</param>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 public CustomMappedTranslationStrategy(IConfigurationSource configurationSource, ITypeReferenceTranslator translator)
     : base(translator)
 {
     _configuration = configurationSource.GetSection <TranspilationConfiguration>()
                      ?? TranspilationConfiguration.Default;
 }
 /// <summary>
 /// Is overridden to define how the type reference is translated by this strategy.
 /// </summary>
 /// <param name="referencedType">Type reference that should be tranlated.</param>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 /// <returns>Result of the translation.</returns>
 protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
 => translator.Translate(Nullable.GetUnderlyingType(referencedType));
 /// <summary>
 /// Creates a <see cref="GenericTypeTranslationStrategy"/>.
 /// </summary>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 public GenericTypeTranslationStrategy(ITypeReferenceTranslator translator)
     : base(translator)
 {
 }
 /// <summary>
 /// Is overridden to define how the type reference is translated by this strategy.
 /// </summary>
 /// <param name="referencedType">Type reference that should be tranlated.</param>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 /// <returns>Result of the translation.</returns>
 protected abstract TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator);
 /// <summary>
 /// Creates a <see cref="BuiltInTypeTranslationStrategy"/>.
 /// </summary>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 public NullableTypeTranslationStrategy(ITypeReferenceTranslator translator)
     : base(translator)
 {
 }
示例#8
0
        private string GetDefaultConstructorCall(Type type, TranspilationConfiguration configuration, ITypeReferenceTranslator typeReferenceTranslator)
        {
            var unconstructables = new[] { typeof(string), typeof(object) };

            if (configuration.CustomTypeMaps.Any(map => map.MapsType(type)) ||
                !type.IsClass ||
                unconstructables.Contains(type) ||
                type.IsGenericParameter)
            {
                return(_defaultValueAssignments[DefaultValueStrategy.PrimitiveDefaults](type));
            }

            return($" = new {typeReferenceTranslator.Translate(type).ReferencedTypeName}()");
        }
 /// <summary>
 /// Creates a <see cref="TranslationStrategyBase"/>.
 /// </summary>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 protected TranslationStrategyBase(ITypeReferenceTranslator translator)
 {
     _translator = translator ?? throw new ArgumentNullException(nameof(translator));
 }
 /// <summary>
 /// Is overridden to define how the type reference is translated by this strategy.
 /// </summary>
 /// <param name="referencedType">Type reference that should be tranlated.</param>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 /// <returns>Result of the translation.</returns>
 protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
 => new TypeReferenceTranslationResult(BuiltInTypes[referencedType], CodeDependencies.Empty);
示例#11
0
        /// <summary>
        /// Creates a DefaultValueProvider.
        /// </summary>
        /// <param name="configurationSource">Configuration that the provider should respect.</param>
        /// <param name="logger">Logger to use for writing log messages.</param>
        public DefaultValueProvider(IConfigurationSource configurationSource, ILogger logger, ITypeReferenceTranslator typeReferenceTranslator)
        {
            if (configurationSource is null)
            {
                throw new ArgumentNullException(nameof(configurationSource));
            }

            _logger        = logger ?? throw new ArgumentNullException(nameof(logger));
            _configuration = configurationSource.GetSection <TranspilationConfiguration>()
                             ?? TranspilationConfiguration.Default;

            _defaultValueAssignments.Add(DefaultValueStrategy.DefaultConstructor,
                                         type => GetDefaultConstructorCall(type, _configuration, typeReferenceTranslator));
        }
 /// <summary>
 /// Creates a <see cref="BuiltInTypeTranslationStrategy"/>.
 /// </summary>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 public BuiltInTypeTranslationStrategy(ITypeReferenceTranslator translator)
     : base(translator)
 {
 }
        /// <summary>
        /// Is overridden to define how the type reference is translated by this strategy.
        /// </summary>
        /// <param name="referencedType">Type reference that should be tranlated.</param>
        /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
        /// <returns>Result of the translation.</returns>
        protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
        {
            var elementType       = referencedType.GetCollectionElementType();
            var translatedElement = translator.Translate(elementType);

            return(new TypeReferenceTranslationResult(
                       $"Array<{ translatedElement.ReferencedTypeName }>",
                       translatedElement.Dependencies));
        }
 /// <summary>
 /// Creates a <see cref="CollectionTypeTranslationStrategy"/>.
 /// </summary>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 public CollectionTypeTranslationStrategy(ITypeReferenceTranslator translator)
     : base(translator)
 {
 }
        /// <summary>
        /// Is overridden to define how the type reference is translated by this strategy.
        /// </summary>
        /// <param name="referencedType">Type reference that should be tranlated.</param>
        /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
        /// <returns>Result of the translation.</returns>
        protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
        {
            var dictionaryType      = GetDictionaryType(referencedType);
            var translatedKeyType   = translator.Translate(dictionaryType.GetGenericArguments()[0]);
            var translatedValueType = translator.Translate(dictionaryType.GetGenericArguments()[1]);

            return(new TypeReferenceTranslationResult(
                       $"{{[key: { translatedKeyType.ReferencedTypeName }]: { translatedValueType.ReferencedTypeName } }}",
                       translatedKeyType.Dependencies.Merge(translatedValueType.Dependencies)));
        }
 /// <summary>
 /// Creates a <see cref="DictionaryTypeTranslationStrategy"/>.
 /// </summary>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 public DictionaryTypeTranslationStrategy(ITypeReferenceTranslator translator)
     : base(translator)
 {
 }