草庐IT

new_post_path

全部标签

c# - 无法从 web api POST 读取正文数据

我正在尝试从新的Asp.NetWebApi中的请求中提取一些数据。我有这样的处理程序设置:publicclassMyTestHandler:DelegatingHandler{protectedoverrideSystem.Threading.Tasks.TaskSendAsync(HttpRequestMessagerequest,System.Threading.CancellationTokencancellationToken){if(request.Content.IsFormData()){request.Content.ReadAsStreamAsync().Contin

c# - 如果类型 T 需要实例化,为什么通用类签名需要指定 new() ?

我正在编写一个通用类,如下所示。publicclassFoo:whereT:Bar,new(){publicvoidMethodInFoo(){T_t=newT();}}如您所见,类型T的对象_t是在运行时实例化的。为了支持泛型类型T的实例化,该语言强制我将new()放在类签名中。如果Bar是一个抽象类,我会同意这一点,但如果Bar是具有公共(public)无参数构造函数的标准非抽象类,为什么需要这样。如果没有找到new(),编译器会提示以下消息。无法创建变量类型“T”的实例,因为它没有new()约束 最佳答案 因为通常没有假设模板

c# - 给定 "where T : new()", "new T()"是否在内部使用 Activator.CreateInstance?

如果我有一个类型参数约束new():voidFoo()whereT:new(){vart=newT();}newT()是否会在内部使用Activator.CreateInstance方法(即反射)? 最佳答案 是的,这是真的。编辑2:这里很好地解释了方法和原因。http://www.simple-talk.com/community/blogs/simonc/archive/2010/11/17/95700.aspx为了验证我编译了如下方法:publicstaticTCreate()whereT:new(){returnnewT()

c# - 委托(delegate)操作 : new Action or casting Action?

我发现了两种不同的方法来使用Action初始化Delegate:创建一个新的Action或转换为Action。Delegatefoo=newAction(()=>DoNothing(param));Delegatebar=(Action)(()=>DoNothing(param));这两种语法有区别吗?哪个更好,为什么?此示例中使用了委托(delegate),因为语法对于使用lambda表达式调用BeginInvoke或Invoke等方法很有用,并且将lambda表达式转换为操作很重要staticmain{Invoke((Action)(()=>DoNothing()));//OKIn

c# - 在 Directory.GetFiles 中使用通配符时出现 "Illegal characters in path"错误

我有一个包含多个包含.doc文件的子目录的目录。示例:C:\Users\user\Documents\testenviroment\Released\test0.docC:\Users\user\Documents\testenviroment\Debug\test1.docC:\Users\user\Documents1\testenviroment\Debug\test2.docC:\Users\user\Documents1\testenviroment\Released\test20.doc我想获取所有Debug文件夹下的所有test*.doc文件。我试过:string[]fi

c# - Lazy<T> 如何解决需要 new() 约束的问题?

示例1(不编译):voidMain(){varc=newC();c.M.F();}classC{T_m=null;publicTM{get{if(_m==null)_m=newT();return_m;}}}classD{publicvoidF(){Console.WriteLine("iwascreated");}}结果:Cannotcreateaninstanceofthevariabletype'T'becauseitdoesnothavethenew()constraint示例2(有效):voidMain(){varc=newC();c.M.F();}classC{Lazy_m

c# - 当我键入 "object"时,如何阻止 Visual Studio 插入 "new {"

在编辑C#源文件时,我输入new{VisualStudio自动将其更正为newobject{有没有办法阻止这种行为? 最佳答案 您可以配置正在键入的字符提交当前的智能感知选择。在工具|选项|文本编辑器|C#|智能感知。删除“{”并确保未选中空格键提交。注意。从VisualStudio2015开始,此选项不再存在。 关于c#-当我键入"object"时,如何阻止VisualStudio插入"new{",我们在StackOverflow上找到一个类似的问题: ht

c# - 如何压缩 HttpWebRequest POST

我正在尝试将数据发布到接受压缩数据的服务器。下面的代码工作得很好,但它是未压缩的。我之前没有使用过压缩或Gzip,因此需要任何帮助。HttpWebRequestrequest=WebRequest.Create(uri)asHttpWebRequest;request.Timeout=600000;request.Method=verb;//POSTrequest.Accept="text/xml";if(!string.IsNullOrEmpty(data)){request.ContentType="text/xml";byte[]byteData=UTF8Encoding.UTF

c# - 使用 HttpWebRequest POST 数据/使用 multipart/form-data 上传图像

我正在尝试使用ImageShackAPI上传图片。要使用它,我应该使用multipart/form-dataPOST图像。我这样做了......varpostData="";varreq=HttpWebRequest.Create("http://www.imageshack.us/upload_api.php");req.Method="POST";req.ContentType="multipart/form-data";postData+="key=my_key_here&";postData+="type=base64&";//getbase64datafromimagebyt

c# - asp.net 中 ./, ../, ../../, ~/on file path(URL) 之间的区别

我有一个脚本文件。看到路径是~/Script。但是,如果我输入../../而不是~/,该过程也同样有效。我的网站URL如:https://sample.com/Scripts/angular.js如果我在Scripts之前输入../../,那么它会自动更改以前的URL(https://sample.com/Scripts/angular.js).Whatistheurlprocess?Andhowcanitsautomaticallychanged?andpleasetellabouttheDifferentbetween./,../,../../,~/,/Scripts,Script