草庐IT

C# 验证 : IDataErrorInfo without hard-coded strings of property name?

实现IDataErrorInfo的最佳做法是什么?无论如何都可以在没有属性名称硬编码字符串的情况下实现它? 最佳答案 通用验证例程的基类您可以使用DataAnnotations如果您在IDataErrorInfo实现中做了一些futzing。例如,这是我经常使用的基本View模型(来自Windows窗体,但您可以推断):publicclassViewModelBase:IDataErrorInfo,INotifyPropertyChanged{publiceventPropertyChangedEventHandlerPropert

c# - 查询 List<T> 或数据库更快吗?

我最近有几种情况需要同一张表中的不同数据。一个例子是我将遍历每个“送货司机”并为他们要送货的每个客户生成一个可打印的PDF文件。在这种情况下,我把所有的customer拉过来,存入ListAllCustomersList=customers.GetAllCustomers();当我遍历送货司机时,我会做这样的事情:ListDeliveryCustomers=AllCustomersList.Where(a=>a.DeliveryDriverID==DriverID);我的问题:每次查询与送货司机相关的客户记录时,我通过查询List对象的方式是否比查询数据库更快?

c# - 为什么这段代码会提示 "the arity of the generic type definition"?

我有一个通用类型:classDictionaryComparer:IEqualityComparer>还有一个工厂方法,它将(应该)为给定的字典类型创建此类的实例。privatestaticIEqualityComparerCreateDictionaryComparer(){Typedef=typeof(DictionaryComparer);Debug.Assert(typeof(T).IsGenericType);Debug.Assert(typeof(T).GetGenericArguments().Length==2);Typet=def.MakeGenericType(ty

c# - 将 null 添加到 List<bool?> 作为 IList 抛出异常

使用.NET3.5和C#3.0,IListlist=newList();list.Add(null);这会抛出一个ArgumentException,感觉不对。Listlist=newList();list.Add(null);完美运行。那么这是Microsoft代码中的错误吗?如何在现实生活中产生这种错误的例子:newJavaScriptSerializer().Deserialize>("[true,false,null]"); 最佳答案 是的,是的。参见http://social.msdn.microsoft.com/Foru

c# - List<T> 没有实现 SyncRoot!

每个人都使用很多列表。我需要遍历这个列表,所以我使用已知的SyncRoot模式。最近我注意到this发布应该避免使用SyncRoot以支持“嵌入式”线程安全(每个方法将锁定一个私有(private)对象而不使用SyncRoot属性公开它)。我能理解,部分同意。问题是List类不实现SyncRoot属性,即使实现了ICollection接口(interface),它公开了SyncRoot属性。我说这会破坏代码Listlist=newList()list.SyncRoot;给我以下编译器错误:errorCS0117:'System.Collections.Generic.List'does

c# - 编辑 List<Tuple> 项

用List你可以用这个简单地编辑一个项目:varindex=List.FindIndex(s=>s.Number==box.Text);List[index]=newString;但是,如何将它应用于List>例如? 最佳答案 您可以用类似的方式找到索引-通过对列表中的每个元组应用条件:varindex=listOfTuples.FindIndex(t=>t.Item1==box1.Text||t.Item2==box2.Text);您可以通过调用Tuple.Create来替换一个项目:listOfTuples[index]=Tup

c# - WPF 数据绑定(bind) : enable/disable a control based on content of var?

我的表单上有一个按钮,只有在TreeView(或tabitem中的ListView)中选择了一个项目时才应启用该按钮。选择一个项目时,它的值存储在一个字符串成员变量中。我可以将按钮的IsEnabled属性绑定(bind)到成员var的内容吗?也就是说,如果成员var不为空,则启用该按钮。同样,当成员变量的内容发生变化(设置或清除)时,按钮的状态也应发生变化。 最佳答案 由于您可能希望根据字符串绑定(bind)按钮的IsEnabled属性,请尝试为它创建一个转换器。即...和转换器:[ValueConversion(typeof(st

c# - C# : generate a list of two dimension arrays with the same shape 中的 FsCheck

假设我正在编写一些视频分析代码。这是视频类的简化版本:publicclassVideo{publicreadonlyintWidth;publicreadonlyintHeight;publicreadonlyListFrames;publicVideo(intwidth,intheight,IEnumerableframes){Width=width;Height=height;Frames=newList();foreach(varframeinframes){if(frame.GetLength(0)!=height||frame.GetLength(1)!=width){thr

c# - ActionResult<IEnumerable<T>> 必须返回一个 List<T>

使用ASP.NETCore2.1获取以下代码:[HttpGet("/unresolved")]publicasyncTask>>GetUnresolvedIdentities(){varresults=await_identities.GetUnresolvedIdentities().ConfigureAwait(false);returnresults.ToList();}自从GetUnresolvedIdentities()我就想到了返回IEnumerable我可以返回returnawait_identities.GetUnresolvedIdentities().Configu

c# - AllowUserToAddRows 不适用于 DataGridView 上的 List<> 数据源

我有一个DataGridView与DataSource设置为List但是,当我设置AllowUserToAddRows时,新行指示器不显示至true,当我设置DataSource至BindingList,这似乎解决了问题。问:应该替换我的List与BindingList或者有更好的解决方案? 最佳答案 是否myClass有一个公共(public)的无参数构造函数?如果不是,您可以从BindingList派生并覆盖AddNewCore调用您的自定义构造函数。(edit)或者-只需将您的列表包装在BindingSource中它可能会起作