草庐IT

has_content_type

全部标签

c# - 将 List<object> 转换为 List<Type>,类型在运行时已知

我正在实现某种反序列化并遇到下一个问题:我有List和System.Reflection.Field,它是FieldType可以是List,List或List,所以我需要从List转换到那种类型。publicstaticobjectConvertList(Listvalue,Typetype){//typemaybeList,List,List}我可以单独写每个案例,但应该有更好的方法使用反射。 最佳答案 我相信你想要的是:publicstaticobjectConvertList(Listvalue,Typetype){varco

c# - NaoQi 和 Leap 问题 : An unhandled exception of type 'System.BadImageFormatException' . .. 并且无法加载文件或程序集

目前,我遇到了一个问题,我无法通过Google上的解决方案解决这个问题。到目前为止没有任何效果。当我编译LeapMotion代码并导入NaoqiC#库时,它工作正常。当我实际尝试使用一行代码连接到机器人时,我的程序崩溃了。我的程序设置为为任何CPU构建。.dll被Leap库和NaoQi库引用,并且.dll被放置在调试文件夹和项目文件夹中以备不时之需。目前我还不知道问题出在哪里。这里有人能弄清楚吗?我会很高兴让这个工作。这是第一条错误信息,然后它要求我中断或继续:Anunhandledexceptionoftype'System.BadImageFormatException'occur

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# - 获取 Content-Disposition 参数

如何使用WebClient获取从WebAPIController返回的Content-Disposition参数?WebAPIController[Route("api/mycontroller/GetFile/{fileId}")]publicHttpResponseMessageGetFile(intfileId){try{varfile=GetSomeFile(fileId)HttpResponseMessageresponse=newHttpResponseMessage(HttpStatusCode.OK);response.Content=newStreamContent(

c# - "An assembly with the same simple name has already been imported"没有重复引用的错误

我收到以下错误:errorCS1704:Anassemblywiththesamesimplename'Interop.xxx.dll,Version=1.0.0.0,Culture=neutral,PublicKeyToken=nullhasalreadybeenimported.Tryremovingoneofthereferencesorsignthemtoenableside-by-side.我所看到的一切都表明我引用了两个同名的程序集,我需要删除其中一个。但是,我已经检查过并且只引用了一次。这也仅在我使用msbuild从我的开发箱上的命令行构建时发生。如果我通过VisualS

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# - 如何修复 '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'有没有办法通过分配默认值或其他方法来避免应