public RobotServiceClassManager(ServiceClassDefinition classDefinition, string appPath)
 {
     _executionType   = ServiceClassExecutionType.ByDefinition;
     _classDefinition = classDefinition;
     _appPath         = appPath;
     _enforceDefined  = false;
 }
 private void OnComplete(ServiceClassDefinition def)
 {
     if (_complete != null)
     {
         _complete(def);
     }
 }
        public IRobotServiceClass GetServiceClassInstance(ServiceClassDefinition classDefinition, string appPath)
        {
            IRobotServiceClass processClass = null;

            OnStep(String.Format(Resources.Class_InitializingProcessment, classDefinition.TypeFullName));
            try
            {
                // Pega o caminho do assembly
                OnStep(String.Format(Resources.Assembly_Locating, classDefinition.AssemblyName));

                string assemblyPath = null;
                if (File.Exists(classDefinition.AssemblyName))
                {
                    assemblyPath = classDefinition.AssemblyName;
                }
                else if (File.Exists(Path.Combine(appPath, classDefinition.AssemblyName)))
                {
                    assemblyPath = Path.Combine(appPath, classDefinition.AssemblyName);
                }

                // Tenta carregar o assembly do caminho encontrado ou do GAC
                Assembly assembly;
                try
                {
                    if (!String.IsNullOrEmpty(assemblyPath))
                    {
                        OnStep(String.Format(Resources.Assembly_Loading, assemblyPath));
                        assembly = Assembly.LoadFrom(assemblyPath);
                    }
                    else
                    {
                        OnStep(String.Format(Resources.Assembly_Loading, classDefinition.AssemblyName));
                        assembly = Assembly.ReflectionOnlyLoadFrom(classDefinition.AssemblyName);
                    }
                }
                catch
                {
                    assembly = null;
                }

                // Verifica se o assembly foi carregado
                if (assembly != null)
                {
                    OnStep(String.Format(Resources.Assembly_Loaded, classDefinition.AssemblyName));

                    // Tenta carregar a classe configurada
                    var type = assembly.GetType(classDefinition.TypeFullName);

                    // Verifica se a classe foi encontrada
                    OnStep(String.Format(Resources.Assembly_VerifyingClassExists, classDefinition.AssemblyName, classDefinition.TypeFullName));
                    if (type != null)
                    {
                        // Verifica se a classe implementa a interface IRobotServiceClass
                        OnStep(String.Format(Resources.Class_VerifyingInterfaceImplementation, classDefinition.TypeFullName));

                        var classInterfaceType  = typeof(IRobotServiceClass);
                        var interfaces          = type.GetInterfaces();
                        var implementsInfertace = interfaces.Any(interfaceType => interfaceType.FullName == classInterfaceType.FullName);

                        if (implementsInfertace)
                        {
                            //Instancia a classe
                            OnStep(String.Format(Resources.Class_Instantiation, classDefinition.TypeFullName));
                            processClass = (IRobotServiceClass)assembly.CreateInstance(type.FullName);
                            OnStep(String.Format(Resources.Class_Instantiated, classDefinition.TypeFullName));
                        }
                        else
                        {
                            throw new NotImplementedException(String.Format(Resources.Class_NotImplementErrorMessage, classDefinition.TypeFullName, classInterfaceType.FullName));
                        }
                    }
                    else
                    {
                        throw new Exception(String.Format(Resources.Assembly_TypeNotFoundErrorMessage, classDefinition.AssemblyName, classDefinition.TypeFullName));
                    }
                }
                else
                {
                    throw new FileNotFoundException(String.Format(Resources.Assembly_AssemblyNotFoundErrorMessage, classDefinition.AssemblyName));
                }

                return(processClass);
            }
            catch (Exception exp)
            {
                OnError(exp);
            }

            return(processClass);
        }