草庐IT

question_type

全部标签

使用 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# - "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不会自动

c# - 如何解决 '...is a ' type', which is not valid in the given context'? (C#)

以下代码会产生错误:Error:'CERas.CERAS'isa'type',whichisnotvalidinthegivencontext为什么会出现这个错误?usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWinApp_WMI2{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){

c# - 没有无参数构造函数的类型的 Activator.CreateInstance(Type)

这个问题在这里已经有了答案:CreatinginstanceoftypewithoutdefaultconstructorinC#usingreflection(4个答案)关闭9年前。阅读工作中的现有代码,我想知道这怎么能行得通。我在程序集中定义了一个类:[Serializable]publicclassA{privatereadonlystring_name;privateA(stringname){_name=name;}}在另一个程序集中:publicvoidf(Typet){objecto=Activator.CreateInstance(t);}和那个简单的调用f(typeo

c# - 如何解决错误 :the type does not appear to implement microsoft. practices.servicelocation.iservicelocator?

我是MVC的新手,我正在关注“AdamFreeman的PROASP.NETMVC4”。我目前正在研究它的第6章。我正在学习如何使用MVC4中的Ninject进行依赖注入(inject)。我已经按照书中的描述创建了应用程序。现在我不明白为什么会出现以下错误:该类型似乎没有实现microsoft.practices.servicelocation.iservicelocator这是我的Controller代码:publicclassHomeController:Controller{privateProduct[]products={newProduct{Name="Kayak",Cate

c# - 错误 : Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'

我正在尝试使用该控件从.ascx设置我的.ascx控件的属性。所以在我的一个包含此控件的.aspx中,我有以下代码试图设置我的嵌入式.ascx的ItemsList属性:Itemitem=GetItem(itemID);myUsercontrol.ItemList=newList().Add(item);我尝试设置的.ascx中的属性如下所示:publicListItemsList{get{returnthis.itemsList;}set{this.itemsList=value;}}错误:无法将类型“void”隐式转换为“System.Collections.Generic.List

c# - x :Type not found in user control library

我正在尝试在WPF用户控件库项目中创建一个ResourceDictionary。当我添加以下样式时:我收到一条错误消息:Thetype'x:Type'wasnotfound.Verifythatyouarenotmissinganassemblyreferenceandthatallreferencedassemblieshavebeenbuilt.我将x声明为:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"当我在WPF应用程序项目中而不是在UserControl库项目中创建资源字典时,这会起作用。知道为什么吗?

C# 泛型 : Simplify type signature

这个问题在这里已经有了答案:GenericswithGenericParametersandAbstractclass(4个答案)关闭9年前。如果我有一个看起来像这样的通用Item类:abstractclassItem{}还有一个看起来像这样的项目容器:classContainerwhereTItem:Item{}既然TItem依赖于T,是否可以简化Container的类型签名,使其只接受一个类型参数?我真正想要的是这样的:classContainerwhereTItem:Item//thisdoesn'tactuallywork,becauseItemtakesatypeparame

c# - Entity Framework 表拆分 : not in the same type hierarchy/do not have a valid one to one foreign key relationship

我正在使用EntityFramework6和代码优先方法,我希望将两个实体放在同一个表中。我做错了什么?[Table("Review")]publicclassReview{publicintId{get;set;}publicPictureInfoPictureInfo{get;set;}publicintPictureInfoId{get;set;}}[Table("Review")]publicclassPictureInfo{[Key,ForeignKey("Review")]publicintReviewId{get;set;}publicReviewReview{get;s