创建HttpPostedFile与反射实例 [英] Creating an instance of HttpPostedFile with Reflection
问题描述
有没有一种方法来创建HttpPostedFile与反射实例
我试过:
VAR OBJ =(HttpPostedFile)typeof运算(HttpPostedFile).GetConstructor(
BindingFlags.NonPublic可| BindingFlags.Instance,
空,Type.EmptyTypes,NULL).Invoke(空) ;
VAR OBJ2 = Activator.CreateInstance(typeof运算(HttpPostedFile)
,BindingFlags.NonPublic可| BindingFlags.Instance
,空
,新的对象[] {}
,System.Globalization.CultureInfo.CurrentCulture);
但他们都不起作用。
更新:显然是内部的构造是这样的:
内部HttpPostedFile(字符串的文件名,字符串的contentType,涧流)
我试过,但没有成功:
流S =新的MemoryStream(b.blob);
// VAR httpPostedFile =新HttpPostedFile();
类型[] =参数{typeof运算(字符串),typeof运算(字符串)的typeof(流)};
VAR OBJ =(HttpPostedFile)typeof运算(HttpPostedFile).GetConstructor(
BindingFlags.NonPublic可| BindingFlags.Instance,
空,参数为空)
.Invoke(新的对象[] {文件名,图像/ JPEG,S});
我会去另一条路线。只需添加一个抽象层次多:创建你所需要的所有成员 IHttpPostedFile
界面和使用,在你的代码,并在同时测试。当你有这个接口,你有两条路走:您可以使用鸭打字中投 IHttpPostedFile
标准 HttpPostedFile
,也可以实现自己的类此接口将呼叫转移至 HttpPostedFile
类的集合实例。
Is there a way to create an instance of HttpPostedFile with Reflection.
I tried:
var obj = (HttpPostedFile)typeof(HttpPostedFile).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, Type.EmptyTypes, null).Invoke(null);
var obj2 = Activator.CreateInstance(typeof(HttpPostedFile)
, BindingFlags.NonPublic | BindingFlags.Instance
, null
, new object[] { }
, System.Globalization.CultureInfo.CurrentCulture);
But both of them do not work.
Update: apparently the internal constructor looks like this:
internal HttpPostedFile(string filename, string contentType, Stream stream)
I tried this but without success:
Stream s = new MemoryStream(b.blob);
//var httpPostedFile = new HttpPostedFile();
Type[] parameters = { typeof(string), typeof(string), typeof(Stream) };
var obj = (HttpPostedFile)typeof(HttpPostedFile).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, parameters, null)
.Invoke(new object[] { "filename", "image/jpeg", s });
I'd go another route. Just add one more level of abstraction: create a IHttpPostedFile
interface with all the members you need and use that both in your code and in tests. When you have this interface, you have two ways to go: you can either use duck typing to "cast" IHttpPostedFile
to standard HttpPostedFile
or you can implement this interface in your own class which will forward calls to an aggregated instance of HttpPostedFile
class.
这篇关于创建HttpPostedFile与反射实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!