我有一个包含 C#/VB.NET 的 n 层业务应用程序项目,并且在获取更新方法以在层之间进行通信时遇到问题。在最接近我的数据库的数据层中,我有一个 GetByID() 方法,该方法根据他们在数据库中的 ID 查找员工,并将他们的头衔、名字等以及整个实体放入字典中(序列化)。
在下一层(业务用户)中,我尝试将其私有成员设置为业务用户 GetByID() 方法的结果,该方法调用数据层 GetByID() 方法。在这里我试图设置我的私人成员
_entity = (byte[])retDict["entity"];
这给了我错误,因为 _entity 是一个类型,所以不能将 byte[] 隐式键入到 System.Type。
在 VB 中完成的表示层,通过 listview 选择值传递一个employeeID,然后再向下传递到用户层和数据层。
这是 EmployeeBusinessData 层 GetByID() 代码:
public byte[] GetByID(int empid)
{
Dictionary<string, Object> retDict = new Dictionary<string, Object>();
try
{
HelpDeskDBEntities dbContext = new HelpDeskDBEntities();
dbContext.Configuration.ProxyCreationEnabled = false;
Employee EmployeeEntity = dbContext.Employees.FirstOrDefault(emp => emp.EmployeeID == empid);
if (EmployeeEntity != null)
{
retDict["title"] = EmployeeEntity.Title;
retDict["firstname"] = EmployeeEntity.FirstName;
retDict["lastname"] = EmployeeEntity.LastName;
retDict["phoneno"] = EmployeeEntity.PhoneNo;
retDict["email"] = EmployeeEntity.Email;
retDict["departmentid"] = EmployeeEntity.DepartmentID;
retDict["employeeid"] = EmployeeEntity.EmployeeID;
retDict["entity"] = Serializer(EmployeeEntity, true);
}
else
{
retDict["error"] = "Employee not found!";
}
}
catch (Exception ex)
{
ErrorRoutine(ex, "EmployeeBusinessData", "GetByID");
}
return Serializer(retDict);
}
EmployeeBusinessUser 层 GetById():
public byte[] GetByID(int empid)
{
Dictionary<string, Object> retDict = new Dictionary<string, object>();
try
{
EmployeeBusinessData empData = new EmployeeBusinessData();
retDict = (Dictionary<string, Object>)Deserializer(empData.GetByID(empid));
_employeeID = Convert.ToInt32(retDict["employeeid"]);
_title = Convert.ToString(retDict["title"]);
_firstName = Convert.ToString(retDict["firstname"]);
_lastName = Convert.ToString(retDict["lastname"]);
_phoneNo = Convert.ToString(retDict["phoneno"]);
_email = Convert.ToString(retDict["email"]);
_departmentID = Convert.ToInt32(retDict["departmentid"]);
_entity = (byte[])retDict["entity"];
}
catch (Exception ex)
{
ErrorRoutine(ex, "EmployeeUserData", "GetByID");
}
return Serializer(retDict);
}
这也是 Serializer 方法代码,在两个类 .dll 中都是相同的:
public static byte[] Serializer(Object inObject, bool bIsEntity = false)
{
byte[] ByteArrayObject;
if (bIsEntity) //If the entity uses DataContractSerializer
{
MemoryStream strm = new MemoryStream();
var serializer = new DataContractSerializer(inObject.GetType());
serializer.WriteObject(strm, inObject);
ByteArrayObject = strm.ToArray();
}
else
{
BinaryFormatter frm = new BinaryFormatter();
MemoryStream strm = new MemoryStream();
frm.Serialize(strm, inObject);
ByteArrayObject = strm.ToArray();
}
return ByteArrayObject;
}
private Type _entity;
解串器的两种不同类型:
public static Object Deserializer(byte[] ByteArrayIn, Type entityType)
{
MemoryStream stream = new MemoryStream(ByteArrayIn);
DataContractSerializer ser = new DataContractSerializer(entityType);
Object returnObject = ser.ReadObject(stream);
return returnObject;
}
public static Object Deserializer(byte[] ByteArrayIn)
{
BinaryFormatter frm = new BinaryFormatter();
MemoryStream strm = new MemoryStream(ByteArrayIn);
Object returnObject = frm.Deserialize(strm);
return returnObject;
}
如何让 retDict["entity"] 转换为一个字节,以便将其分配给 _entity?