Questions tagged «c#»

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

6
如何在Visual Studio中获取文件的相对路径?
我正在尝试获取在Visual Studio的解决方案资源管理器中添加的图像文件的路径,但无法获取该图像的相对路径。H是我的项目的文件结构: / BulutDepoProject /文件夹图标 Folder.ico Main.cs 我可以得到这样的图像: "C:\\Users\\Tolga\\Desktop\\BulutDepo\\BulutDepoProject\\FolderIcon\\Folder.ico" 但是我应该能够通过以下方式获得它: "~\\FolderIcon\\Folder.ico" 我想我不知道它的确切语法,所以我无法获取图像。:(
82 c#  visual-studio 

3
IdentityServer4注册UserService并从asp.net核心中的数据库获取用户
我到处搜索了如何UserService在asp.net核心中向IdentityServer4注册,但是我似乎找不到正确的方法。 这是注册发现InMemoryUsers代码在这里,但是我想从样品中定义我的MSSQL数据库不是静态的用户访问权限的用户。 var builder = services.AddIdentityServer(options => { options.SigningCertificate = cert; }); builder.AddInMemoryClients(Clients.Get()); builder.AddInMemoryScopes(Scopes.Get()); builder.AddInMemoryUsers(Users.Get()); 因此,我查看了这是为IdentityServer3设计的。 var factory = new IdentityServerServiceFactory() .UseInMemoryClients(Clients.Get()) .UseInMemoryScopes(Scopes.Get()); var userService = new UserService(); factory.UserService = new Registration<IUserService>(resolver => userService); 从网上阅读看来,我似乎需要使用DI系统来注册UserService,但是我不确定它如何绑定到IdentityServer。 services.AddScoped<IUserService, UserService>(); 所以我的问题是: 如何将我绑定UserService到构建器(IdentityServer4用户)?我该如何调用数据库来访问和验证UserService(在我使用存储库连接到db的情况下)现有的db用户? 考虑到这一点必须与asp.net core一起使用。 谢谢!

6
每个循环的C#以什么顺序遍历List <T>?
我想知道C#中的foreach循环遍历System.Collections.Generic.List&lt;T&gt;对象的顺序。 我发现了关于同一主题的另一个问题,但是我认为它不能使我满意。 有人指出没有顺序被定义。但是正如其他人所述,它遍历数组的顺序是固定的(从0到Length-1)。8.8.4 foreach语句 也有人说,对于带有顺序的任何标准类(例如List&lt;T&gt;)也是如此。我找不到任何文档来进行备份。因此,就我所知,它现在可能会像现在那样工作,但是也许在下一个.NET版本中,它会有所不同(即使可能不太可能)。 我也看了List(t).Enumerator运气不好的文档。 另一个相关问题指出,对于Java,它在文档中特别提到: List.iterator()以正确的顺序返回此列表中元素的迭代器。” 我正在C#文档中寻找类似的内容。 提前致谢。 编辑:谢谢您为我提供的所有答案(令人惊奇的是我得到这么多回复的速度如此之快)。从所有答案中我了解到的是,List&lt;T&gt;它始终会按其索引顺序进行迭代。但是我仍然希望看到一个清晰的文档说明这一点,类似于上的Java文档List。



10
为什么要使用IList或List?
我知道有很多关于此的文章,但它仍然让我感到困惑,为什么您应该传入IList之类的接口并返回IList之类的接口而不是具体列表。 我读了很多文章,说这如何使以后更容易更改实现,但是我只是不完全了解它是如何工作的。 说如果我有这种方法 public class SomeClass { public bool IsChecked { get; set; } } public void LogAllChecked(IList&lt;SomeClass&gt; someClasses) { foreach (var s in someClasses) { if (s.IsChecked) { // log } } } 我不确定使用IList将如何在将来对我有所帮助。 如果我已经在方法中了怎么办?我应该继续使用IList吗? public void LogAllChecked(IList&lt;SomeClass&gt; someClasses) { //why not List&lt;string&gt; myStrings = new List&lt;string&gt;() IList&lt;string&gt; myStrings …
82 c# 

6
LINQ OrderBy具有多个字段
我有一个列表,需要按两个字段进行排序。我试过在LINQ中使用OrderBy,但这只允许我指定一个字段。我正在寻找要按第一个字段排序的列表,然后在第一个字段中是否有重复项要按第二个字段排序。 例如,我希望结果看起来像这样(按姓氏然后按名字排序)。 约翰·亚当斯 史密斯,詹姆斯 彼得·史密斯 汤普森,弗雷德 我已经看到您可以使用类似SQL的语法来完成此操作,但是我正在寻找一种使用OrderBy方法执行此操作的方法。 IList&lt;Person&gt; listOfPeople = /*The list is filled somehow.*/ IEnumerable&lt;Person&gt; sortedListOfPeople = listOfPeople.OrderBy(aPerson =&gt; aPerson.LastName, aPerson.FirstName); //This doesn't work.
82 c#  linq 

11
C#泛型和类型检查
我有一个使用IList&lt;T&gt;作为参数的方法。我需要检查该T对象的类型是什么,并基于该对象执行某些操作。我试图使用该T值,但编译器不允许使用它。我的解决方案如下: private static string BuildClause&lt;T&gt;(IList&lt;T&gt; clause) { if (clause.Count &gt; 0) { if (clause[0] is int || clause[0] is decimal) { //do something } else if (clause[0] is String) { //do something else } else if (...) //etc for all the types else { throw new ApplicationException("Invalid type"); } } …
82 c#  generics  types 

4
Web.Config调试/发布
我知道Visual Studio 2010中的web.config提供了从数据库从调试模式切换到发布模式的功能。 这是我的Web.Release.config: &lt;?xml version="1.0"?&gt; &lt;!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --&gt; &lt;configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"&gt; &lt;connectionStrings&gt; &lt;add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /&gt; &lt;add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /&gt; &lt;/connectionStrings&gt; &lt;system.web&gt; &lt;compilation xdt:Transform="RemoveAttributes(debug)" /&gt; &lt;/system.web&gt; &lt;/configuration&gt; 这是我的Web.Debug.config代码: &lt;?xml version="1.0"?&gt; &lt;!-- For more information …
82 c#  asp.net  web-config 

2
Func <T>()与Func <T> .Invoke()
我很好奇直接调用Func与在其上使用Invoke()之间的区别。有区别吗?是第一个语法糖,并且在下面调用Invoke()吗? public T DoWork&lt;T&gt;(Func&lt;T&gt; method) { return (T)method.Invoke(); } 与 public T DoWork&lt;T&gt;(Func&lt;T&gt; method) { return (T)method(); } 还是我完全走错了路:)谢谢。
82 c#  invoke  func 

2
在Task中捕获异常的最佳方法是什么?
使用System.Threading.Tasks.Task&lt;TResult&gt;,我必须管理可能引发的异常。我正在寻找做到这一点的最佳方法。到目前为止,我已经创建了一个基类,该基类在调用时管理所有未捕获的异常。.ContinueWith(...) 我想知道是否有更好的方法可以做到这一点。甚至是这样做的好方法。 public class BaseClass { protected void ExecuteIfTaskIsNotFaulted&lt;T&gt;(Task&lt;T&gt; e, Action action) { if (!e.IsFaulted) { action(); } else { Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =&gt; { /* I display a window explaining the error in the GUI * and I log the error. */ this.Handle.Error(e.Exception); })); } } } public class ChildClass …

14
如何在C#中将结构转换为字节数组?
如何在C#中将结构转换为字节数组? 我定义了这样的结构: public struct CIFSPacket { public uint protocolIdentifier; //The value must be "0xFF+'SMB'". public byte command; public byte errorClass; public byte reserved; public ushort error; public byte flags; //Here there are 14 bytes of data which is used differently among different dialects. //I do want the flags2. However, so …
82 c#  struct 

3
在代码中设置WPF标签的Style属性?
在App.xaml中,我有以下代码: &lt;Application.Resources&gt; &lt;Style x:Key="LabelTemplate" TargetType="{x:Type Label}"&gt; &lt;Setter Property="Height" Value="53" /&gt; &lt;Setter Property="Width" Value="130" /&gt; &lt;Setter Property="HorizontalAlignment" Value="Left" /&gt; &lt;Setter Property="Margin" Value="99,71,0,0" /&gt; &lt;Setter Property="VerticalAlignment" Value= "Top" /&gt; &lt;Setter Property="Foreground" Value="#FFE75959" /&gt; &lt;Setter Property="FontFamily" Value="Calibri" /&gt; &lt;Setter Property="FontSize" Value="40" /&gt; &lt;/Style&gt; &lt;/Application.Resources&gt; 这旨在为我的标签提供通用模板。 在主要的XAML代码中,我具有以下代码行: &lt;Label Content="Movies" Style="{StaticResource LabelTemplate}" Name="label1" /&gt; 但是,我想通过代码初始化Style属性。我努力了: …
82 c#  wpf  user-interface  label 

2
默认值类型与属性的类型不匹配
我有这堂课 public class Tooth { public string Id {get;set;} } 而这个立方控制 public partial class ToothUI : UserControl { public ToothUI() { InitializeComponent(); } public Tooth Tooth { get { return (Tooth)GetValue(ToothProperty); } set { SetValue(ToothProperty, value); NombrePieza.Text = value.Id.Replace("_",String.Empty); } } public static readonly DependencyProperty ToothProperty = DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI), …
82 c#  wpf  wpf-controls 

3
表单不响应KeyDown事件
我在Windows Forms项目上工作了一段时间,因此决定尝试键盘快捷键。经过一番阅读之后,我发现我只需要编写一个事件处理程序并将其绑定到表单的KeyDown事件即可: private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.Control &amp;&amp; e.Alt &amp;&amp; e.KeyCode == Keys.O) { MessageBox.Show("Ctrl+Alt+O: magic!"); } } 我这样做是一种很好的方法,即打开Visual Studio设计器的“属性”面板,然后双击表单的KeyDown事件以生成Form1_KeyDown事件处理程序。但是在测试我的应用程序时,该表单完全不响应Ctrl+ Alt+O键盘快捷键。但是,Visual Studio设计器确实生成了将事件处理程序绑定到表单的代码: private void InitializeComponent() { // ... this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); // ... } 因此,我尝试向Console.WriteLine()处理程序添加一个调用,以检查该调用是否已被调用,但也没有运气。 另外,我尝试在事件绑定调用上设置一个断点(如上图所示),发现程序可以很好地达到该断点。但是,我无法在方法定义本身中设置的任何断点都不会到达。 为了确保我正确地执行了前几个步骤,我尝试使用以下命令重复执行这些步骤: 相同解决方案中的新表格。 相同的问题:当我按我的Ctrl+ Alt+O键盘快捷键并且调试器甚至没有进入事件处理程序时,表单都没有响应。 再试一次,它可以工作。 全新的WinForms解决方案。 它工作正常:出现消息对话框(Console.WriteLine()呼叫也可以)。 所以我在这里很迷路。是什么阻止了这个项目中的所有表单接收KeyDown事件?
82 c#  .net  winforms 

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.