Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何实例化HttpPostedFile_C#_Reflection_File Upload - Fatal编程技术网

C# 如何实例化HttpPostedFile

C# 如何实例化HttpPostedFile,c#,reflection,file-upload,C#,Reflection,File Upload,我正在尝试与一个我无法控制的系统通信,但是它的一个方法接收了一个HttpPostedFile,在我的代码中,我有一个字节数组。我知道HttpPostedFile的构造函数是内部的,有人举过实例化HttpPostedFile的例子吗 我发现的最好的方法是使用反射,但是它们被引导到了另一个方向,我不能接受,因为我无法修改第三方系统方法签名 你可以试试 var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.

我正在尝试与一个我无法控制的系统通信,但是它的一个方法接收了一个HttpPostedFile,在我的代码中,我有一个字节数组。我知道HttpPostedFile的构造函数是内部的,有人举过实例化HttpPostedFile的例子吗

我发现的最好的方法是使用反射,但是它们被引导到了另一个方向,我不能接受,因为我无法修改第三方系统方法签名

你可以试试

 var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
 var obj = (HttpPostedFile)constructorInfo
           .Invoke(new object[] { "filename", "image/jpeg", null });

obj
的类型应该是
HttpPostedFile
。我正在将最后一个参数设置为null,但它必须是一个HttpInputStream。

这确实是一个非常粗糙的代码,但以下内容似乎对我有效:

public HttpPostedFile ConstructHttpPostedFile(byte[] data, string filename, string contentType) {
  // Get the System.Web assembly reference
  Assembly systemWebAssembly = typeof (HttpPostedFileBase).Assembly;
  // Get the types of the two internal types we need
  Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent");
  Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream");

  // Prepare the signatures of the constructors we want.
  Type[] uploadedParams = { typeof(int), typeof(int) };
  Type[] streamParams = {typeHttpRawUploadedContent, typeof (int), typeof (int)};
  Type[] parameters = { typeof(string), typeof(string), typeHttpInputStream };

  // Create an HttpRawUploadedContent instance
  object uploadedContent = typeHttpRawUploadedContent
    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null)
    .Invoke(new object[]{data.Length, data.Length});

  // Call the AddBytes method
  typeHttpRawUploadedContent
    .GetMethod("AddBytes", BindingFlags.NonPublic | BindingFlags.Instance)
    .Invoke(uploadedContent, new object[] {data, 0, data.Length});

  // This is necessary if you will be using the returned content (ie to Save)
  typeHttpRawUploadedContent
    .GetMethod("DoneAddingBytes", BindingFlags.NonPublic | BindingFlags.Instance)
    .Invoke(uploadedContent, null);

  // Create an HttpInputStream instance
  object stream = (Stream)typeHttpInputStream
    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null)
    .Invoke(new object[] {uploadedContent, 0, data.Length});

  // Create an HttpPostedFile instance
  HttpPostedFile postedFile = (HttpPostedFile)typeof(HttpPostedFile)
    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null)
    .Invoke(new object[] {filename, contentType, stream});

  return postedFile;
}

@paracycle-在对返回的HttpPostedFile调用.Save(filePath)时,我得到一个空映像。知道为什么吗?@wilsjd:我已经很久没有调查过这件事了。从那时起,.NET平台代码可能已经发生了很大的变化。我建议您签出新代码(使用Reflector或类似工具)并进行适当的更改。如果需要保存HttpPostedFile,还需要调用DoneAddingBytes方法:
typeHttpRawUploadedContent.GetMethod(“DoneAddingBytes”,BindingFlags.NonPublic | BindingFlags.Instance”).Invoke(uploadedContent,null)@sundog谢谢你,用你的代码更新了示例。这对单元测试非常有用,为我节省了很多时间!