Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#

Project Tool

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
23 Sep 2007CPOL3 min read 54.5K   1.7K   73  
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace QiHe.CodeLib
{
    /// <summary>
    /// Read stream from Assembly Resource
    /// </summary>
    public class Resource
    {
        public static Stream GetStream(Type type, string name)
        {
            //Assembly assembly = Assembly.GetAssembly(type);
            Assembly assembly = type.Assembly;
            return assembly.GetManifestResourceStream(type, name);
        }

        public static Stream GetStream(string fullname)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            return assembly.GetManifestResourceStream(fullname);
        }

        public static StreamReader GetStreamReader(string fullname)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream stream = assembly.GetManifestResourceStream(fullname);
            if (stream == null) return null;
            return new StreamReader(stream);
        }
        public static StreamReader GetStreamReader(Type type, string name)
        {
            Assembly assembly = type.Assembly;
            string resname = type.Namespace + "." + name;
            Stream stream = assembly.GetManifestResourceStream(resname);
            if (stream == null) return null;
            return new StreamReader(stream);
        }
        public static StreamReader GetStreamReader(Type type, string name, string encodingName)
        {
            Assembly assembly = type.Assembly;
            Stream stream = assembly.GetManifestResourceStream(type, name);
            if (stream == null) return null;
            Encoding encoding = Encoding.GetEncoding(encodingName);
            return new StreamReader(stream, encoding, false);
        }

        public static Type GetTypeInAssembly(Assembly assembly, string Namespace, string name)
        {
            string fullName = Namespace + "." + name;
            return assembly.GetType(fullName, false);
        }

        public static Dictionary<string, Type> GetTypesInAssembly(Assembly assembly)
        {
            Type[] alltypes = assembly.GetTypes();
            Dictionary<string, Type> typesdict = new Dictionary<string, Type>(alltypes.Length);
            foreach (Type type in alltypes)
            {
                typesdict[type.Name] = type;
            }
            return typesdict;
        }

        public static Dictionary<string, Type> GetTypesInAssembly(Assembly assembly, string Namespace)
        {
            Type[] alltypes = assembly.GetTypes();
            Dictionary<string, Type> typesdict = new Dictionary<string, Type>(alltypes.Length);
            foreach (Type type in alltypes)
            {
                if (type.FullName == Namespace + "." + type.Name)
                {
                    typesdict[type.Name] = type;
                }
            }
            return typesdict;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions