I’m trying to create an instance of HttpPostedFile with
var obj2 = Activator.CreateInstance(
typeof(HttpPostedFile),
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new object[] { },
System.Globalization.CultureInfo.CurrentCulture
);
but I’m getting the error ‘Constructor on type ‘System.Web.HttpPostedFile’ not found.’.
Is there another way to create an instance of HttpPostedFile or am I doing something wrong?
You might be able to use
System.Web.HttpPostedFileBaseinstead. This has the same methods asHttpPostedFilebut is designed for sub-classing. You can then useHttpPostedFileWrapperto pass in a realHttpPostedFileto a method expecting anHttpPostedFileBase.These extra classes are in the System.Web.Abstractions assembly. ASP.NET MVC makes use of this (and other abstractions in the same assembly) to make unit testing easier. See the documentation on MSDN for more information.