我理解 和 之间的关系HttpPostedFileBase
,HttpPostedFileWrapper
就两者的需求而言(即在单元测试/模拟中)。但是为什么,当我在 return 上设置一个断点时HttpPostedFileBase
,它会显示为HttpPostedFileWrapper
吗?
此外,HttpPostedFileBase
不实现 ContentType 属性。那么为什么当我的代码只引用HttpPostedFileBase
而不是引用时它会返回一个值HttpPostedFileWrapper
呢?这是什么诡计?
编辑#1:
感谢@lawliet29 的精彩回复。我已经按照建议写出了结构。
public sealed class MyHttpPostedFile
{
public string ContentType { get { return "123"; } }
}
public abstract class MyHttpPostedFileBase
{
}
public class MyHttpPostedFileWrapper : MyHttpPostedFileBase
{
private MyHttpPostedFile _myHttpPostedFile;
public MyHttpPostedFileWrapper(MyHttpPostedFile myHttpPostedFile) {
_myHttpPostedFile = myHttpPostedFile;
}
public string ContentType { get { return _myHttpPostedFile.ContentType; } }
}
但是,为了使它起作用,我需要像这样传递参数:
GetFiles(new MyHttpPostedFileWrapper(new MyHttpPostedFile());
这似乎是我所质疑的诡计所在。.NET 如何知道传递给它的字节是一个类型的类,MyHttpPostedFile
并且它应该接受该对象并将其作为参数传递给我的构造函数?
编辑#2:
我没有意识到 ASP.NET MVC 绑定器不仅仅通过传递这些更高级别的对象来传递字节。这是我想知道的诡计!感谢您的好评。