Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

1
使用Fluent验证进行条件验证
我需要的是一种方法,可以根据是否填写其他字段来有条件地验证字段。 例如 我有一个下拉列表和一个相关的日期字段。如果未设置任何字段,则表单应通过验证。但是,如果设置了两个字段之一,但未设置另一个字段,则将触发验证,要求设置另一个字段。 我已经编写了自定义验证类,但似乎是在单个字段上进行验证。有没有一种方法可以使用内置验证器来设置我需要的验证?如果不是,是否存在使用自定义验证器连接两个字段的好方法?

4
通过POST在C#中发送JSON并接收返回的JSON?
这是我第一次在任何应用程序中使用JSONSystem.Net和WebRequest。我的应用程序应该将JSON有效负载(类似于以下内容)发送到身份验证服务器: { "agent": { "name": "Agent Name", "version": 1 }, "username": "Username", "password": "User Password", "token": "xxxxxx" } 为了创建此有效负载,我使用了JSON.NET库。如何将这些数据发送到身份验证服务器并接收其JSON响应?这是我在一些示例中看到的内容,但没有JSON内容: var http = (HttpWebRequest)WebRequest.Create(new Uri(baseUrl)); http.Accept = "application/json"; http.ContentType = "application/json"; http.Method = "POST"; string parsedContent = "Parsed JSON Content needs to go here"; ASCIIEncoding encoding = new ASCIIEncoding(); Byte[] bytes …

28
找不到VS 2017元数据文件'.dll
我知道还有另一个问题完全相同,但是我从所有这些答案中脱颖而出,没有一个帮助我。:((这是问题。) 我刚刚创建了一个新的ASP.NET MVC项目,并在解决方案中加入了一些'.dll'。现在,当我尝试构建项目时,在5个库中的3个上显示以下错误消息。 Error CS0006 Metadata file 'C:\Users\...\source\Database\bin\Debug\DataAccessLayer.dll' could not be found Logic C:\Users\...\source\Logic\CSC 1 Active Error CS0006 Metadata file 'C:\Users\...\source\Logic\bin\Debug\Logic.dll' could not be found PTS2-MVC C:\Users\...\source\PTS2-MVC\CSC 1 Active Error CS0006 Metadata file 'C:\Users\...\source\PTS2-MVC\bin\PTS2-MVC.dll' could not be found PTS2-MVC.Tests C:\Users\...\source\PTS2-MVC.Tests\CSC 1 Active 当我转到该.dll的bin \ debug文件夹时,我看到它为空,而没有收到错误消息的另一个.dll也为空。但是我不知道如何解决这个问题或为实现这一目标而做了什么。 最常见的答案是转到解决方案的属性,转到配置,然后取消选中-> Apply- >再检查并再次应用,但这没有用

3
是什么 ?[]?C#中的语法?
虽然我正在研究委托其是实际上是一个抽象类中Delegate.cs,我看到了下面的方法中,我不明白 为什么返回值?已经使用了reference(class)类型,为什么要使用 ?[]? 参数含义 你能解释一下吗? public static Delegate? Combine(params Delegate?[]? delegates) { if (delegates == null || delegates.Length == 0) return null; Delegate? d = delegates[0]; for (int i = 1; i < delegates.Length; i++) d = Combine(d, delegates[i]); return d; }


9
?:?? 运算符代替IF | ELSE
public string Source { get { /* if ( Source == null ){ return string . Empty; } else { return Source; } */ return Source ?? string.Empty; } set { /* if ( Source == null ) { Source = string . Empty; } else { if ( Source …

7
在WPF中对WebBrowser的Source属性进行数据绑定
有谁知道如何在WPF(3.5SP1)中对WebBrowser的.Source属性进行数据绑定?我有一个列表视图,我想在左侧有一个小的WebBrowser,在右侧是内容,并希望将每个WebBrowser的源与绑定到列表项的每个对象中的URI进行数据绑定。 到目前为止,这就是我作为概念证明所得到的,但是“ <WebBrowser Source="{Binding Path=WebAddress}"”没有编译。 <DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress"> <StackPanel Orientation="Horizontal"> <!--Web Control Here--> <WebBrowser Source="{Binding Path=WebAddress}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Width="300" Height="200" /> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" /> <TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" /> </StackPanel> <TextBox Text="{Binding Path=Street[0]}" /> <TextBox Text="{Binding Path=Street[1]}" /> <TextBox Text="{Binding Path=PhoneNumber}"/> <TextBox Text="{Binding …
85 c#  wpf  xaml  data-binding  browser 


3
访问闭包警告中的foreach变量
我收到以下警告: 访问闭包中的foreach变量。使用不同版本的编译器进行编译时,可能会有不同的行为。 这是我的编辑器中的样子: 我知道如何解决此警告,但我想知道为什么会收到此警告? 这是关于“ CLR”版本的吗?与“ IL”有关吗?
85 c#  .net 


12
嵌套循环的替代方法更快吗?
我需要创建一个数字组合列表。数字很​​小,所以我可以用byte代替int。但是,它需要许多嵌套循环才能获得所有可能的组合。我想知道是否有一种更有效的方式来执行我要执行的操作。到目前为止的代码是: var data = new List<byte[]>(); for (byte a = 0; a < 2; a++) for (byte b = 0; b < 3; b++) for (byte c = 0; c < 4; c++) for (byte d = 0; d < 3; d++) for (byte e = 0; e < 4; …
85 c#  combinations 

9
在捕获块中等待
我有以下代码: WebClient wc = new WebClient(); string result; try { result = await wc.DownloadStringTaskAsync( new Uri( "http://badurl" ) ); } catch { result = await wc.DownloadStringTaskAsync( new Uri( "http://fallbackurl" ) ); } 基本上,我想从一个URL下载,当它失败并出现异常时,我想从另一个URL下载。当然这两个时间都是异步的。但是由于以下原因,代码无法编译 错误CS1985:无法在catch子句的主体中等待 好的,出于任何原因都被禁止,但是这里的正确代码模式是什么? 编辑: 好消息是C#6.0可能会允许catch和finally块中的等待调用。

10
C#可以将值类型比较为null
我今天遇到了这个问题,不知道为什么C#编译器没有抛出错误。 Int32 x = 1; if (x == null) { Console.WriteLine("What the?"); } 我对x怎么可能为null感到困惑。特别是由于此分配肯定会引发编译器错误: Int32 x = null; x是否有可能成为null,Microsoft是否只是决定不将此检查放入编译器,还是完全错过了? 更新:在弄乱编写本文的代码之后,突然编译器提出了警告,该表达式永远不会为真。现在我真的迷路了。我将对象放到一个类中,现在警告已经消失了,但还有一个问题,值类型最终是否可以为null。 public class Test { public DateTime ADate = DateTime.Now; public Test () { Test test = new Test(); if (test.ADate == null) { Console.WriteLine("What the?"); } } }
85 c#  null 

14
在C#中,为什么List <string>对象不能存储在List <object>变量中
似乎List对象不能存储在C#中的List变量中,甚至不能以这种方式显式转换。 List&lt;string&gt; sl = new List&lt;string&gt;(); List&lt;object&gt; ol; ol = sl; 结果无法将类型隐式转换System.Collections.Generic.List&lt;string&gt;为System.Collections.Generic.List&lt;object&gt; 然后... List&lt;string&gt; sl = new List&lt;string&gt;(); List&lt;object&gt; ol; ol = (List&lt;object&gt;)sl; 结果无法将类型转换System.Collections.Generic.List&lt;string&gt;为System.Collections.Generic.List&lt;object&gt; 当然,您可以通过将所有内容从字符串列表中拉出并一次放回一个列表中来完成此操作,但这是一个相当复杂的解决方案。

9
使用MVC,C#和jQuery导出为CSV
我正在尝试将列表导出到CSV文件。我已经完成所有工作,直到我想将文件写入响应流。这什么也没做。 这是我的代码: 从页面调用方法。 $('#btn_export').click(function () { $.post('NewsLetter/Export'); }); 控制器中的代码如下: [HttpPost] public void Export() { try { var filter = Session[FilterSessionKey] != null ? Session[FilterSessionKey] as SubscriberFilter : new SubscriberFilter(); var predicate = _subscriberService.BuildPredicate(filter); var compiledPredicate = predicate.Compile(); var filterRecords = _subscriberService.GetSubscribersInGroup().Where(x =&gt; !x.IsDeleted).AsEnumerable().Where(compiledPredicate).GroupBy(s =&gt; s.Subscriber.EmailAddress).OrderBy(x =&gt; x.Key); ExportAsCSV(filterRecords); } catch …
85 c#  jquery  asp.net-mvc  csv 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.