Beispiel #1
0
        private static void CreateDelegateWrapperMethod(DelegateToTypeWrapperOptions options, Type returnType, StringBuilder code, List <string> methodParameterNamesWithTypes,
                                                        Guid id, List <string> delegateMethodParameters)
        {
            if (typeof(void) != returnType)
            {
                code.AppendLine(
                    $"public {GetFriendlyName(returnType)} {GetMethodName(options)} ({string.Join(", ", methodParameterNamesWithTypes)})");
            }
            else
            {
                code.AppendLine(
                    $"public void Run ({string.Join(", ", methodParameterNamesWithTypes)})");
            }

            code.AppendLine("{");
            code.AppendLine($"var deleg = Weikio.TypeGenerator.Delegates.DelegateCache.Get(System.Guid.Parse(\"{id.ToString()}\"));");

            if (typeof(void) != returnType)
            {
                code.AppendLine(
                    $"return ({GetFriendlyName(returnType)}) deleg.DynamicInvoke({string.Join(", ", delegateMethodParameters)});");
            }
            else
            {
                code.AppendLine(
                    $"deleg.DynamicInvoke({string.Join(", ", delegateMethodParameters)});");
            }

            code.AppendLine("}"); // Close method
        }
Beispiel #2
0
        private static string GetTypeName(DelegateToTypeWrapperOptions options)
        {
            if (options?.TypeNameGenerator != null)
            {
                return(options.TypeNameGenerator(options));
            }

            return("GeneratedType");
        }
Beispiel #3
0
        private static string GetMethodName(DelegateToTypeWrapperOptions options)
        {
            if (options?.MethodNameGenerator != null)
            {
                return(options.MethodNameGenerator(options));
            }

            return("Run");
        }
Beispiel #4
0
        private static void CreateConstructor(DelegateToTypeWrapperOptions options, List <string> constructorParameterNames, List <string> constructorFielsNamesWithTypes,
                                              StringBuilder code, List <string> constructorParameterNamesWithTypes)
        {
            if (constructorParameterNames?.Any() == true)
            {
                foreach (var fieldNameWithType in constructorFielsNamesWithTypes)
                {
                    code.AppendLine($"private {fieldNameWithType};");
                }

                code.AppendLine($"public {GetTypeName(options)}({string.Join(", ", constructorParameterNamesWithTypes)})");
                code.AppendLine("{");

                foreach (var constructorParameterName in constructorParameterNames)
                {
                    code.AppendLine($"_{constructorParameterName} = {constructorParameterName};");
                }

                code.AppendLine("}"); // Close constructor
            }
        }
Beispiel #5
0
        public Type CreateType(MulticastDelegate multicastDelegate,
                               DelegateToTypeWrapperOptions options = default)
        {
            var methodInfo = multicastDelegate.GetMethodInfo();

            if (methodInfo == null)
            {
                throw new Exception("Couldn't get method info from delegate");
            }

            var parameters = methodInfo.GetParameters();
            var returnType = methodInfo.ReturnType;

            var id = DelegateCache.Add(multicastDelegate);

            var allTypes = GetRequiredTypes(parameters, returnType);

            var generator = new CodeToAssemblyGenerator();

            AddReferences(generator, allTypes);

            var code = new StringBuilder();

            AddNamespaces(code, allTypes);

            var constructorParameterNames          = new List <string>();
            var methodParameterNamesWithTypes      = new List <string>();
            var constructorParameterNamesWithTypes = new List <string>();
            var constructorFielsNamesWithTypes     = new List <string>();
            var propertyNamesWithTypes             = new List <string>();
            var delegateMethodParameters           = new List <string>();

            var conversionRules = options?.ConversionRules;

            if (conversionRules == null)
            {
                conversionRules = new List <ParameterConversionRule>();
            }

            DoConversions(parameters, conversionRules, constructorParameterNames, constructorParameterNamesWithTypes, constructorFielsNamesWithTypes, delegateMethodParameters, propertyNamesWithTypes, methodParameterNamesWithTypes);

            code.AppendLine();
            code.AppendLine($"namespace {GetNamespace(options)}");
            code.AppendLine("{");
            code.AppendLine($"public class {GetTypeName(options)}");
            code.AppendLine("{");

            CreateConstructor(options, constructorParameterNames, constructorFielsNamesWithTypes, code, constructorParameterNamesWithTypes);
            CreateProperties(propertyNamesWithTypes, code);
            CreateDelegateWrapperMethod(options, returnType, code, methodParameterNamesWithTypes, id, delegateMethodParameters);

            code.AppendLine("}"); // Close class
            code.AppendLine("}"); // Close namespace

            var s        = code.ToString();
            var assembly = generator.GenerateAssembly(s);

            var result = assembly.GetExportedTypes().Single();

            return(result);
        }