通过单独的任务更新BindingSource中的元素

我有一个class,比如说人,有一个Id和一个名字。 该类正确实现了INotifyPropertyChanged 另外:有人要求上课人。 我真正的问题是一个更复杂的课程,我把它简化为一个相当简单的POCO,以确定它不是因为我的课程。 本来: public class Person { public int Id {get; set;} public string Name {get; set;} } 对于更新,它需要实现INofityChanged。 完整的代码就在这个问题的最后 StackOverflow:如何正确实现INotifyPropertyChanged 我有一个System.Windows.Forms.Form 此表单有一个BindingSource。 绑定源的DataSource属性设置为我的类Person 我有一个绑定到BindingSource的DataGridView 我已经为绑定源添加了几个Person实例 添加的人员被正确显示。 如果我以编程方式更改bindingsource中的Person,则会正确显示更改的值。 到现在为止还挺好。 如果在单独的线程中更改Person,则会出现问题。 我经常收到带有消息的InvalidOperationException BindingSource不能是自己的数据源。 不要将DataSource和DataMember属性设置为引用BindingSource的值。 我想这与更新是在一个等待的异步任务中完成的事实有关。 我知道在更新用户界面项之前,您应该检查InvokeRequired是否相应地采取行动。 private void OnGuiItemChanged() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(() => { OnGuiItemChanged(); })); } else { … // […]

C#null coalescing运算符等效于c ++

C#null合并运算符是否有C ++等价物? 我在代码中进行了太多的空检查。 所以正在寻找一种减少空代码量的方法。

.NETreflection – 如何从ParameterInfo中获取“真实”类型

我正在尝试validation参数是否为out参数并扩展接口(ICollection)。 reflectionapi似乎不想给我参数的“真实”类型,只有最后一个“&”的参数不能在IsAssignableFrom语句中正确评估。 我写了一些有效的c#代码,但似乎应该有更好的方法来做到这一点。 bool isCachedArg(ParameterInfo pInfo) { if (!pInfo.IsOut) return false; string typeName = pInfo.ParameterType.FullName; string nameNoAmpersand = typeName.Substring(0, typeName.Length – 1); Type realType = Type.GetType(nameNoAmpersand); if (!typeof(ICollection).IsAssignableFrom(realType)) return false; return true; } 有没有办法获得realType而无需从其字符串名称重新加载Type? 我还在使用.NET 2.1。 谢谢,兰迪

如何使用C#使用Selenium WebDriver滚动到元素

如何让Selenium WebDriver滚动到特定元素以在屏幕上显示它。 我尝试了很多不同的选择,但没有运气。 这不适用于c#绑定吗? 我可以跳转到特定位置ex ((IJavaScriptExecutor)Driver).ExecuteScript(“window.scrollTo(0, document.body.scrollHeight – 150)”); 但我希望能够将它发送到不同的元素,而不是每次都给出确切的位置。 public IWebElement Example { get { return Driver.FindElement(By.Id(“123456”)); } } Ex 1) ((IJavaScriptExecutor)Driver).ExecuteScript(“arguments[0].scrollIntoView(true);”, Example); 例2 ((IJavaScriptExecutor)Driver).ExecuteScript(“window.scrollBy(Example.Location.X”, “Example.Location.Y – 100)”); 当我观看它时,它不会将页面跳转到元素,并且exception与屏幕外的元素匹配。 更新:我添加了一个bool ex = Example.Exists(); 之后检查结果。 它确实存在(它是真的)。 它没有显示(因为它仍然在屏幕外,因为它没有移动到元素)它没有被选中?????? 有人看到成功By.ClassName。 有没有人知道在C#绑定中执行此操作是否有问题.Id?

关于收益率回报和对foreach的打破

有没有一种正确的方法来打破foreach,以便IEnumerable 知道我已经完成并且它应该清理。 请考虑以下代码: private static IEnumerable getPeople() { using (SqlConnection sqlConnection = new SqlConnection(“…”)) { try { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(“select id, firstName, lastName from people”, sqlConnection)) { using (SqlDataReader reader = sqlCommand.ExecuteReader()) { while (reader.Read()) yield return new Person(reader.GetGuid(0), reader.GetString(1), reader.GetString(2)); } } } finally { Console.WriteLine(“finally disposing of the connection”); […]

反序列化XML数组,其中Root是数组,元素不遵循约定

我得到的XML是由外部源提供的,所以我没有能力轻松地重新格式化它。 我想在我的实体上使用xml属性,而不必编写知道如何格式化XML和实体的linq查询。 这是一个例子: 2013-2 Spring 2013 2013-3 Summer 2013 Jun&Jul 我知道XMLSerializer需要ArrayOfTerm而不是TERMS,但是我可以调整我的实体以使用不同的元素名称和xml属性,例如: public class TermData { [XmlArray(“TERMS”)] [XmlArrayItem(“TERM”)] public List terms; } public class Term { [XmlElement(“ID”)] public string id; [XmlElement(“DESC”)] public string desc; } 我正在反序列化数据,如下所示: TermData data; XmlSerializer serializer = new XmlSerializer(typeof(TermData)); using (StringReader reader = new StringReader(xml)) { data = (TermData)serializer.Deserialize(reader); } return View(data.terms); […]

xml序列化 – 删除命名空间

我正在使用c#序列化一个对象。 我得到的结果如下所示 <Users 但我希望得到这种格式的结果。 这是我的代码 public class Users { [XmlArray(“Users”)] public List ListData { get; set; } public string GetXML() { System.IO.MemoryStream ms = new System.IO.MemoryStream(); XmlSerializer sr = new XmlSerializer(typeof(Users)); sr.Serialize(ms, this); ms.Position = 0; return System.Text.Encoding.UTF8.GetString(ms.ToArray()); } } public class User { [XmlAttribute(“Id”)] public Int64 UserId { get; set; } [XmlAttribute(“CreateDate”)] public string […]

C#`foreach`行为 – 澄清?

我在这里阅读了Eric关于foreach枚举的文章以及foreach可以工作的不同场景 为了防止旧的C#版本进行装箱,C#团队为foreach启用了鸭子类型,以便在非Ienumerable集合上运行。(返回具有公共MoveNext和Current属性的公共GetEnumerator就足够了(。 所以,埃里克写了一个样本 : class MyIntegers : IEnumerable { public class MyEnumerator : IEnumerator { private int index = 0; object IEnumerator.Current { return this.Current; } int Current { return index * index; } public bool MoveNext() { if (index > 10) return false; ++index; return true; } } public MyEnumerator GetEnumerator() { return new […]

“在读取XML数据时已超出最大字符串内容长度配额(8192)”将XML字符串发送到WCF时出错

我正在使用.NET,C#应用程序,该应用程序打算将一个长XML字符串发送到WCF服务方法以进行进一步操作。 当我的应用程序尝试在运行时将XML字符串发送到WCF服务时,我收到一条错误消息: “The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:strProdUserDataXML. The InnerException message was ‘There was an error deserializing the object of type System.String. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be […]

参数参数有什么不好?

我遇到的情况我认为只能通过使用ref参数来解决。 但是,这意味着当我只需要5%的ref参数提供的function时,将方法更改为始终接受ref参数。 这让我觉得“哇,疯了,必须找到另一种方式”。 我是傻瓜吗? ref参数可能导致哪些问题? 编辑 要求提供进一步的细节,我认为它们与我的要求完全无关,但我们走了。 我想要保存一个新实例(将使用以后可能使用的ID更新)或检索与某些逻辑匹配的现有实例并更新它,保存它然后更改新实例的引用以指向现有的。 代码可能会更清晰: protected override void BeforeSave(Log entity) { var newLog = entity; var existingLog = (from log in repository.All() where log.Stuff == newLog.Stuff && log.Id != newLog.Id select log).SingleOrDefault(); if (existingLog != null) { // update the time existingLog.SomeValue = entity.SomeValue; // remove the reference to the new […]