Load() public static method

Loads an assembly given a string, which is the AssemblyName
public static Load ( string name ) : Assembly
name string
return System.Reflection.Assembly
コード例 #1
0
        /// <summary>
        /// Returns an array of custom attributes of the specified type applied to this method
        /// </summary>
        public T[] GetCustomAttributes <T>(bool inherit) where T : class
        {
#if PORTABLE
            //return MethodInfo.GetAttributes<T>(inherit).ToArray();
            var           allAttributes = MethodInfo.GetCustomAttributes();
            List <string> attributeDic  = new List <string>();
            foreach (Attribute atb in allAttributes)
            {
                attributeDic.Add(atb.GetType().FullName);
            }
            var      assembly = MethodInfo.DeclaringType.GetTypeInfo().Assembly;
            List <T> objects  = new List <T>();

            string path = System.IO.Path.GetDirectoryName(assembly.Location);

            if (!Directory.Exists(path))
            {
                TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
                return(objects.ToArray());
            }
            foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
            {
                IEnumerable <Type> types;
                try
                {
                    Assembly please = AssemblyHelper.Load(assemblyPath);
                    if (please == null)
                    {
                        continue;
                    }
                    types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract&& p.GetTypeInfo().IsClass&& p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
                }
                catch (Exception)
                {
                    continue;
                }
                for (int i = 0; i < types.Count(); i++)
                {
                    try
                    {
                        if (attributeDic.Contains(types.ElementAt(i).FullName))
                        {
                            objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
                        }
                    }
                    catch (Exception)
                    {
                        //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
                    }
                }
            }

            return(objects.ToArray());
#else
            return((T[])MethodInfo.GetCustomAttributes(typeof(T), inherit));
#endif
        }
コード例 #2
0
        public void CopyTestAssemblyReturnsCorrectType()
        {
            var       runner   = new NUnitTestAssemblyRunner(new DefaultTestAssemblyBuilder());
            TestSuite assembly =
                new TestAssembly(
                    AssemblyHelper.Load(Path.Combine(TestContext.CurrentContext.TestDirectory, "mock-assembly.dll")),
                    "mock-assembly");
            var copiedAssembly = assembly.Copy(TestFilter.Empty);

            Assert.That(copiedAssembly, Is.TypeOf <TestAssembly>());
        }