0

我正在使用一个 C# 类来发送电子邮件,该类需要一个 HttpPostedFiles 列表作为其方法的参数之一(用于电子邮件的文件附件)。我有一个文件(在文件系统上),我想在我的程序中附加到电子邮件中,我正在使用这个类来完成它。我的问题(我认为)是在下面的代码中:

var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];    
HttpPostedFile postedFile = (HttpPostedFile)constructorInfo.Invoke(new object[] { pathString + "Policy_Assoc_home_computer_V8.pdf", "application/pdf", httpStream });

httpStream应该是 type HttpInputStream。我不知道如何实例化这样的事情。我尝试了以下方法:

string pathString = Server.MapPath("~/Policy/");
                FileStream stream = new FileStream(pathString + "Policy_Assoc_home_computer_V8.pdf", FileMode.Open);
                Assembly systemWebAssembly = typeof(HttpPostedFile).Assembly;
                Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent");
                Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream");
                Type[] uploadedParams = { typeof(int), typeof(int) };
                Type[] streamParams = { typeHttpRawUploadedContent, typeof(int), typeof(int) };
                object uploadedContent = typeHttpRawUploadedContent
                    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null)
                    .Invoke(new object[] { stream.Length, stream.Length });
                object httpStream = (Stream)typeHttpInputStream
                    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null)
                    .Invoke(new object[] { uploadedContent, 0, stream.Length });

但它会在实例化的行上引发错误uploadedContent

object of type System.Int64 cannot be converted to System.Int32

我该怎么做呢?

4

0 回答 0