/// <summary>
        /// Returns a <see cref="System.Type"/> from an already loaded Assembly or an
        /// Assembly that is loaded with a partial name.
        /// </summary>
        /// <param name="name">An <see cref="AssemblyQualifiedTypeName" />.</param>
        /// <param name="throwOnError"><see langword="true" /> if an exception should be thrown
        /// in case of an error, <see langword="false" /> otherwise.</param>
        /// <returns>
        /// A <see cref="System.Type"/> object that represents the specified type,
        /// or <see langword="null" /> if the type cannot be loaded.
        /// </returns>
        /// <remarks>
        /// Attempts to get a reference to the type from an already loaded assembly.  If the
        /// type cannot be found then the assembly is loaded using
        /// <see cref="Assembly.Load(string)" />.
        /// </remarks>
        public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, bool throwOnError)
        {
            try
            {
                // Try to get the type from an already loaded assembly
                System.Type type = System.Type.GetType(name.ToString());

                if (type != null)
                {
                    return(type);
                }

                if (name.Assembly == null)
                {
                    // No assembly was specified for the type, so just fail
                    string message = "Could not load type " + name + ". Possible cause: no assembly name specified.";
                    log.Warn(message);
                    if (throwOnError)
                    {
                        throw new TypeLoadException(message);
                    }
                    return(null);
                }

                Assembly assembly = Assembly.Load(name.Assembly);

                if (assembly == null)
                {
                    log.Warn("Could not load type " + name + ". Possible cause: incorrect assembly name specified.");
                    return(null);
                }

                type = assembly.GetType(name.Type, throwOnError);

                if (type == null)
                {
                    log.Warn("Could not load type " + name + ".");
                    return(null);
                }

                return(type);
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("Could not load type " + name + ".", e);
                }
                if (throwOnError)
                {
                    throw;
                }
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Returns a <see cref="System.Type"/> from an already loaded Assembly or an
        /// Assembly that is loaded with a partial name.
        /// </summary>
        /// <param name="name">An <see cref="AssemblyQualifiedTypeName" />.</param>
        /// <param name="throwOnError"><see langword="true" /> if an exception should be thrown
        /// in case of an error, <see langword="false" /> otherwise.</param>
        /// <returns>
        /// A <see cref="System.Type"/> object that represents the specified type,
        /// or <see langword="null" /> if the type cannot be loaded.
        /// </returns>
        /// <remarks>
        /// Attempts to get a reference to the type from an already loaded assembly.  If the 
        /// type cannot be found then the assembly is loaded using
        /// <see cref="Assembly.Load(string)" />.
        /// </remarks>
        public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, bool throwOnError)
        {
            try
            {
                // Try to get the type from an already loaded assembly
                System.Type type = System.Type.GetType(name.ToString());

                if (type != null)
                {
                    return type;
                }

                if (name.Assembly == null)
                {
                    // No assembly was specified for the type, so just fail
                    string message = "Could not load type " + name + ". Possible cause: no assembly name specified.";
                    log.Warn(message);
                    if (throwOnError) throw new TypeLoadException(message);
                    return null;
                }

                Assembly assembly = Assembly.Load(name.Assembly);

                if (assembly == null)
                {
                    log.Warn("Could not load type " + name + ". Possible cause: incorrect assembly name specified.");
                    return null;
                }

                type = assembly.GetType(name.Type, throwOnError);

                if (type == null)
                {
                    log.Warn("Could not load type " + name + ".");
                    return null;
                }

                return type;
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("Could not load type " + name + ".", e);
                }
                if (throwOnError) throw;
                return null;
            }
        }
예제 #3
0
        /// <summary>
        /// Returns a <see cref="System.Type"/> from an already loaded Assembly or an
        /// Assembly that is loaded with a partial name.
        /// </summary>
        /// <param name="name">An <see cref="AssemblyQualifiedTypeName" />.</param>
        /// <param name="throwOnError"><see langword="true" /> if an exception should be thrown
        /// in case of an error, <see langword="false" /> otherwise.</param>
        /// <returns>
        /// A <see cref="System.Type"/> object that represents the specified type,
        /// or <see langword="null" /> if the type cannot be loaded.
        /// </returns>
        /// <remarks>
        /// Attempts to get a reference to the type from an already loaded assembly.  If the
        /// type cannot be found then the assembly is loaded using
        /// <see cref="Assembly.Load(string)" />.
        /// </remarks>
        public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, bool throwOnError)
        {
            try
            {
                // Try to get the type from an already loaded assembly
                System.Type type = System.Type.GetType(name.ToString());
                if (type != null)
                {
                    return(type);
                }

                if (name.Assembly == null)
                {
                    // No assembly was specified for the type, so just fail
                    const string noAssembly = "Could not load type {0}. Possible cause: no assembly name specified.";
                    log.Warn(noAssembly, name);
                    if (throwOnError)
                    {
                        throw new TypeLoadException(string.Format(noAssembly, name));
                    }
                    return(null);
                }

                //Load type from already loaded assembly
                type = System.Type.GetType(
                    name.ToString(),
                    an => AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == an.FullName),
                    null);
                if (type != null)
                {
                    return(type);
                }

                Assembly assembly = Assembly.Load(name.Assembly);

                if (assembly == null)
                {
                    log.Warn("Could not load type {0}. Possible cause: incorrect assembly name specified.", name);
                    return(null);
                }

                type = assembly.GetType(name.Type, throwOnError);

                if (type == null)
                {
                    log.Warn("Could not load type {0}.", name);
                    return(null);
                }

                return(type);
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled())
                {
                    log.Error(e, "Could not load type {0}.", name);
                }
                if (throwOnError)
                {
                    throw;
                }
                return(null);
            }
        }
예제 #4
0
        /// <summary>
        /// Returns a <see cref="System.Type"/> from an already loaded Assembly or an
        /// Assembly that is loaded with a partial name.
        /// </summary>
        /// <param name="name">An <see cref="AssemblyQualifiedTypeName" />.</param>
        /// <param name="throwOnError"><see langword="true" /> if an exception should be thrown
        /// in case of an error, <see langword="false" /> otherwise.</param>
        /// <returns>
        /// A <see cref="System.Type"/> object that represents the specified type,
        /// or <see langword="null" /> if the type cannot be loaded.
        /// </returns>
        /// <remarks>
        /// Attempts to get a reference to the type from an already loaded assembly.  If the
        /// type cannot be found then the assembly is loaded using
        /// <see cref="Assembly.Load(string)" />.
        /// </remarks>
        public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, bool throwOnError)
        {
            try
            {
                // Try to get the type from an already loaded assembly
                System.Type type = System.Type.GetType(name.ToString());

                if (type != null)
                {
                    return(type);
                }

                if (name.Assembly == null)
                {
                    // No assembly was specified for the type, so just fail
                    const string noAssembly = "Could not load type {0}. Possible cause: no assembly name specified.";
                    log.Warn(noAssembly, name);
                    if (throwOnError)
                    {
                        throw new TypeLoadException(string.Format(noAssembly, name));
                    }
                    return(null);
                }

                if (ReflectHelperMemoryAssembly.Instance.GetAssemblyCount() > 0)
                {
                    type = ReflectHelperMemoryAssembly.Instance.GetType(name.Type, name.Assembly);
                    if (type != null)
                    {
                        return(type);
                    }
                }

                Assembly assembly = Assembly.Load(name.Assembly);

                if (assembly == null)
                {
                    log.Warn("Could not load type {0}. Possible cause: incorrect assembly name specified.", name);
                    return(null);
                }

                type = assembly.GetType(name.Type, throwOnError);

                if (type == null)
                {
                    log.Warn("Could not load type {0}.", name);
                    return(null);
                }

                return(type);
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled())
                {
                    log.Error(e, "Could not load type {0}.", name);
                }
                if (throwOnError)
                {
                    throw;
                }
                return(null);
            }
        }
		public void ToStringEscaped()
		{
			AssemblyQualifiedTypeName tn = new AssemblyQualifiedTypeName("Escaped\\,Type", "Escaped\\,Assembly");
			Assert.AreEqual(tn.Type + ", " + tn.Assembly, tn.ToString());
		}
		public void ToStringComplex()
		{
			AssemblyQualifiedTypeName tn = new AssemblyQualifiedTypeName("MyType", "MyAssembly");
			Assert.AreEqual("MyType, MyAssembly", tn.ToString());
		}
		public void ToStringSimple()
		{
			AssemblyQualifiedTypeName tn = new AssemblyQualifiedTypeName("MyType", null);
			Assert.AreEqual("MyType", tn.ToString());
		}