Questions tagged «c#»

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

1
与实现INotifyPropertyChanged时的替代方法相比,[CallerMemberName]是否较慢?
有好文章提出了不同的实现方法INotifyPropertyChanged。 考虑以下基本实现: class BasicClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void FirePropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } private int sampleIntField; public int SampleIntProperty { get { return sampleIntField; } set { if (value != sampleIntField) { sampleIntField = value; …

3
Log4Net,如何向我的日志中添加自定义字段
我使用log4net.Appender.AdoNetAppender附加程序。 我的log4net表是以下字段[Date],[Thread],[Level],[Logger],[Message],[Exception] 我需要在log4net表中添加另一个字段(例如SalesId),但是当记录错误或信息消息时,如何在xml和代码中指定记录“ SalesId”的信息? 例如 log.Info("SomeMessage", SalesId) 这是log4net xml <appender name="SalesDBAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value ="System.Data.SqlClient.SqlConnection" /> <connectionString value="Data Source=..." /> <commandText value="INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@thread" …
98 c#  log4net  appender 

8
在C#中,如何实例化方法内部传递的泛型类型?
如何在InstantiateType<T>下面的方法中实例化类型T ? 我收到错误消息:“ T”是“类型参数”,但其用法类似于“变量”。: (向下滚动以获取答复) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGeneric33 { class Program { static void Main(string[] args) { Container container = new Container(); Console.WriteLine(container.InstantiateType<Customer>("Jim", "Smith")); Console.WriteLine(container.InstantiateType<Employee>("Joe", "Thompson")); Console.ReadLine(); } } public class Container { public T InstantiateType<T>(string firstName, string lastName) where T : IPerson …
98 c#  generics 


1
主要构造函数不再在VS2015中编译
在这一天之前,我可以使用主要的构造函数,例如: public class Test(string text) { private string mText = text; } 为了做到这一点,在以前的Visual Studio CTP中,我必须将其添加到csproj文件中: <LangVersion>Experimental</LangVersion> 无论如何,这在Visual Studio 2015预览版(带有或不带有LangVersion)中不再起作用。有人对可能发生的事情有任何想法吗?


10
在当前上下文中不存在HttpUtility
编译C#应用程序时出现此错误。看起来像个小错误,但我无法解决。 我的设置是Windows 7 64位。Visual Studio 2010 C#Express B2Rel。 我添加了对位于的System.Web.dll的引用C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0,但是它带有黄色的感叹号,并且仍然出现上述错误。我也有using System.Web宣言。 我究竟做错了什么? 更新:在得到提示我根本原因的迅速答案之后,我在Google中进行了一些搜索,搜索到该文件指出System.Web.dll适用于整个框架。我没有找到这样的参考。 对于像我这样的新手,此博客很好地总结了框架(客户端和完整)之间的差异。我找不到可以说客户端框架是否支持某个Dll的地方。我想Visual Studio中的感叹号应该是第一个信号...


5
使用C#更改文件扩展名
我有许多文件类型:pdf,tiff,jpeg,bmp。等等。我的问题是如何更改文件扩展名?我尝试了这个: my file= c:/my documents/my images/cars/a.jpg; string extension = Path.GetExtension(myffile); myfile.replace(extension,".Jpeg"); 无论文件是哪种类型,我指定的格式都必须带有文件名。但这行不通。我从类似的浏览器获取文件路径c:\..\..\a.jpg,并且文件格式为a.jpeg。因此,当我尝试删除它时,它给了我一个错误:Cannot find the file on specified path'。因此,我认为这与文件扩展名不匹配有关。所以,我试图转换.jpg为.jpeg然后删除该文件。
98 c# 

13
ReferentialConstraint中的从属属性映射到商店生成的列
写入数据库时​​出现此错误: ReferentialConstraint中的从属属性映射到商店生成的列。列:“ PaymentId”。 public bool PayForItem(int terminalId, double paymentAmount, eNums.MasterCategoryEnum mastercategoryEnum, int CategoryId, int CategoryItemId) { using (var dbEntities = new DatabaseAccess.Schema.EntityModel()) { int pinnumber = 0; long pinid = 1; //getPinId(terminalId,ref pinnumber) ; var payment = new DatabaseAccess.Schema.Payment(); payment.CategoryId = CategoryId; payment.ItemCategoryId = CategoryItemId; payment.PaymentAmount = (decimal)paymentAmount; payment.TerminalId = …

3
在后面的代码中更改Canvas.Left属性?
我的XAML中有一个矩形,想Canvas.Left在后面的代码中更改其属性: <UserControl x:Class="Second90.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300" KeyDown="txt_KeyDown"> <Canvas> <Rectangle Name="theObject" Canvas.Top="20" Canvas.Left="20" Width="10" Height="10" Fill="Gray"/> </Canvas> </UserControl> 但这不起作用: private void txt_KeyDown(object sender, KeyEventArgs e) { theObject.Canvas.Left = 50; } 谁知道这样做的语法是什么?

4
将IList或IEnumerable转换为数组的最佳方法
我有一个HQL查询,可以生成结果的IList或结果的IEnumerable。 但是,我希望它返回我选择的Entity的数组,实现该目标的最佳方法是什么?我可以枚举它并构建数组,也可以使用CopyTo()一个定义的数组。 有什么更好的办法吗?我采用了CopyTo方法。
98 c#  nhibernate 

6
从32位应用程序读取64位注册表
我有一个为AnyCPU编译的ac#单元测试项目。我们的构建服务器是一台64位计算机,并安装了64位SQL Express实例。 测试项目使用与以下类似的代码来标识.MDF文件的路径: private string GetExpressPath() { RegistryKey sqlServerKey = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" ); string sqlExpressKeyName = (string) sqlServerKey.GetValue( "SQLEXPRESS" ); RegistryKey sqlInstanceSetupKey = sqlServerKey.OpenSubKey( sqlExpressKeyName + @"\Setup" ); return sqlInstanceSetupKey.GetValue( "SQLDataRoot" ).ToString(); } 该代码在我们的32位工作站上可以正常工作,并且在构建服务器上也可以正常工作,直到我最近启用了NCover的代码覆盖率分析为止。因为NCover使用32位COM组件,所以测试运行程序(Gallio)作为32位进程运行。 检查注册表,下面没有“实例名称”键 HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Microsoft SQL Server 以32位模式运行的应用程序是否可以访问Wow6432Node外部的注册表?
98 c#  .net  64-bit  registry  32-bit 


3
为什么此条件(null ||!TryParse)会导致“使用未分配的局部变量”?
以下代码导致使用未分配的局部变量“ numberOfGroups”: int numberOfGroups; if(options.NumberOfGroups == null || !int.TryParse(options.NumberOfGroups, out numberOfGroups)) { numberOfGroups = 10; } 但是,此代码可以正常工作(尽管ReSharper说的= 10是多余的): int numberOfGroups = 10; if(options.NumberOfGroups == null || !int.TryParse(options.NumberOfGroups, out numberOfGroups)) { numberOfGroups = 10; } 我是否缺少某些东西,或者编译器不喜欢我的东西||? 我将范围缩小到dynamic导致问题的范围(options在我上面的代码中是一个动态变量)。问题仍然存在,我为什么不能这样做? 该代码无法编译: internal class Program { #region Static Methods private static void Main(string[] args) { …

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.