using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Web;
namespace BatchService.Framework.Utility
{
///
/// ³ÌÐò¼¯·´É丨ÖúÀà
///
public static class AssemblyHelper
{
///
/// µÃµ½Èë¿Ú³ÌÐò¼¯£¬¼æÈÝWebºÍWinform
///
///
public static Assembly GetEntryAssembly()
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
return entryAssembly;
if (System.Web.HttpContext.Current == null ||
System.Web.HttpContext.Current.ApplicationInstance == null)
return Assembly.GetExecutingAssembly();
var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
while (type != null && type.Namespace == "ASP")
{
type = type.BaseType;
}
return type == null ? null : type.Assembly;
}
public static string GetPropertyValue(T t, string propertyName)
{
string ret = "";
var types = typeof(T);
var memberResult = types.GetProperties().FirstOrDefault(p => p.Name.ToLower() == propertyName.ToLower());
var value = memberResult.GetValue(t, null);
if (value != null)
{
ret = value.ToString();
}
else
{
ret = "";
}
return ret;
}
public static IList GetResourceStream(Assembly assembly, System.Linq.Expressions.Expression> predicate)
{
List result = new List();
foreach (string resource in assembly.GetManifestResourceNames())
{
if (predicate.Compile().Invoke(resource))
{
result.Add(assembly.GetManifestResourceStream(resource));
}
}
StreamReader sr = new StreamReader(result[0]);
string r = sr.ReadToEnd();
result[0].Position = 0;
return result;
}
///
/// ɨÃè³ÌÐò¼¯ÕÒµ½¼Ì³ÐÁËij»ùÀàµÄËùÓÐ×ÓÀà
///
/// »ùÀà
/// ÎļþÃû¹ýÂË
///
public static List FindTypeByInheritType(Type inheritType, string searchpattern = "*.dll")
{
var result = new List();
Type attr = inheritType;
string domain = GetBaseDirectory();
string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly);
foreach (string dllFileName in dllFiles)
{
foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes())
{
if (type.BaseType == inheritType)
{
result.Add(type);
}
}
}
return result;
}
///
/// ɨÃè³ÌÐò¼¯ÕÒµ½´øÓÐij¸öAttributeµÄËùÓÐPropertyInfo
///
///
/// ÎļþÃû¹ýÂË
///
public static Dictionary FindAllPropertyByAttribute(string searchpattern = "*.dll") where T : Attribute
{
var result = new Dictionary();
var attr = typeof(T);
string domain = GetBaseDirectory();
string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly);
foreach (string dllFileName in dllFiles)
{
foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes())
{
foreach (var property in type.GetProperties())
{
var attrs = property.GetCustomAttributes(attr, true);
if (attrs.Length == 0)
continue;
result.Add(property, (T)attrs.First());
}
}
}
return result;
}
///
/// ɨÃè³ÌÐò¼¯ÕÒµ½ÀàÐÍÉÏ´øÓÐij¸öAttributeµÄËùÓÐÀàÐÍ
///
///
/// ÎļþÃû¹ýÂË
///
public static Dictionary> FindAllTypeByAttribute(string searchpattern = "*.dll") where T : Attribute
{
var result = new Dictionary>();
Type attr = typeof(T);
string domain = GetBaseDirectory();
string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly);
foreach (string dllFileName in dllFiles)
{
foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes())
{
var typeName = type.AssemblyQualifiedName;
var attrs = type.GetCustomAttributes(attr, true);
if (attrs.Length == 0)
continue;
result.Add(typeName, new List());
foreach (T a in attrs)
result[typeName].Add(a);
}
}
return result;
}
///
/// ɨÃè³ÌÐò¼¯ÕÒµ½ÊµÏÖÁËij¸ö½Ó¿ÚµÄµÚÒ»¸öʵÀý
///
///
/// ÎļþÃû¹ýÂË
///
public static T FindTypeByInterface(string i_servicename, string searchpattern = "BatchService.Custom*.dll") where T : class
{
//var interfaceType = typeof(T);
string domain = GetBaseDirectory();
//domain = Path.Combine(domain, "Plugin");
string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly);
foreach (string dllFileName in dllFiles)
{
foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes())
{
if (i_servicename == type.FullName)
{
LogHelper.Info("´´½¨ÊµÀý:" + type.FullName);
var instance = Activator.CreateInstance(type) as T;
return instance;
}
}
}
return null;
}
public static IEnumerable GetLoadableTypes(this Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException("assembly");
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
}
}
///
/// µÃµ½µ±Ç°Ó¦ÓóÌÐòµÄ¸ùĿ¼
///
///
public static string GetBaseDirectory()
{
var baseDirectory = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
if (AppDomain.CurrentDomain.SetupInformation.PrivateBinPath == null)
baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
return baseDirectory;
}
public static string GetBaseDirectory_Autoswitch()
{
var ret = "";
DirectoryInfo dirInfo = new DirectoryInfo(GetBaseDirectory());
if (IsWebApp())
{
ret = dirInfo.Parent.FullName;
}
else
{
ret = dirInfo.FullName;
}
return ret;
}
///
/// ÅжÏÊÇ·ñÊÇweb³ÌÐò
///
/// true£ºÊÇ£¬false£ºwinform
public static bool IsWebApp()
{
bool flag = false;
if (HttpContext.Current != null)
{
flag = true;
}
//·ñÔòÊÇwinform³ÌÐò
return flag;
}
}
}