草庐IT

C# 编译器错误?用于 Expression 中只写属性的对象初始值设定项语法使 csc 崩溃

您可能认为这是一个错误报告,但我很好奇我在这里是否大错特错,或者Eric或Microsoft的其他人是否有解释。更新现在是postedasabug在MicrosoftConnect上。描述考虑以下类:classA{publicobjectB{set{}}}在这里,A.B是一个只写但其他方面都很好的属性。现在,假设我们在表达式内部赋值:Expression>expr=()=>newA{B=newobject{}};此代码使C#编译器(3.5.30729.4926和4.0.30319.1)吐出InternalCompilerError(0xc0000005ataddress013E213F

c# - 是否可以将 c# 对象初始值设定项与工厂方法一起使用?

我有一个带有静态工厂方法的类。我想调用工厂来检索类的实例,然后进行额外的初始化,最好通过c#对象初始化语法:MyClassinstance=MyClass.FactoryCreate(){someProperty=someValue;}对比MyClassinstance=MyClass.FactoryCreate();instance.someProperty=someValue; 最佳答案 没有。或者,您可以接受lambda作为参数,这也使您可以完全控制将调用“创建”过程的哪一部分。这样你就可以这样调用它:MyClassinsta

c# - 'CrystalDecisions.CrystalReports.Engine.ReportDocument' 的类型初始值设定项抛出异常

我正在为使用CrystalReportsAPI显示和打印表单的客户部署桌面应用程序。我正在使用InstallShield2012构建安装程序。我还包括了.NET4.0Framework以及所有Crystal程序集。我有2个(有点相关的)问题:1)每当我试图在客户端机器上运行应用程序时,我都会收到以下错误:Thetypeinitializerfor'CrystalDecisions.CrystalReports.Engine.ReportDocument'threwanexception.我不知道我在这里忘记了什么。CrystalDecisions.CrystalReports.Engi

c# - LINQ to Entities 消息中仅支持无参数构造函数和初始值设定项

我有一个从EF模型返回数据的方法。我收到上述消息,但我不知道如何规避该问题。publicstaticIEnumerableGetFundedCount(){vartoday=DateTime.Now;vardaysInMonth=DateTime.DaysInMonth(today.Year,today.Month);varday1=DateTime.Now.AddDays(-1);varday31=DateTime.Now.AddDays(-31);using(varuow=newUnitOfWork(ConnectionString.PaydayLenders)){varr=new

c# - 为什么集合初始值设定项表达式需要实现 IEnumerable?

为什么会产生编译错误:classX{publicvoidAdd(stringstr){Console.WriteLine(str);}}staticclassProgram{staticvoidMain(){//errorCS1922:Cannotinitializetype'X'withacollectioninitializer//becauseitdoesnotimplement'System.Collections.IEnumerable'varx=newX{"string"};}}但这不是:classX:IEnumerable{publicvoidAdd(stringstr)

c# - 使用对象初始值设定项时,为什么编译器会生成一个额外的局部变量?

昨天在回答关于SO的问题时,我注意到如果使用对象初始化器初始化对象,编译器会创建一个额外的局部变量。考虑以下C#3.0代码,在VS2008中以Release模式编译:publicclassClass1{publicstringFoo{get;set;}}publicclassClass2{publicstringFoo{get;set;}}publicclassTestHarness{staticvoidMain(string[]args){Class1class1=newClass1();class1.Foo="fooBar";Class2class2=newClass2{Foo="

c# - 无法使用集合初始值设定项初始化类型 '',因为它未实现 'System.Collections.IEnumerable'

我创建了一个类,其中包含三个类作为属性:publicclassFeeds{publicRentalsRentals{get;set;}publicAgentAgents{get;set;}publicNorthwindService.ServiceReference1.FileFile{get;set;}}我是这样使用它的:varquery=fromrinent.Rentalsjoinainent.Agentsonr.ListingAgentIDequalsa.AgentIDselectnewFeeds{a.AgentID,a.Alias,a.Bio,a.Email,a.Fax,r.F

c# - C# 中的构造函数与对象初始值设定项优先级

我最近一直在学习C#中的对象初始化器,但现在我想知道当它与构造函数发生冲突时它是如何工作的。publicclassA{publicboolfoo{get;set;}publicA(){foo=true;}publicA(boolbar){foo=bar;}}当我尝试这个时会发生什么?publicclassB{privateAa=newA(){foo=false};privateAb=newA(true){foo=false};}构造函数中的默认值是否是让bool开始为真且可以更改的好方法?publicA(boolbar=true){foo=bar;} 最佳答

c# - 'System.Web.Http.GlobalConfiguration' 的类型初始值设定项抛出异常

我添加了一个新的WebAPI项目。我安装CorsPM>Install-PackageMicrosoft.AspNet.WebApi.Cors-Pre然后当我运行我的项目时,我得到这个错误:Thetypeinitializerfor'System.Web.Http.GlobalConfiguration'threwanexception.这是我内心的异常(exception):{"Attemptbymethod'System.Web.Http.GlobalConfiguration..cctor()'toaccessfield'System.Web.Http.GlobalConfigur

c# - 字段初始值设定项不能引用非静态字段、方法或属性

我有一个类,当我尝试在另一个类中使用它时,我收到以下错误。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;namespaceMySite{publicclassReminders{publicDictionaryTimeSpanText{get;set;}//WearesettingthedefaultvaluesusingtheCostructorpublicReminders(){TimeSpanText.Add(TimeSpan.Zero,"None");TimeSpanText.Add(newTimeSp