Example #1
1
        public Type[] Execute(System.Reflection.Assembly assembly)
        {
            var targetNamespaces = new HashSet<string>();
            var resourceNames = assembly.GetManifestResourceNames().Where(n => n.EndsWith("VenusIoc.config"));
            foreach (var resourceName in resourceNames)
            {
                var xmlDoc = new XmlDocument();
                using (var sr = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
                {
                    xmlDoc.Load(sr);
                    foreach (var node in xmlDoc.DocumentElement.SelectNodes("components/assemblyScan/namespace"))
                    {
                        var name = ((XmlElement)node).GetAttribute("name");
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            targetNamespaces.Add(name.Trim());
                        }
                    }
                }
            }

            var types = new List<Type>();
            foreach (var type in assembly.GetTypes())
            {
                if (targetNamespaces.Contains(type.Namespace) && !type.IsAbstract && type.IsDefined(typeof(NamedAttribute), false))
                {
                    types.Add(type);
                }
            }

            return types.ToArray();
        }
        public static void EnsureResources(System.Reflection.Assembly assembly, string dataPath)
        {
            var contentIdentifier = assembly.GetName().Name + ".Content.";
            var scriptIdentifier = assembly.GetName().Name + ".Scripts.";
            var dataIdentifier = assembly.GetName().Name + ".App_Data.";

            foreach (var resource in assembly.GetManifestResourceNames()) {
                if (resource.StartsWith (contentIdentifier)) {
                    // Treat Content resources as though every "." except the last is a directory separator
                    var path = resource.Substring (contentIdentifier.Length);
                    var lastDot = path.LastIndexOf (".");
                    if (lastDot > -1)
                        path = path.Substring (0, lastDot).Replace ('.', Path.DirectorySeparatorChar) + "." + path.Substring (lastDot + 1);
                    else
                        path = path.Replace('.', Path.DirectorySeparatorChar);
                    path = Path.Combine (dataPath, path);
                    EnsureResource (assembly, path, resource);
                } else if (resource.StartsWith (scriptIdentifier)) {
                    var path = Path.Combine (dataPath, resource.Substring (scriptIdentifier.Length));
                    EnsureResource (assembly, path, resource);
                } else if (resource.StartsWith (dataIdentifier)) {
                    var path = Path.Combine (dataPath, resource.Substring (dataIdentifier.Length));
                    EnsureResource (assembly, path, resource);
                }
            }
        }
		public static StreamReader GetStream(System.Reflection.Assembly assembly, string name)
		{
			foreach (string resName in assembly.GetManifestResourceNames())
			{
				if (resName.EndsWith(name))
					return new StreamReader(assembly.GetManifestResourceStream(resName));
			}
			return null;
		}
Example #4
0
        private static Dictionary<string, string> GetEmbeddedResourceMapping(System.Reflection.Assembly assembly)
        {
            string[] resources = assembly.GetManifestResourceNames();

            Dictionary<string, string> resourceMap = new Dictionary<string, string>();

            foreach (string resource in resources)
            {
                resourceMap[resource.ToLower()] = resource;
            }

            return resourceMap;
        }
Example #5
0
        public static SchemaInfo GetSchemaFromAssembly(System.Reflection.Assembly ass)
        {
            SchemaInfo schemaInfo = (SchemaInfo) assembly2SchemaInfo[ass];

            if (schemaInfo == null)
            {
                lock (typeof(SchemaLoader))
                {
                    schemaInfo = (SchemaInfo) assembly2SchemaInfo[ass];
                    if (schemaInfo == null)
                    {
                        foreach (string name in ass.GetManifestResourceNames())
                        {
                            if (name.EndsWith("_DBSchema.bin"))
                            {
                                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                                using (Stream resourceStream = ass.GetManifestResourceStream(name))
                                {
                                    schemaInfo = (SchemaInfo) bf.Deserialize(resourceStream);
                                    schemaInfo.Resolve();
                                }
                                break;
                            }
                            if (name.EndsWith("_DBSchema.xml"))
                            {
                                using (Stream resourceStream = ass.GetManifestResourceStream(name))
                                {
                                    XmlSerializer ser = new XmlSerializer(typeof(SchemaInfo));
                                    XmlTextReader reader = new XmlTextReader(resourceStream);

                                    schemaInfo = (SchemaInfo) ser.Deserialize(reader);
                                    schemaInfo.Resolve();
                                }
                                break;
                            }
                        }
                        if (schemaInfo == null)
                        {
                            throw new InvalidOperationException("_DBSchema.xml not embedded in " + ass.CodeBase);
                        }
                        assembly2SchemaInfo[ass] = schemaInfo;
                    }
                }
            }
            return schemaInfo;
        }
 public static void WriteResource(System.Reflection.Assembly targetAssembly, string resourceName, string filePath)
 {
     string[] resources = targetAssembly.GetManifestResourceNames();
     List<string> resoruceList = resources.ToList();
     foreach (string s in resoruceList)
     {
         using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(s))
         {
             using (System.IO.FileStream fileStream = new System.IO.FileStream(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filePath), resourceName), System.IO.FileMode.Create))
             {
                 for (int i = 0; i < stream.Length; i++)
                 {
                     fileStream.WriteByte((byte)stream.ReadByte());
                 }
                 fileStream.Close();
             }
         }
     }
 }
        public Type[] Execute(System.Reflection.Assembly assembly)
        {
            var targetNamespaces = NamespaceList.Create();
            var resourceNames = assembly.GetManifestResourceNames().Where(n => n.EndsWith("VenusIoc.config"));
            foreach (var resourceName in resourceNames)
            {
                var xmlDoc = new XmlDocument();
                using (var sr = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
                {
                    xmlDoc.Load(sr);
                    foreach (var node in xmlDoc.DocumentElement.SelectNodes("components/assemblyScan/namespace"))
                    {
                        var name = ((XmlElement)node).GetAttribute("name");
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            targetNamespaces.Add(name.Split('.'));
                        }
                    }
                }
            }

            if (targetNamespaces.Count == 0)
                return new Type[0];

            var types = new List<Type>();
            var checkList = new HashSet<string>();
            var ignoreList = new HashSet<string>();
            foreach (var type in assembly.GetTypes())
            {
                bool toCheck = false;
                if (!ignoreList.Contains(type.Namespace) && type.Namespace != null)
                {
                    if (checkList.Contains(type.Namespace))
                    {
                        toCheck = true;
                    }
                    else
                    {
                        if (targetNamespaces.Include(type.Namespace.Split('.')))
                        {
                            checkList.Add(type.Namespace);
                            toCheck = true;
                        }
                        else
                        {
                            ignoreList.Add(type.Namespace);
                        }
                    }
                }

                if (toCheck)
                {
                    if (!type.IsAbstract && type.IsDefined(typeof(NamedAttribute), false))
                    {
                        types.Add(type);
                    }
                }
            }

            return types.ToArray();
        }
Example #8
0
		private System.IO.Stream GetManifestResourceStream(System.Reflection.Assembly a, string resourceName)
		{
			resourceName=resourceName.ToLower();
			string[] resources=a.GetManifestResourceNames();
			foreach(string name in resources)
			{
				if(name.ToLower()==resourceName)
				{
					return a.GetManifestResourceStream(name);
					//break;
				}
			}
			return null;
		}
Example #9
0
 public static Stream GetEmbeddedFile(System.Reflection.Assembly assembly, string fullName)
 {
     if (assembly == null) return null;
     try
     {
         Stream str = assembly.GetManifestResourceStream(fullName);
         if (str == null)
         {
             string[] names = assembly.GetManifestResourceNames();
             if (names != null)
             {
                 string targetName = fullName;
                 targetName = targetName.ToLower();
                 foreach (string name in names)
                 {
                     if (name.ToLower() == targetName)
                     {
                         str = assembly.GetManifestResourceStream(name);
                         if (str != null)
                         {
                             Log.WarnFormat("It is better use name '{0}' to load embedded resource.", name);
                         }
                         break;
                     }
                 }
             }
         }
         return str;
     }
     catch (Exception e)
     {
         throw new Exception(assembly.FullName + ": " + e.Message);
     }
 }
Example #10
0
        public static string GetEmbeddedSqlScript(string strScriptName, ref System.Reflection.Assembly ass)
        {
            string strReturnValue = null;

            bool bNotFound = true;
            foreach (string strThisRessourceName in ass.GetManifestResourceNames())
            {
                if (strThisRessourceName != null && strThisRessourceName.EndsWith(strScriptName, System.StringComparison.OrdinalIgnoreCase))
                {

                    using (System.IO.StreamReader sr = new System.IO.StreamReader(ass.GetManifestResourceStream(strThisRessourceName)))
                    {
                        bNotFound = false;
                        strReturnValue = sr.ReadToEnd();
                        break;
                    } // End Using sr

                } // End if (strThisRessourceName != null && strThisRessourceName.EndsWith(strScriptName, StringComparison.OrdinalIgnoreCase) )

            } // Next strThisRessourceName

            if (bNotFound)
            {
                throw new System.Exception("No script called \"" + strScriptName + "\" found in embedded ressources.");
            }

            return strReturnValue;
        }