Exemplo n.º 1
0
    private static object TryGenerateCreate(Type protocol, Actor actor, IMailbox mailbox, ProxyGenerator generator, string targetClassName, string lookupTypeName)
    {
        try
        {
            var originalProtocol = protocol;

            if (protocol.IsGenericType && !protocol.IsGenericTypeDefinition)
            {
                protocol = protocol.GetGenericTypeDefinition();
            }

            // Allows to determine if async/await keyword was used
            var asyncAwaitedMethods = actor.GetType().GetTypeInfo().ImplementedInterfaces
                                      .Select(ii => actor.GetType().GetInterfaceMap(ii))
                                      .Where(im => im.InterfaceType == originalProtocol)
                                      .SelectMany(im => im.TargetMethods)
                                      .Where(IsAsyncStateMachine)
                                      .ToArray();
            var result = generator.GenerateFor(protocol, asyncAwaitedMethods);
            var input  = new Input(
                protocol,
                targetClassName,
                lookupTypeName,
                result.Source,
                result.SourceFile,
                ClassLoaderFor(actor),
                generator.Type,
                true);

            var proxyCompiler = new DynaCompiler();
            var proxyClass    = proxyCompiler.Compile(input);
            if (proxyClass != null && proxyClass.IsGenericTypeDefinition)
            {
                proxyClass = proxyClass.MakeGenericType(originalProtocol.GetGenericArguments());
            }

            return(TryCreateWithProxyClass(proxyClass !, actor, mailbox));
        }
        catch (Exception e)
        {
            throw new ArgumentException($"Actor proxy {protocol.Name} not created because: {e.Message}", e);
        }
    }