Tag: c# 4.0

如何测试AccessViolationException的处理

我需要编写一个测试来validation我的代码可以处理AccessViolationException(或任何其他WIN32损坏状态exception – CSE),它通常通过调用第三方库来处理不安全的上下文。 这应该都是使用C#on .net 4.0完成的。 我发现了这个相关的问题如何处理AccessViolationException和这篇相关的文章http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx ,它解释了如何捕获这些CSE和他们的背景。 所以我想在测试中激发一个WIN32 CSE,以确保在我的应用程序中正确处理。 就像是: 一些要测试的示例类: public class MyExceptionHandler { [HandleProcessCorruptedStateExceptions] public void HandleCorruptedStateException() { try { //Force genuine unsafe AccessViolationException //not just a throw new AccessViolationException } catch(Exception e) { //Log/cleanup/other } } public void DoesNotHandleCorruptedStateException() { try { //Force genuine unsafe AccessViolationException //not just a throw new […]

处理类中全局变量的方法

有这样的PHP代码,(这里的PHP函数的一部分)将其转换为C#。 function jaktDate2() { Global $nameofselectbox,$startYear,$endYear,$year, $startDate,$endDate,$startMounth,$endMounth,$startDay,$endDay; $today = getdate(); $year=$today[‘year’]; $mounth=$today[‘mon’]; $day=$today[‘mday’]; 我的C#代码试试这个, public class HuntingDate { public string StartYear; public string EndYear; public string Year; public DateTime StartDate; public DateTime EndDate; public string StartMonth; public string EndMonth; public DateTime StartDay; public DateTime EndDay; } 我是这样开始的..这样的方式是否正确? 我接下来该怎么办?

构建JSON配置节

有没有办法让配置部分用JSON而不是XML编写? 假设我有以下ConfigurationSection : public class UsersConfig : ConfigurationSection { [ConfigurationProperty(“users”, IsRequired = false)] public UserCollection Users { get { return this[“users”] as UserCollection; } } } [ConfigurationCollection(typeof(UserElement), AddItemName = “user”] public class UsersCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new UserElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((UserElement)element).Name; } } […]

打破内部foreach循环并继续外部foreach循环

如果我有一个嵌套的foreach循环,我怎么做打破内循环并告诉外部继续在那一点没有在内循环下面做任何其他代码? foreach(var item in items) { foreach(var otheritem in otheritems) { if (!double.TryParse(otheritem)) { //break inner loop //continue outer loop so we never get to DoStuff() } } DoStuff(); }

Contract.Requires抛出pex错误

可能重复: 如何配置Pex以尊重代码合同? 目前,当我运行pex探索时,我在我的类中创建的代码契约被视为pex探索结果中的错误。 我认为当你使用代码合同进行pex勘探时,合同失败应该被视为预期的行为。 这是导致exception的代码。 测试方法: [PexMethod] public void TestEquality(Guid userId, string username, string password, string securityQuestion, string securityAnswer) { UserSecurity user = UserTools.CreateUser(Guid.NewGuid(), username, password, securityQuestion, securityAnswer); bool passwordResult = UserTools.VerifyInput(password, user.Password, user.PasswordSalt); bool securityAnswerResult = UserTools.VerifyInput(securityAnswer, user.SecurityAnswer, user.SecurityAnswerSalt); Assert.IsTrue(passwordResult, “Password did not correctly re-hash”); Assert.IsTrue(securityAnswerResult, “Security Answer did not correctly re-hash”); } 失败的方法调用: […]

通过GMail和.NET 4发送电子邮件

.NET 4在发送电子邮件时有任何问题吗? 我有一个.NET 3.5解决方案,它正在工作,然后将解决方案迁移到.NET 4,它不起作用; 没有改变! 注意:我得到这个例外: System.Net.Mail.SmtpException:SMTP服务器需要安全连接或客户端未经过身份validation。 服务器响应为:5.5.1需要身份validation。 在System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response)了解更多信息 在System.Net.Mail.MailCommand.Send(SmtpConnection conn,Byte []命令,字符串来自) 在System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection recipients,String deliveryNotify,SmtpFailedRecipientException&exception) 在System.Net.Mail.SmtpClient.Send(MailMessage消息) 在 … 这是我的代码: public static void SendEmail( this Email email) { if (email.MailingSettings == null) throw new ArgumentNullException(“email.MailingSettings”, “specify email.MailingSettings”); var settings = email.MailingSettings; if (string.IsNullOrEmpty(email.Sender)) throw new ArgumentException(“email.Sender”, “specify a sender”); if (email.Recipients == […]

将数据库连接字符串放在.resx文件中

我们可以将数据库连接字符串放在C#类库项目类型的.resx文件中吗? 如果是这样,有什么优缺点?

如何创建可点击标签

我想在表单应用程序中创建一个随机可点击标签。 我随机生成了一个标签,但我无法点击它。 有谁能够帮我?

C# – .NET 4.0 – 该程序集不允许部分信任的调用者

从网络共享运行时,我的应用程序抛出以下exception: 该程序集不允许部分信任的调用者。 我的应用引用了两个DLL文件: BitFactory.Logging.dll FileHelpers.dll 我不确定它有哪些问题。 AllowPartiallyTrustedCallersAttribute :读取它,但我没有任何DLL文件的源,所以我无法将属性添加到这些DLL文件。 CASPOL.EXE :使用一些变体添加我的网络共享,例如caspol -machine -addgroup 1. -url \\netserver\netshare\* LocalIntranet似乎没有任何影响。 我曾经使用过.NET 3.5的CASPOL hack,但是,它现在似乎不适用于.net 4.0。 任何人都可以建议如何绕过这个“部分受信任的来电者”检查? 谢谢。

C# – user32.dll – GetWindowRect问题

[DllImport(“user32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } foreach (Process […]