我自己学着写的这个,但是如果加上命名空间,就会出现“未能从程序集“App_Code.rdirds3h, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“EmployeeDA”。”这个错误。如果去掉命名空间,就没问题! msdn上说命名空间不指定的话,也会有个默认的命名空间。为什么指定的命名空间会发生错误? 谢谢!代码如下:using System;
using System.Text;
using System.Web;
using SMS.MySqlHelper;namespace SMS.DataAccess
{
///<summary>
///
///</summary>
public interface IEntityDA
{
} public class DataAccessFactory
{
public IEntityDA CreateEntityDA(string type)
{
IEntityDA da = null;
// try
// {
Type currentType = Type.GetType(type, true); da = (IEntityDA)Activator.CreateInstance(currentType);
// }
// catch (TypeLoadException e)
// {
// System.Web.HttpContext.Current.Response.Write("该对象未被定义,详细信息<br />" + e.Message);
// }
return da;
}
} public class MemberDA : IEntityDA
{
public MemberDA()
{
System.Web.HttpContext.Current.Response.Write("Member");
}
} public class EmployeeDA : IEntityDA
{
public string name; public EmployeeDA()
{
name = "admin";
System.Web.HttpContext.Current.Response.Write(name);
}
}
}
using System.Text;
using System.Web;
using SMS.MySqlHelper;namespace SMS.DataAccess
{
///<summary>
///
///</summary>
public interface IEntityDA
{
} public class DataAccessFactory
{
public IEntityDA CreateEntityDA(string type)
{
IEntityDA da = null;
// try
// {
Type currentType = Type.GetType(type, true); da = (IEntityDA)Activator.CreateInstance(currentType);
// }
// catch (TypeLoadException e)
// {
// System.Web.HttpContext.Current.Response.Write("该对象未被定义,详细信息<br />" + e.Message);
// }
return da;
}
} public class MemberDA : IEntityDA
{
public MemberDA()
{
System.Web.HttpContext.Current.Response.Write("Member");
}
} public class EmployeeDA : IEntityDA
{
public string name; public EmployeeDA()
{
name = "admin";
System.Web.HttpContext.Current.Response.Write(name);
}
}
}
返回是放在 try{ return; }
不过如果发生错误,是不是就会返回一个null,我就不知道了
to ccp5780199:
我试了下,果然。如果没有 da = null ,会提示错误,"使用了未赋值的局部变量da";不过这里把 return da放到try里又不行了,提示“返回值的方法必须在所有代码路径中都具有 return 语句”
配合 throw 就可以在try里返回。。不知道有什么区别都是C#
自己规定的东西