Web.config<add key="BLL" value="jm.BLL" />BLLnamespace jm.BLL
{
public class Login:ILogin
{}Factorynamespace jm.Factory
{
public class OperatorFactory
{
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["BLL"];
public static object CreateObject(string AssemblyPath, string ClassNamespace)
{
object objType = null;
if (objType == null)
{
try
{
objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
}
catch //未能加载文件或程序集“jm.BLL”或它的某一个依赖项。系统找不到指定的文件。
{ }
}
return objType;
}
public static jm.Interface.ILogin CreateLogin()
{ string ClassNamespace = AssemblyPath + ".Login";
object objType = CreateObject(AssemblyPath, ClassNamespace);
return (jm.Interface.ILogin)objType;
}
}
}Interfacenamespace jm.Interface
{
public interface ILogin
{
bool SystemLogin(string OperatorName, string Password);
}
}UI
namespace UI.Login
{
public partial class Login : System.Web.UI.Page
{
private readonly ILogin ilogin = OperatorFactory.CreateLogin(); }
}
为什么创建不成功,一直提示未能加载文件或程序集“jm.BLL”,谢谢啊
{
public class Login:ILogin
{}Factorynamespace jm.Factory
{
public class OperatorFactory
{
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["BLL"];
public static object CreateObject(string AssemblyPath, string ClassNamespace)
{
object objType = null;
if (objType == null)
{
try
{
objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
}
catch //未能加载文件或程序集“jm.BLL”或它的某一个依赖项。系统找不到指定的文件。
{ }
}
return objType;
}
public static jm.Interface.ILogin CreateLogin()
{ string ClassNamespace = AssemblyPath + ".Login";
object objType = CreateObject(AssemblyPath, ClassNamespace);
return (jm.Interface.ILogin)objType;
}
}
}Interfacenamespace jm.Interface
{
public interface ILogin
{
bool SystemLogin(string OperatorName, string Password);
}
}UI
namespace UI.Login
{
public partial class Login : System.Web.UI.Page
{
private readonly ILogin ilogin = OperatorFactory.CreateLogin(); }
}
为什么创建不成功,一直提示未能加载文件或程序集“jm.BLL”,谢谢啊
下断点,看AssemblyPath输出什么。找不到这个dll文件。
谢谢,AssemblyPath输出的是:jm.BLL
比如 Server.MapPath("~/bll/jm.bll.dll")
方法2. 把jm.bll做成是强命名的程序集,添加到全局程序集缓存里,使用完全限定名Load,比如:
SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3
方法3. 用 Assembly.LoadFile(dll路径)
Assembly.Load(AssemblyPath)这个AssemblyPath应该是带文件后缀名吧,如"jm.BLL.dll"或"jm.BLL.exe",而不是"jm.BLL"。
Assembly.LoadFile 或者 LoadFrom 通过路径加载
objType = Assembly.LoadFile("/Factory/bin/Debug/BLL.dll").CreateInstance(ClassNamespace);提示说,需要绝对路径信息。难不成要从本地的C盘:/文件夹/写起吧