Example #1
0
        public static void CheckAssembly()
        {
            try
            {
                var path = (string)AppDomain.CurrentDomain.GetData(_assemblyKey);

                var loader = new PluginsAndWorkflowLoader();

                AssemblyReaderResult result = loader.LoadAssembly(path);

                byte[] fileBody = null;

                DataContractSerializer ser = new DataContractSerializer(typeof(AssemblyReaderResult));

                using (var memoryStream = new MemoryStream())
                {
                    ser.WriteObject(memoryStream, result);

                    memoryStream.Seek(0, SeekOrigin.Begin);

                    fileBody = memoryStream.ToArray();
                }

                AppDomain.CurrentDomain.SetData(_resultKey, fileBody);
            }
            catch (Exception ex)
            {
                DTEHelper.WriteExceptionToLog(ex);
            }
        }
Example #2
0
        internal AssemblyReaderResult ReadAssembly(string assemblyPath)
        {
            try
            {
                _domain.SetData(_assemblyKey, assemblyPath);
                _domain.DoCallBack(new CrossAppDomainDelegate(CheckAssembly));

                var check = _domain.GetData(_resultKey);

                if (check != null &&
                    check is byte[] arraySerialized &&
                    arraySerialized.Length > 0
                    )
                {
                    AssemblyReaderResult result = null;

                    DataContractSerializer ser = new DataContractSerializer(typeof(AssemblyReaderResult));

                    using (var memoryStream = new MemoryStream())
                    {
                        memoryStream.Write(arraySerialized, 0, arraySerialized.Length);

                        memoryStream.Seek(0, SeekOrigin.Begin);

                        result = ser.ReadObject(memoryStream) as AssemblyReaderResult;
                    }

                    return(result);
                }
            }
            catch (Exception ex)
            {
                DTEHelper.WriteExceptionToOutput(null, ex);
            }

            return(null);
        }
Example #3
0
        public AssemblyReaderResult LoadAssembly(string assemblyPath)
        {
            this._assemblyDirectory = Path.GetDirectoryName(assemblyPath);

            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= Domain_ReflectionOnlyAssemblyResolve;
            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= Domain_ReflectionOnlyAssemblyResolve;
            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += Domain_ReflectionOnlyAssemblyResolve;

            var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);

            var assemblyName = assembly.GetName();

            AssemblyReaderResult result = new AssemblyReaderResult()
            {
                FilePath = assemblyPath,
                FileName = Path.GetFileName(assemblyPath),

                Name = assemblyName.Name,
                FullName = assemblyName.FullName,

                Version = assemblyName.Version.ToString(),
            };

            if (assemblyName.CultureInfo.LCID == System.Globalization.CultureInfo.InvariantCulture.LCID)
            {
                result.Culture = "neutral";
            }
            else
            {
                result.Culture = assemblyName.CultureInfo.Name;
            }

            byte[] tokenBytes = assemblyName.GetPublicKeyToken();
            if (tokenBytes == null || tokenBytes.Length == 0)
            {
                result.PublicKeyToken = null;
            }
            else
            {
                result.PublicKeyToken = string.Join(string.Empty, tokenBytes.Select(b => b.ToString("X2")));
            }

            HashSet<string> assemblyPlugins = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
            HashSet<string> assemblyWorkflow = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

            Type[] loadedAssemblyTypes = assembly.GetTypes();

            if (loadedAssemblyTypes != null)
            {
                foreach (var assemblyType in loadedAssemblyTypes)
                {
                    if (assemblyType.IsAbstract || !assemblyType.IsVisible || assemblyType.FullName.Contains("<>c"))
                    {
                        continue;
                    }

                    if (IsPluginClass(assemblyType))
                    {
                        assemblyPlugins.Add(assemblyType.FullName);
                    }
                    else if (IsSubClassOfCodeActivity(assemblyType))
                    {
                        assemblyWorkflow.Add(assemblyType.FullName);
                    }
                }
            }

            result.Plugins = new List<string>(assemblyPlugins.OrderBy(s => s));
            result.Workflows = new List<string>(assemblyWorkflow.OrderBy(s => s));

            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= Domain_ReflectionOnlyAssemblyResolve;
            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= Domain_ReflectionOnlyAssemblyResolve;

            return result;
        }