Tag: .net

如何重用具有略微不同的ProcessStartInfo实例的Process实例?

我有以下代码启动robocopy作为Process 。 我还需要进行数据库查询以确定每次调用robocopy我需要复制哪些目录,因此我使用ProcessStartInfo来控制传递的参数。 internal class Program { private static void Main(string[] args) { using (var context = new MyDbContext()) { IEnumerable processInfos = GetProcessInfos(context, args[0]); foreach (ProcessStartInfo processInfo in processInfos) { // How can I reuse robocopy Process instances and // how can I dispose of them properly? Process.Start(processInfo); } } } private static IEnumerable […]

将Class转换为XML为string

我正在使用XMLSerializer将类序列化为XML。 有很多例子可以将XML保存到文件中。 但是我想要的是将XML放入字符串而不是将其保存到文件中。 我正在尝试下面的代码,但它不起作用: public static void Main(string[] args) { XmlSerializer ser = new XmlSerializer(typeof(TestClass)); MemoryStream m = new MemoryStream(); ser.Serialize(m, new TestClass()); string xml = new StreamReader(m).ReadToEnd(); Console.WriteLine(xml); Console.ReadLine(); } public class TestClass { public int Legs = 4; public int NoOfKills = 100; } 有想法该怎么解决这个吗 ? 谢谢。

ApiController扩展方法 – 无法访问ResponseMessage

我想为ApiController创建扩展方法,以便能够返回自定义内容。 我的想法是用我自己的细节返回自定义错误。 我想返回类似于OAuthAuthorizationServerProvider返回的错误的自定义错误: { “error”: “invalid_grant”, “error_description”: “You have 3 more attempts before Your account will be locked.” } 在我的ApiController里面我添加了这个方法: public IHttpActionResult Test() { HttpError err = new HttpError(); err[“error”] = “40001”; err[“error_description”] = “Something is wrong”; var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, err); return ResponseMessage(response); } 这让我看起来很好看: { “error”: “40001”, “error_description”: “Somethis is wrong” } 我尝试将此转换为以下扩展方法: […]

等待IO的可重用测试代码

我正在尝试在WCF公开的方法/服务上使用async / await。 一切正常,但我想模拟实际等待IO的服务方法,以便服务调用将注册到IO完成端口,并将线程放回线程池。 为了澄清,我只是在尝试确认IO完成端口的使用,并更好地理解实际发生的机制。 所以我的测试服务目前看起来像这样: [ServiceContract] public interface IHelloWorldService { [OperationContract] Task SayHello(string firstName, string lastName); } public class HelloWorldService : IHelloWorldService { public async Task SayHello(string firstName, string lastName) { string str = string.Format(“Hello {0} {1}”, firstName, lastName); return await Task.Factory.StartNew(() => str); } } 我想在SayHello()中做一些事情来使代码等待一些IO,理想情况下我可以复制/粘贴的代码模式通常在我想模拟等待IO时使用。 通常,Thread.Sleep()用于模拟长时间运行的任务,但我很确定这会使线程池线程进入hibernate状态而不会触发IO完成端口的使用。

如何获取另一个线程的ThreadStatic值?

是否可以通过Thread引用获取该线程的ThreadStatic值?

Visual Studiounit testing安全例外

我正在开发一个监视打印队列的应用程序。 要做到这一点,它需要能够管理它们。 当我运行它进行调试时,应用程序运行正常,但是,当我尝试对类进行unit testing时,会抛出安全exception。 有没有办法让visual studiounit testing具有更高的安全级别? PrintQueue q = new PrintQueue(server, QueueName, PrintSystemDesiredAccess.AdministratePrinter); 抛出exception:尝试通过安全透明方法’MS.Internal.PrintWin32Thunk.PrinterDefaults.Dispose(Boolean)’来访问安全关键方法’MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDispose(Boolean)’失败。 同样,在unit testing之外使用时,不会抛出任何exception并且类按预期工作。 我曾尝试以管理员身份运行visual studio,但仍然收到exception。 更新:看起来我将无法测试这个类。 包含它的类库已经用.NET 3.5编写,这就是使用该类时不会发生exception的原因。 我做了一些进一步的实验,发现虽然这个类库以.NET 3.5为目标,但是抛出exception的类不能被面向.NET 4.0的应用程序使用。 Visual Studio测试项目需要以.NET 4.0为目标。

在ASP.NET中创建唯一的URL

在我的网站中,我需要创建一个管理员用户将其发送给一组用户的唯一URL。 只要管理员创建新表单,就会创建唯一的URL。 我知道我可以使用guid来表示唯一的URL,但我正在寻找更短的东西(希望大约4个字符,因为它更容易记住)。 如何在ASP.NET中生成一个如下所示的唯一URL: http://mydomain.com/ABCD 据我所知,一些URL缩短网站(如bit.ly)使用非常短的唯一URL做了类似的事情。 有可用的算法吗?

将数据从模型传递到自定义validation类

我有一个数据validation类,用于检查会议的开始日期是否在结束日期之前。 该模型会自动传递需要validation的日期,但是我在传递需要validation的数据时遇到了一些困难。 这是我的validation课程 sealed public class StartLessThanEndAttribute : ValidationAttribute { public DateTime DateEnd { get; set; } public override bool IsValid(object value) { DateTime end = DateEnd; DateTime date = (DateTime)value; return (date < end); } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name); } } 这是包含数据注释的类 [StartLessThanEnd(ErrorMessage=”Start Date must be before the […]

阻止ButtoninheritanceParent的BackColor

当我有一个父控件有一个BackColor而不是SystemColors.Control ,但我有那个父控件上的按钮,我想在系统中绘制它们。 但是,当我不更改按钮的BackColor时,它会以父颜色绘制。 当我将按钮的BackColor更改为SystemColors.Control ,它不再在Windows主题中绘制。 左侧版本使用SystemColors.Control ,右侧版本不更改BackColor 。 炸了,看起来像这样。 在这里,您可以看到按钮具有坚实的背景。 有什么建议我可以解决这个问题吗? 可以通过创建新的.NET 2.0 WinForms项目并将Form1的构造函数更改为以下内容来实现图像中的效果: public Form1() { InitializeComponent(); var textBox = new TextBox(); Controls.Add(textBox); var button = new Button { Text = “L”, Width = 23, Height = 18, Left = -1, Top = -1 }; textBox.Controls.Add(button); // Disable the line below to get the […]

如何通过reflection获取透明代理的属性值?

我的代码接收透明代理而不是原始实例。 虽然这个var type = obj.GetType(); 产生原始类的类型,以下代码抛出TargetException : 对象与目标类型不匹配 var value = property.GetValue(obj, null); 其中property是type.GetProperties()