Example #1
0
        internal static Actor ActorFor(
            Stage stage,
            Actor?parent,
            Definition definition,
            IAddress address,
            IMailbox mailbox,
            ISupervisor?supervisor,
            ILogger logger)
        {
            var environment = new Environment(
                stage,
                address,
                definition,
                parent,
                mailbox,
                supervisor,
                ActorLoggerAdapter.From(logger, address, definition.Type !));

            ThreadLocalEnvironment.Value = environment;

            Actor?actor = null;

            var definitionParameterCount = definition.InternalParameters().Count();

            if (definitionParameterCount == 0)
            {
                actor = (Actor)Activator.CreateInstance(definition.Type);
                actor.LifeCycle.SendStart(actor);
            }
            else
            {
                foreach (var ctor in definition.Type !.GetConstructors())
                {
                    if (ctor.GetParameters().Length != definitionParameterCount)
                    {
                        continue;
                    }

                    actor = Start(ctor, address, definition, logger);

                    if (actor != null)
                    {
                        break;
                    }
                }
            }

            if (actor == null)
            {
                throw new MissingMethodException("No constructor matches the given number of parameters.");
            }

            if (parent != null)
            {
                parent.LifeCycle.Environment.AddChild(actor);
            }

            return(actor);
        }
        internal static Actor ActorFor(
            Stage stage,
            Actor?parent,
            Definition definition,
            IAddress address,
            IMailbox mailbox,
            ISupervisor?supervisor,
            ILogger logger)
        {
            var environment = new Environment(
                stage,
                address,
                definition,
                parent,
                mailbox,
                supervisor,
                ActorLoggerAdapter.From(logger, address, definition.Type !));

            ThreadLocalEnvironment.Value = environment;

            Actor?actor = null;

            var definitionParameterCount = definition.InternalParameters().Count();

            if (definitionParameterCount == 0)
            {
                if (definition.Type != null)
                {
                    actor = (Actor)Activator.CreateInstance(definition.Type) !;
                    actor.LifeCycle.SendStart(actor);
                }
            }
            else
            {
                foreach (var ctor in definition.Type !.GetConstructors())
                {
                    var expectedConstructorParameters      = definition.InternalParameters().ToList();
                    var actualParametersInfosOfConstructor = ctor.GetParameters();
                    if (actualParametersInfosOfConstructor.Length != definitionParameterCount)
                    {
                        continue;
                    }

                    var differentParameterType = false;
                    for (var i = 0; i < actualParametersInfosOfConstructor.Length; i++)
                    {
                        if (actualParametersInfosOfConstructor[i].ParameterType != expectedConstructorParameters[i].GetType() &&
                            !actualParametersInfosOfConstructor[i].ParameterType.IsAssignableFrom(expectedConstructorParameters[i].GetType()))
                        {
                            differentParameterType = true;
                            break;
                        }
                    }

                    if (differentParameterType)
                    {
                        continue;
                    }

                    actor = Start(ctor, address, definition, logger);

                    if (actor != null)
                    {
                        break;
                    }
                }
            }

            if (actor == null)
            {
                throw new MissingMethodException("No constructor matches the given number of parameters.");
            }

            if (parent != null)
            {
                parent.LifeCycle.Environment.AddChild(actor);
            }

            return(actor);
        }