﻿using NBi.Core.Transformation.Transformer.Native;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.Transformation.Transformer
{
    public class NativeTransformationFactory
    {
        public INativeTransformation Instantiate(string code)
        {
            var textInfo = CultureInfo.InvariantCulture.TextInfo;

            var parameters = code.Replace("(", ",")
                .Replace(")", ",")
                .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .ToList().Skip(1).Select(x => x.Trim()).ToArray();

            var classToken = code.Contains("(") ? code.Replace(" ", "").Substring(0, code.IndexOf('(')) : code;
            var className = textInfo.ToTitleCase(classToken.Trim().Replace("-", " ")).Replace(" ", "").Replace("Datetime", "DateTime");

            var clazz = AppDomain.CurrentDomain.GetAssemblies()
                       .SelectMany(t => t.GetTypes())
                       .Where(
                                t => t.IsClass
                                && t.IsAbstract == false
                                && t.Name == className
                                && t.GetInterface("INativeTransformation") != null)
                       .SingleOrDefault();

            if (clazz == null)
                throw new NotImplementedTransformationException(className);

            return (INativeTransformation)Activator.CreateInstance(clazz, parameters);
        }
    }
}

﻿using NBi.Core.Transformation.Transformer.Native;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.Transformation.Transformer
{
    public class NativeTransformationFactory
    {
        public INativeTransformation Instantiate(string code)
        {
            var textInfo = CultureInfo.InvariantCulture.TextInfo;

            var parameters = code.Replace("(", ",")
                .Replace(")", ",")
                .Replace(" ", "")
                .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .ToList().Skip(1).Select(x => x.Trim()).ToArray();

            var classToken = code.Contains("(") ? code.Replace(" ", "").Substring(0, code.IndexOf('(') - 1) : code;
            var className = textInfo.ToTitleCase(classToken.Trim().Replace("-", " ")).Replace(" ", "").Replace("Datetime", "DateTime");

            var clazz = AppDomain.CurrentDomain.GetAssemblies()
                       .SelectMany(t => t.GetTypes())
                       .Where(
                                t => t.IsClass
                                && t.IsAbstract == false
                                && t.Name == className
                                && t.GetInterface("INativeTransformation") != null)
                       .SingleOrDefault();

            if (clazz == null)
                throw new NotImplementedTransformationException(className);

            return (INativeTransformation)Activator.CreateInstance(clazz, parameters);
        }
    }
}

Fix bugs that native functions with parameters are not instantiable due to parsing mistakes
Fix bugs that native functions with parameters are not instantiable due to parsing mistakes
