草庐IT

TASK_TYPE

全部标签

c# - 从仅给定 Type 实例的 MEF 容器中获取导出

我有一个场景,我必须从我的CompositionContainer实例中导出,但我只有一个Type可以使用;我在编译时不知道类型,因此我无法以正常的通用方式检索导出的对象。通常你会这样做:_container.GetExportedObject();但就我而言,我有这个:TypesomeType=...;_container.HowDoIGetTheExport(someType);有什么想法吗? 最佳答案 找到答案:varexport=_container.GetExports(someType,null,null).FirstO

c# - 使用 System.Type 进行转换 - c#

是否可以使用System.Type?作为引用将对象转换为所需的类型?我进行了搜索,普遍的共识是否定的,尽管我希望C#4.0中引入的一些帮助可以帮助我。即以下将不起作用,但伪代码是我想要的。objecto=null;vart=typeof(string);...stringfoo=(t)o;编辑:我需要使用XmlSerializer来重构/反序列化为存储在t中的类型 最佳答案 看看:varfoo=Convert.ChangeType(o,typeof(string)) 关于c#-使用Sys

c# - 尝试在 : await Task. 之外捕获 Run(()

在awaitTask.Run(()=>之外的trycatch是否有意义,或者仅在await内部使用它们?privateasyncvoidTest(){try{awaitTask.Run(()=>{try{DoingSomething();}catch(Exceptionex){log.Error(ex.Message);}});}catch(Exceptionex){log.Error(ex.Message);}} 最佳答案 如果你处理Exception在委托(delegate)内部(在您的情况下仅用于记录目的),await在正常情

c# - await Task.CompletedTask 是为了什么?

我使用WindowsTemplateStudio创建了UWP应用程序在Build2017上引入的。下面的类是它生成的代码的一部分。publicclassSampleModelService{publicasyncTask>GetDataAsync(){awaitTask.CompletedTask;//();data.Add(newSampleModel{Title="Loremipsumdolorsit1",Description="Loremipsumdolorsitamet",Symbol=Symbol.Globe});data.Add(newSampleModel{Title=

c# - 将 "C# friendly type"名称转换为实际类型 : "int" => typeof(int)

我想得到一个System.Type给定一个string指定一个(原始)类型的C#友好名称,基本上是C#编译器在阅读C#源代码时的方式。我觉得描述我所追求的东西的最佳方式是以单元测试的形式。我希望存在一种通用技术可以使以下所有断言都通过,而不是尝试对特殊C#名称的特殊情况进行硬编码。TypeGetFriendlyType(stringtypeName){...??...}voidTest(){//usingfluentassertionsGetFriendlyType("bool").Should().Be(typeof(bool));GetFriendlyType("int").Sho

c# - 无法设置 Content-Type header

我在设置HttpClient的Content-Type时遇到问题。我跟着这个问题:HowdoyousettheContent-TypeheaderforanHttpClientrequest?但仍然没有运气。StringrcString=JsonConvert.SerializeObject(newRoadsmartChecks(){userguid=user_guid,coords=coordinates,radius=(radius*100)+""},ROADSMART_JSON_FORMAT,JSONNET_SETTINGS);HttpClientc=newHttpClient(

c# - 为什么 Task.WhenAny 没有抛出预期的 TimeoutException?

请注意以下简单代码:classProgram{staticvoidMain(){varsw=newStopwatch();sw.Start();try{Task.WhenAny(RunAsync()).GetAwaiter().GetResult();}catch(TimeoutException){Console.WriteLine("Timedout");}Console.WriteLine("Elapsed:"+sw.Elapsed);Console.WriteLine("PressEntertoexit");Console.ReadLine();}privatestaticas

c# - 安全异常 : Request for the permission of type 'System.Net.Mail.SmtpPermission'

这是“在本地工作,在服务器上不工作”的帖子之一。我有一个发送电子邮件的简单联系表单。在服务器上,我得到以下异常:SecurityExceptionDescription:Theapplicationattemptedtoperformanoperationnotallowedbythesecuritypolicy.Tograntthisapplicationtherequiredpermissionpleasecontactyoursystemadministratororchangetheapplication'strustlevelintheconfigurationfile.Ex

c# - Task.Run 和 Func<>

如何运行一个返回值并接受参数的任务?我看到有一个重载方法Task.Run(Func)但是我怎样才能在那里传递参数呢? 最佳答案 Func不带参数。通常,您会使用lambda表达式捕获参数。例如:publicvoidDoSomething(stringtext){Tasktask=Task.Run(()=>text.Length);...}在这里text是一个捕获的变量...所以即使你只是创建一个Func,它使用方法参数。 关于c#-Task.Run和Func,我们在StackOverfl

c# - 如何调整 "is a type but is used like a variable"?

我正在尝试在网络服务中生成一些代码。但它返回了2个错误:1)List是一种类型,但像变量一样使用2)方法“Customer”没有重载接受“3个参数”[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)][ToolboxItem(false)]publicclasswstest:System.Web.Services.WebService{[WebMethod]publicListGetList(){Listli=List();li.A