C#学习教程:使用反射创建HttpPostedFile实例我试过:varobj=(HttpPostedFile)typeof(HttpPostedFile).GetConstructor(BindingFlags.NonPublic|BindingFlags.Instance,null,Type.EmptyTypes,null).Invoke(null);varobj2=Activator.CreateInstance(typeof(HttpPostedFile),BindingFlags.NonPublic|BindingFlags.Instance,null,newobject[]{},System.Globalization.CultureInfo.CurrentCulture);但它们都不起作用。更新:显然内部构造函数看起来像这样:internalHttpPostedFile(stringfilename,stringcontentType,Streamstream)我试过没有成功:Streams=newMemoryStream(b.blob);//varhttpPostedFile=newHttpPostedFile();类型[]参数={typeof(string),typeof(string),typeof(Stream)};varobj=(HttpPostedFile)typeof(HttpPostedFile).GetConstructor(BindingFlags.NonPublic|BindingFlags.Instance,null,parameters,null).Invoke(newobject[]{"文件名","图像/jpeg",s});我会走另一条路。只需添加一个抽象级别:使用您需要的所有成员创建一个IHttpPostedFile接口,并在您的代码和测试中使用它们。当你有这个接口时,你有两种方法可以做到这一点:你可以使用鸭子类型将IHttpPostedFile“转换”为标准的HttpPostedFile或者你可以在你自己的类中实现这个接口,它将调用转发到HttpPostedFile的类Aggregation实例。HttpPostedFile类需要一个HttpInputStream类的实例,而不是MemeoryStream。HttpInputStream又是一个内部类,因此您需要使用反射来创建它。你真的需要这样做吗?以上就是C#学习教程的全部内容:使用Reflection创建HttpPostedFile实例。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权请点击右侧联系管理员删除。如需转载请注明出处:
