Example #1
0
        private bool TryLoadTypes(string filePath, int lineNumber, string assemblyNameAndNamespaceReference, IRuntime runtime, out Type[] types)
        {
            if (string.IsNullOrWhiteSpace(assemblyNameAndNamespaceReference))
            {
                types = new Type[0];
                return(true);
            }

            try
            {
                runtime.Log("Parsing " + assemblyNameAndNamespaceReference);
                var parts = Regex.Split(assemblyNameAndNamespaceReference, "::");
                if (parts.Length != 2)
                {
                    var error = string.Format("Unrecognised format: use AssemblyName::Namespace) in '{0}'", assemblyNameAndNamespaceReference);
                    runtime.ErrorInFile(filePath, lineNumber, error);
                    System.Environment.Exit(-1);
                }

                var assemblyName  = parts[0];
                var namespaceName = parts[1];
                var assembly      = assemblyLoader.Load(assemblyName);

                runtime.Log(string.Format("Generating from assembly {0}, namespace {1}", assemblyName, namespaceName));

                types = assembly
                        .GetTypes()
                        .Where(TypeExtensions.IsExportableType)
                        .Where(t => t.Namespace.StartsWith(namespaceName))
                        .ToArray();

                runtime.Log("Loaded " + types.Length.ToString() + " types");

                return(true);
            }
            catch (ReflectionTypeLoadException ex)
            {
                runtime.Error(ex.ToString());
                foreach (var loaderException in ex.LoaderExceptions)
                {
                    runtime.Error(loaderException.ToString());
                }

                types = new Type[0];
                return(false);
            }
            catch (Exception ex)
            {
                runtime.Error(ex.ToString());
                types = new Type[0];
                return(false);
            }
        }
Example #2
0
        private bool TryLoadTypes(string filePath, int lineNumber, string assemblyNameAndNamespaceReference, IRuntime runtime, out Type[] types)
        {
            if (string.IsNullOrWhiteSpace(assemblyNameAndNamespaceReference))
            {
                types = new Type[0];
                return true;
            }

            try
            {
                runtime.Log("Parsing " + assemblyNameAndNamespaceReference);
                var parts = Regex.Split(assemblyNameAndNamespaceReference, "::");
                if (parts.Length != 2)
                {
                    var error = string.Format("Unrecognised format: use AssemblyName::Namespace) in '{0}'", assemblyNameAndNamespaceReference);
                    runtime.ErrorInFile(filePath, lineNumber, error);
                    System.Environment.Exit(-1);
                }

                var assemblyName = parts[0];
                var namespaceName = parts[1];
                var assembly = assemblyLoader.Load(assemblyName);

                runtime.Log(string.Format("Generating from assembly {0}, namespace {1}", assemblyName, namespaceName ));

                types = assembly
                    .GetTypes()
                    .Where(TypeExtensions.IsExportableType)
                    .Where(t => t.Namespace.StartsWith(namespaceName))
                    .ToArray();

                runtime.Log("Loaded " + types.Length.ToString() + " types");

                return true;
            }
            catch (ReflectionTypeLoadException ex)
            {
                runtime.Error(ex.ToString());
                foreach (var loaderException in ex.LoaderExceptions)
                {
                    runtime.Error(loaderException.ToString());
                }

                types = new Type[0];
                return false;
            }
            catch (Exception ex)
            {
                runtime.Error(ex.ToString());
                types = new Type[0];
                return false;
            }
        }