草庐IT

c# - 公开课 - "is inaccessible due to its protection level. Only public types can be processed."

我正在做一个测试项目来了解对象的XML序列化,但我遇到了一个奇怪的运行时错误:namespaceSerializeTest{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidserializeConnection(Connconnection){XmlSerializerserializer=newXmlSerializer(typeof(Conn));TextWritertextWrit

c# - 为什么 typeA == typeB 比 type == typeof(Type B) 慢?

我最近一直在优化/对一些代码进行基准测试并遇到了这个方法:publicvoidSomeMethod(TypemessageType){if(messageType==typeof(BroadcastMessage)){//...}elseif(messageType==typeof(DirectMessage)){//...}elseif(messageType==typeof(ClientListRequest)){//...}}这是从其他地方的性能关键循环调用的,所以我很自然地假设所有这些typeof(...)调用都增加了不必要的开销(我知道这是一种微优化)并且可以移动到类中的私有

c# - 简易喷油器 : Registering a type with constructor argument that's based on its parent

我目前正在从我的项目中删除Ninject,并转而使用SimpleInjector,但有一件事我无法正常工作。对于我的日志记录,在注册服务时,我以前能够将参数传递到我的日志记录类中_kernel.Bind().To().WithConstructorArgument("name",x=>x.Request.ParentContext.Request.Service.FullName);我正在寻找一种在SimpleInjector中重新创建它的方法。到目前为止,除了这个,我还有其他所有工作。通过执行以下操作,我可以使日志记录正常工作,尽管没有显示正确的记录器名称:_container.Re

c# - 使用 LINQ 创建字典并避免 "item with the same key has already been added"错误

我想在字典中找到一个键,如果找到则替换值,如果找不到则添加键/值。代码:publicclassMyObject{publicstringUniqueKey{get;set;}publicstringField1{get;set;}publicstringField2{get;set;}}LINQ解决方案(抛出已添加具有相同键的项目。):DictionaryobjectDict=csvEntries.ToDictionary(csvEntry=>csvEntry.ToMyObject().UniqueKey,csvEntry=>csvEntry.ToMyObject());ForEach

c# - IDbSet.Add 和 DbEntityEntry.State = EntityState.Added 有什么区别?

在EF4.1+中,这两行代码之间有区别吗?dbContext.SomeEntitySet.Add(entityInstance);dbContext.Entry(entityInstance).State=EntityState.Added;或者他们做同样的事情?我想知道一个是否会以不同于另一个的方式影响子集合/导航属性。 最佳答案 当您使用dbContext.SomeEntitySet.Add(entityInstance);时,此及其所有相关实体/集合的状态设置为已添加,而dbContext.Entry(entityInstan

c# - 如何修复 'T' 是 'type parameter' 但被用作 'variable' 编译错误

我需要检查泛型类型参数T是MyEntity还是它的子类。下面的代码会导致这个编译器错误:'T'isa'typeparameter'butisusedlikea'variable'如何修复?publicclassMyEntity{}staticvoidTest(){//Error34'T'isa'typeparameter'butisusedlikea'variable'if(TisMyEntity){}} 最佳答案 您可以使用IsAssignableFromType上的方法检查是否有一个Type可以分配给另一个。if(typeof(

使用 Json.Net : Error converting value to type 的 C# 枚举反序列化

我正在使用Json.NET序列化/反序列化一些JSONAPI。API响应有一些整数值映射到应用程序中定义的枚举。枚举是这样的:publicenumMyEnum{Type1,Type2,Type3}并且JSONAPI响应具有以下内容:{"Name":"abc","MyEnumValue":"Type1"}有时,API会为我的枚举中未定义的MyEnumValue字段返回一个值,如下所示:{"Name":"abc","MyEnumValue":"Type4"}抛出异常:Errorconvertingvalue"Type4"totype'MyEnum'有没有办法通过分配默认值或其他方法来避免应

c# - 通用扩展方法 : Type argument cannot be inferred from the usage

我正在尝试创建一个适用于类型化数据表的通用扩展方法:publicstaticclassExtensions{publicstaticTableTypeDoSomething(thisTableTypetable,paramExpression>[]predicates)whereTableType:TypedTableBasewhereRowType:DataRow{//dosomethingtoeachrowofthetablewheretherowmatchesthepredicatesreturntable;}[STAThread]publicstaticvoidmain(){M

c# - EF4 代码优先 : how to add a relationship without adding a navigation property

我应该如何在不使用任何导航属性的情况下使用CodeFirst来定义关系?之前,我通过在关系的两端使用导航属性来定义一对多和多对多。并在数据库中创建适当的关系。这是类外观的精简版本(为简单起见,我已将多对多关系转换为一对多)。publicclassUser{publicstringUserId{get;set;}publicstringPasswordHash{get;set;}publicboolIsDisabled{get;set;}publicDateTimeAccessExpiryDate{get;set;}publicboolMustChangePassword{get;set

c# - "Possible multiple enumeration of IEnumerable"与 "Parameter can be declared with base type"

在Resharper5中,以下代码导致list出现警告“Parametercanbedeclaredwithbasetype”:publicvoidDoSomething(Listlist){if(list.Any()){//...}foreach(variteminlist){//...}}在Resharper6中,情况并非如此。但是,如果我将方法更改为以下内容,我仍然会收到该警告:publicvoidDoSomething(Listlist){foreach(variteminlist){//...}}原因是,在这个版本中,list只枚举一次,所以改成IEnumerable不会自动