Tag: c#

将这两个正则表达式合二为一

我在C#中有以下内容: public static bool IsAlphaAndNumeric(string s) { return Regex.IsMatch(s, @”[a-zA-Z]+”) && Regex.IsMatch(s, @”\d+”); } 我想检查参数s包含至少一个字母字符和一个数字,我写了上面的方法来执行此操作。 但是有没有办法将两个正则表达式( “[a-zA-Z]+”和”\d+” )组合成一个?

如何从C#中的数组中获取随机值

可能重复: 访问列表中的随机项 我有一个数字数组,我想从这个数组中获取随机元素。 例如:{0,1,4,6,8,2}。 我想选择6并将此数字放在另一个数组中,新数组的值为{6,….}。 我使用random.next(0,array.length),但这给出了一个随机数的长度,我需要随机数组。 for (int i = 0; i < caminohormiga.Length; i++ ) { if (caminohormiga[i] == 0) { continue; } for (int j = 0; j < caminohormiga.Length; j++) { if (caminohormiga[j] == caminohormiga[i] && i != j) { caminohormiga[j] = 0; } } } for (int i = 0; i […]

在C#中使用sqlite进行SQL转义

我有一个文本字段,它打破了我的SQL语句。 我如何逃脱该领域的所有角色? 我在C#中使用带有http://sqlite.phxsoftware.com/的 sqlite

编写一个接受out参数的lambda或匿名函数

我的代码中定义了一个委托: public bool delegate CutoffDateDelegate( out DateTime cutoffDate ); 我想创建委托并使用lambda或匿名函数初始化,但这些都没有编译。 CutoffDateDelegate del1 = dt => { dt = DateTime.Now; return true; } CutoffDateDelegate del2 = delegate( out dt ) { dt = DateTime.Now; return true; } 有办法做到这一点吗?

如何将动态用作通用?

如何将动态作为通用使用? 这个 var x = something not strongly typed; callFunction(); 还有这个 dynamic x = something not strongly typed; callFunction(); 都产生这个错误 Error 1 The type or namespace name ‘x’ could not be found (are you missing a using directive or an assembly reference?) 我该怎么做才能使它足够合法用于 ?

inheritance自struct

我试着弄明白我的代码有什么问题。 我有这个代码: public struct MyStructA { public MyStructA(string str) { myString= str; } public string myString; } public struct MyStructB: MyStructA { public string myReversString; } 我得到这个错误: Error at compile time: Type ‘MyStructA’ in interface list is not an interface 我不明白为什么? .net不像类那样实现struct?

在同一个具有相同名称的合同中不能有两个操作(Async和Non)

当激活以下服务时,我得到以下exception(在相同的合同中不能有两个操作,具有相同的名称,方法ExecuteAsync和Execute)。 [ServiceContract] public interface IMyService { [OperationContract] byte[] Execute(MyRequest request); [OperationContract] Task ExecuteAsync(MyRequest request); } 我想如果您使用svcutil.exe来创建服务引用,这是有道理的,因为基于任务的操作是自动为您创建的。 但是,我不想添加服务引用,而只是使用标准的ChannelFactory来创建WCF通道。 如果不将异步方法重命名为其他方法,还有其他方法吗? 或者我必须在Task.Run中的客户端上包装sync方法吗?

entity framework不会在SQL Express(MDF)中保留数据

我正在使用Entity Framework开发一个应用程序并将数据存储在.mdf数据库中。 我的代码可以读取数据,显然它也可以保存,但显然只能。 它没有错误,而程序正在运行它就像保存数据一样,我可以保存一个对象,处理上下文,创建一个新的,然后当我搜索我的对象它就在那里! 但是当我查询数据库以查看存储的数据时,那里什么都没有。 如果我关闭应用程序并再次运行它,则所有数据都将消失。 这是我为测试而编写的示例代码: using (DatabaseEntities e = new DatabaseEntities()) { for (int i = 0; i < 50; i++) { User u = new User(); u.Nome = "User" + i.ToString(); e.AddToUser(u); } int c = e.SaveChanges(true); List us = e.User.Where(x => x.ID < 50).ToList(); foreach (User u in us) Console.WriteLine(“ID: ” […]

错误:此模板尝试加载组件程序集“Microsoft.VisualStudio.SmartDevice”

我安装了Visual Studio 2015,我正在尝试为Windows Phone 8.1创建一个测试应用程序。 当我创建一个新项目时,我收到以下消息: 有关如何解决这个问题的任何建议?

“清单XML签名无效”

操作系统:安装了.NET 4.5的Visual Studio Pro 2012的Windows 7 64位。 我在Visual Studios中使用了Publish选项,并确保我单击了Sign the clickOnce清单并签署程序集。 它仍然无法在另一台计算机上运行,​​并且说我没有有效的XML签名。 我已粘贴下面的错误消息。 我还读过: 如何移动ClickOnce部署包 , 我是否必须签署ClickOnce清单? 。 VS2012 .NET 4.0 Clickonce VSTO CryptographicException:无法为提供的签名算法和其他几个算法创建SignatureDescription 。 我需要能够在.NET 4.0上部署我的程序,并且我无法访问另一个版本的Visual Studio。 提前致谢! 完成错误如下: PLATFORM VERSION INFO Windows:5.1.2600.196608(Win32NT)公共语言运行时:2.0.50727.3603 System.Deployment.dll:2.0.50727.3053(netfxsp.050727-3000)mscorwks.dll:2.0.50727.3603(GDR.050727-3600)dfdll。 dll:2.0.50727.3053(netfxsp.050727-3000)dfshim.dll:4.0.31106.0(Main.031106-0000) 来源部署url:file:/// C:/Documents%20and%20Settings/Administrator/Desktop/EatonWizard.application 错误摘要以下是错误摘要,这些错误的详细信息将在后面的日志中列出。 *激活C:\ Documents and Settings \ Administrator \ Desktop \ EatonWizard.application导致exception。 检测到以下失败消息:+文件中的exception读取清单:/// C:/Documents%20and%20Settings/Administrator/Desktop/EatonWizard.application:清单可能无效或无法打开文件。 + Manifest XML签名无效。 无法为提供的签名算法创建+ […]