.net-4.0

Different behaviour when collection modified between Dictionary and ConcurrentDictionary

风格不统一 提交于 2019-12-08 00:44:18
问题 With a normal Dictionary code as list below, I get exception that Collection was modified; enumeration operation may not execute. Dictionary<int, int> dict2 = new Dictionary<int, int>(); dict2.Add(1, 10); dict2.Add(2, 20); dict2.Add(3, 30); dict2.Add(4, 40); foreach (var d in dict2) { if (dict2.ContainsKey(2)) dict2.Remove(2); if (dict2.ContainsKey(3)) dict2.Remove(3); } However with ConcurrentDictionary, this works fine. ConcurrentDictionary<int, int> dict1 = new ConcurrentDictionary<int,

Application.GetContentStream returns null for content Uri

只谈情不闲聊 提交于 2019-12-08 00:31:07
问题 I have file "sample.xml" in my project set as content. I am running this code: Uri uri = new Uri("/sample.xml", UriKind.Relative); StreamResourceInfo contentStream = Application.GetContentStream(uri); Why it returns null contentStream? 回答1: It means probably that it does not find the file. Is the path correct?. Return Value Type: System.Windows.Resources.StreamResourceInfo A StreamResourceInfo that contains a content data file that is located at the specified Uri. If a loose resource is not

WinForm ComboBox add text “Select” after data binding

余生长醉 提交于 2019-12-07 23:44:52
问题 At my form, I have one control ComboBox. I want after databinding add text "Select". I try this cbOperatorList.DataSource = operatorService.GetOperatorList(); cbOperatorList.Items.Insert(0, "Select"); But when I do this. I get exception what Changing the collection of items is impossible if you set the property DataSource. UPDATE public BindingList<Operator> GetOperatorList(string filter = "") { return new BindingList<Operator>( this.operatorRepository.All.Where( item => item.FirtsName

How do you give a C# Auto-Property a default value using a custom attribute?

旧时模样 提交于 2019-12-07 22:42:44
问题 How do you give a C# Auto-Property a default value, using a custom attribute? This is the code I want to see: class Person { [MyDefault("William")] public string Name { get; set; } } I am aware that there is no built in method to initialize the default using an attribute - can I write my own custom class that uses my custom attributes to initialize the default? 回答1: You could use a helper class like that: public class DefaultValueHelper { public static void InitializeDefaultValues<T>(T obj) {

Have multiple calls wait on the same internal async task

让人想犯罪 __ 提交于 2019-12-07 22:34:23
问题 (Note: this is an over-simplified scenario to demonstrate my coding issue.) I have the following class interface: public class CustomerService { Task<IEnumerable<Customer>> FindCustomersInArea(String areaName); Task<Customer> GetCustomerByName(String name); : } This is the client-side of a RESTful API which loads a list of Customer objects from the server then exposes methods that allows client code to consume and work against that list. Both of these methods work against the internal list of

What is the fastest way for checking a big proxy list on a specific web site?

老子叫甜甜 提交于 2019-12-07 20:43:14
问题 I have a big list of proxy servers (txt file , Format = ip:port in each line) and wrote the code below for checking them: public static void MyChecker() { string[] lines = File.ReadAllLines(txtProxyListPath.Text); List<string> list_lines = new List<string>(lines); List<string> list_lines_RemovedDup = new List<string>(); HashSet<string> HS = new HashSet<string>(); int Duplicate_Count = 0; int badProxy = 0; int CheckedCount = 0; foreach (string line in list_lines) { string[] line_char = line

ASP.NET MVC : Using the same database for Entity Framework and ASP.NET Membership

人走茶凉 提交于 2019-12-07 20:14:26
Im trying to user ASP.NET MVC3 with Entity Framework and ASP.NET Membership for authentication. I've set up an existing database as my application services database for membership. When i create the entity data model through the wizard it adds the following connection string to my web.config. <add name="DBEntities" connectionString="metadata=res://*/Models.DB.csdl|res://*/Models.DB.ssdl|res://*/Models.DB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=PM\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data

Using x-www-form-urlencoded Content-Type in WCF

梦想的初衷 提交于 2019-12-07 20:03:51
问题 I'm writing a .NET client for Menalto Gallery 3, which uses a RESTful JSON API (see API docs). I decided to use WCF for my client and it looks like it could greatly simplify my work, were it not for the fact that there's one method that requires a Content-Type of application/x-www-form-urlencoded instead of application/json . I have seen various hacks to send urlencoded data from WCF, for example by using a Stream parameter which enables me to send arbitrary data, but still requires an

Deploying Office 2010 addin

核能气质少年 提交于 2019-12-07 19:12:28
问题 I've created two addins, one is shared addin (for Excel, Word and Outlook) and another one is for Outlook. They work fine on my machine (where I developed it). Framework used: 4.0 I've created a 'setup project' throught VS2010 following steps in Deploying an Office Solution by Using Windows Installer. When I run setup of it, shared addin runs fine. But the outlook addin does not work. This is log of 'Event Viewer' Event Type: Error Event Source: VSTO 4.0 Event Category: None Event ID: 4096

How do I output console to multiple streams at once in C#?

↘锁芯ラ 提交于 2019-12-07 18:51:26
问题 I have a program that takes the Console output and writes it to a logfile, however it no longer shows up in the console window. Is there a way to keep it in the window but write it to the log file as well? Update: appLogStream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read); TextWriter logtxtWriter = Console.Out; logstrmWriter = new StreamWriter(appLogStream); if(!console) Console.SetOut(logstrmWriter); logstrmWriter.AutoFlush = true; Console.WriteLine("Started at