Example #1
0
 /// <summary>
 /// Resolves the supplied <paramref name="typeName"/> to a
 /// <see cref="System.Type"/> instance.
 /// </summary>
 /// <param name="typeName">
 /// The unresolved (possibly partially assembly qualified) name 
 /// of a <see cref="System.Type"/>.
 /// </param>
 /// <returns>
 /// A resolved <see cref="System.Type"/> instance.
 /// </returns>
 /// <exception cref="System.TypeLoadException">
 /// If the supplied <paramref name="typeName"/> could not be resolved
 /// to a <see cref="System.Type"/>.
 /// </exception>
 public virtual Type Resolve(string typeName)
 {
     if (StringUtils.IsNullOrEmpty(typeName))
     {
         throw BuildTypeLoadException(typeName);
     }
     TypeAssemblyHolder typeInfo = new TypeAssemblyHolder(typeName);
     Type type = null;
     try
     {
         type = (typeInfo.IsAssemblyQualified) ?
              LoadTypeDirectlyFromAssembly(typeInfo) :
              LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
     }
     catch (Exception ex)
     {
         if (ex is TypeLoadException)
         {
             throw;
         }
         throw BuildTypeLoadException(typeName, ex);
     }
     if (type == null)
     {
         throw BuildTypeLoadException(typeName);
     }
     return type;
 }
 public void CanTakeQualifiedGenericType()
 {
     Type testType = typeof(TestGenericObject<int, string>);
     TypeAssemblyHolder tah = new TypeAssemblyHolder(testType.AssemblyQualifiedName);
     Assert.IsTrue(tah.IsAssemblyQualified);
     Assert.AreEqual(testType.FullName, tah.TypeName);
     Assert.AreEqual(testType.Assembly.FullName, tah.AssemblyName);
 }
 public void CanTakeUnqualifiedType()
 {
     Type testType = typeof(TestObject);
     TypeAssemblyHolder tah = new TypeAssemblyHolder(testType.FullName);
     Assert.IsFalse(tah.IsAssemblyQualified);
     Assert.AreEqual(testType.FullName, tah.TypeName);
     Assert.AreEqual(null, tah.AssemblyName);
 }
        public void CanTakeUnqualifiedType()
        {
            Type testType          = typeof(TestObject);
            TypeAssemblyHolder tah = new TypeAssemblyHolder(testType.FullName);

            Assert.IsFalse(tah.IsAssemblyQualified);
            Assert.AreEqual(testType.FullName, tah.TypeName);
            Assert.AreEqual(null, tah.AssemblyName);
        }
Example #5
0
        public void CanTakeQualifiedGenericType()
        {
            Type testType          = typeof(TestGenericObject <int, string>);
            TypeAssemblyHolder tah = new TypeAssemblyHolder(testType.AssemblyQualifiedName);

            Assert.IsTrue(tah.IsAssemblyQualified);
            Assert.AreEqual(testType.FullName, tah.TypeName);
            Assert.AreEqual(testType.Assembly.FullName, tah.AssemblyName);
        }
Example #6
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyHolder typeInfo)
        {
            Type     type     = null;
            Assembly assembly = Assembly.Load(typeInfo.AssemblyName);

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return(type);
        }
Example #7
0
        /// <summary>
        /// Uses <see cref="M:System.AppDomain.CurrentDomain.GetAssemblies()"/>
        /// to load the attendant <see cref="System.Type"/> referred to by
        /// the <paramref name="typeInfo"/> parameter.
        /// </summary>
        /// <param name="typeInfo">
        /// The type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyHolder typeInfo)
        {
            Type type = null;

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                type = assembly.GetType(typeInfo.TypeName, false, false);
                if (type != null)
                {
                    break;
                }
            }
            return(type);
        }
Example #8
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyHolder typeInfo)
        {
            Type type = null;

#if MONO_2_0
            Assembly assembly = Assembly.Load(typeInfo.AssemblyName);
#else
            Assembly assembly = Assembly.LoadWithPartialName(typeInfo.AssemblyName);
#endif
            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return(type);
        }
 private void ParseAndSetFromStaticFieldValue()
 {
     TypeAssemblyHolder info = new TypeAssemblyHolder(this.staticField);
     // the info.TypeName should now contains the Type name, followed by a
     // period, followed by the name of the field
     int lastDotIndex = info.TypeName.LastIndexOf('.');
     if (lastDotIndex == -1
         || lastDotIndex == info.TypeName.Length)
     {
         throw new ArgumentException(
             "The value passed to the StaticField property must be a fully " +
             "qualified Type plus field name: " +
             "e.g. 'Example.MyExampleClass.MyField, MyAssembly'");
     }
     string typeName = info.TypeName.Substring(0, lastDotIndex);
     string fieldName = info.TypeName.Substring(lastDotIndex + 1);
     StringBuilder buffer = new StringBuilder(typeName);
     if (info.IsAssemblyQualified)
     {
         buffer.Append(TypeAssemblyHolder.TypeAssemblySeparator);
         buffer.Append(info.AssemblyName);
     }
     TargetType = TypeResolutionUtils.ResolveType(buffer.ToString());
     TargetField = fieldName;
 }
Example #10
0
 /// <summary>
 /// Uses <see cref="M:System.AppDomain.CurrentDomain.GetAssemblies()"/>
 /// to load the attendant <see cref="System.Type"/> referred to by 
 /// the <paramref name="typeInfo"/> parameter.
 /// </summary>
 /// <param name="typeInfo">
 /// The type to be loaded.
 /// </param>
 /// <returns>
 /// A <see cref="System.Type"/>, or <see lang="null"/>.
 /// </returns>
 private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyHolder typeInfo)
 {
     Type type = null;
     Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
     foreach (Assembly assembly in assemblies)
     {
         type = assembly.GetType(typeInfo.TypeName, false, false);
         if (type != null)
         {
             break;
         }
     }
     return type;
 }
Example #11
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyHolder typeInfo)
        {
            Type type = null;
#if MONO_2_0
            Assembly assembly = Assembly.Load(typeInfo.AssemblyName);
#else
			Assembly assembly = Assembly.LoadWithPartialName(typeInfo.AssemblyName);
#endif
            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return type;
        }