Example #1
0
 /// <summary>
 /// Gets the embedded resource stream.
 /// </summary>
 /// <param name="resourceName">Name of the resource.</param>
 /// <returns></returns>
 public static Stream GetEmbeddedResourceStream(string resourceName)
 {
     Stream stream = null;
     BaseAssemblyInfo fileInfo = new BaseAssemblyInfo();
     fileInfo.OriginalName = resourceName;
     if (fileInfo.IsAssemblyReady)
     {
         Assembly assembly = Assembly.Load(fileInfo.AssemblyName);
         stream = assembly.GetManifestResourceStream(fileInfo.ResourceFileName);
         if (stream == null)
         {
             stream = assembly.GetManifestResourceStream(fileInfo.Name);
         }
     }
     else
     {
         Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
         foreach (Assembly assembly in assemblies)
         {
             if (!(assembly is AssemblyBuilder))
             {
                 stream = assembly.GetManifestResourceStream(fileInfo.Name);
                 if (stream != null)
                 {
                     break;
                 }
             }
         }
     }
     return stream;
 }
Example #2
0
 /// <summary>
 /// Gets from embedded resource.
 /// </summary>
 /// <param name="resourceName">Name of the resource.</param>
 /// <returns></returns>
 public static XmlDocument GetFromEmbeddedResource(string resourceName)
 {
     XmlDocument result = new XmlDocument();
     bool loaded = false;
     BaseAssemblyInfo fileInfo = new BaseAssemblyInfo();
     fileInfo.OriginalName = resourceName;
     if (fileInfo.IsAssemblyReady)
     {
         Assembly assembly = Assembly.Load(fileInfo.AssemblyName);
         Stream stream = assembly.GetManifestResourceStream(fileInfo.ResourceFileName);
         if (stream == null)
         {
             stream = assembly.GetManifestResourceStream(fileInfo.Name);
         }
         if (stream != null)
         {
             try
             {
                 result.Load(stream);
                 loaded = true;
             }
             catch (Exception ex)
             {
                 throw new ApplicationException(string.Format("Unable to load xml from embedded resource \"{0}\". Cause : {1}", resourceName, ex.Message), ex);
             }
         }
     }
     else
     {
         Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
         foreach (Assembly assembly in assemblies)
         {
             Stream stream = assembly.GetManifestResourceStream(fileInfo.Name);
             if (stream != null)
             {
                 try
                 {
                     result.Load(stream);
                     loaded = true;
                 }
                 catch (Exception ex)
                 {
                     throw new ApplicationException(string.Format("Unable to load xml from embedded resource \"{0}\". Cause : {1}", resourceName, ex.Message), ex);
                 }
                 break;
             }
         }
     }
     if (!loaded)
     {
         throw new ApplicationException(string.Format("Unable to load xml from embedded resource \"{0}\".", resourceName));
     }
     return result;
 }
Example #3
0
 /// <summary>
 /// Loads a type from assembly.
 /// </summary>
 /// <param name="typeInfo">The type info.</param>
 /// <returns></returns>
 private Type LoadFromAssembly(BaseAssemblyInfo typeInfo)
 {
     Assembly assembly = Assembly.Load(typeInfo.AssemblyName);
     return assembly == null ? null : assembly.GetType(typeInfo.Name, true, true);
 }
Example #4
0
 /// <summary>
 /// Loads a type from all assemblies of the current domain.
 /// </summary>
 /// <param name="typeInfo">The type info.</param>
 /// <returns></returns>
 private Type LoadFromAllAssemblies(BaseAssemblyInfo typeInfo)
 {
     Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
     foreach (Assembly assembly in assemblies)
     {
         Type type = assembly.GetType(typeInfo.Name, false, true);
         if (type != null)
         {
             return type;
         }
     }
     return null;
 }
Example #5
0
 /// <summary>
 /// Resolves a non generic type.
 /// </summary>
 /// <param name="typeName">Name of the type.</param>
 /// <returns></returns>
 /// <exception cref="System.TypeLoadException">
 /// </exception>
 protected Type ResolveNonGeneric(string typeName)
 {
     BaseAssemblyInfo typeInfo = new BaseAssemblyInfo(true);
     typeInfo.OriginalName = typeName;
     Type result = null;
     try
     {
         result = typeInfo.IsAssemblyReady ? LoadFromAssembly(typeInfo) : LoadFromAllAssemblies(typeInfo);
         if (result == null)
         {
             string[] tokens = typeInfo.OriginalName.Split('.');
             //typeInfo.Name = tokens[tokens.Length - 1];
             string s = tokens[0];
             for (int i = 1; i < tokens.Length - 1; i++)
             {
                 s = s + "." + tokens[i];
             }
             typeInfo.AssemblyName = s;
             result = LoadFromAssembly(typeInfo);
         }
     }
     catch (Exception ex)
     {
         throw new TypeLoadException(string.Format("Could not resolve type '{0}'.", typeName), ex);
     }
     return result;
 }