草庐IT

c# - 无法转换类型 : why is it necesssary to cast twice?

鉴于这个高度简化的例子:abstractclassAnimal{}classDog:Animal{publicvoidBark(){}}classCat:Animal{publicvoidMew(){}}classSoundRecorderwhereT:Animal{privatereadonlyT_animal;publicSoundRecorder(Tanimal){_animal=animal;}publicvoidRecordSound(stringfact){if(this._animalisDog){((Dog)this._animal).Bark();//Compiler

c# - .NET : Unable to cast object to interface it implements

我有一个类(TabControlH60),它既继承自基类(UserControl)又实现了一个接口(interface)(IFrameworkClient)。我使用.NETActivator类实例化该对象。使用返回的实例,我可以转换为UserControl基类,但不能转换为接口(interface)。我得到的异常在代码片段下方。如何转换到界面?objectobj=Activator.CreateInstance(objType);Type[]interfaces=obj.GetType().GetInterfaces();//containsIFrameworkClientm_Clie

c# - 系统.IO.FileNotFoundException : Could not load file or assembly 'X' or one of its dependencies when deploying the application

我在部署应用程序时遇到了一个奇怪的问题,该应用程序引用了一个用托管C++编写的程序集。我创建了一个程序集X,对其进行编译并在名为Starter的exe文件中引用它。Starter.exe在本地机器上正常启动。但是,当我将启动器调试文件夹的所有内容复制到虚拟机器并尝试在那里启动它时,它崩溃并出现以下异常:UnhandledException:System.IO.FileNotFoundException:Couldnotloadfileorassembly'X'oroneofitsdependencies.Thespecifiedmodulecouldnotbefound.这对我来说没有

c# - 将 It.IsAny<int>() 和 It.IsAny<int>() 的值传递给方法设置有什么区别

我正在使用Moq,并希望创建构建器类来创建具有预设合理默认值的模拟,这些默认值可以在测试设置期间根据需要覆盖。我采用的方法使用扩展方法,我在其中传递输入参数值和预期输出。这样做时,我看到在我看来语义等效的代码中有不同的行为:直接在设置中传递It.IsAny()与在设置中间接传递It.IsAny()的值。示例:publicinterfaceIFoo{boolBar(intvalue);boolBar2(intvalue);}publicclassFoo:IFoo{publicboolBar(intvalue){returnfalse;}publicboolBar2(intvalue){r

c# - 使用 Moq.It.IsAny 测试以某物开头的字符串

是否可以使用Moq来表示一个方法接受以“ABC”开头的字符串。举个例子:logger.Verify(x=>x.WriteData(Moq.It.IsAny().StartsWith("ABC")),Times.Exactly(3));那不会编译,但希望它能说明我的观点 最佳答案 尝试:logger.Verify(x=>x.WriteData(Moq.It.Is(str=>str.StartsWith("ABC"))),Times.Exactly(3));你可以看到It.Is的另一个例子://matchingFunc,lazyeval

c# - "This BackgroundWorker states that it doesn' t 报告进度。”- 为什么?

我是这个后台worker的新手我已经阅读了一些关于如何创建一个的文章这是它产生的privatevoidbackgroundWorker1_DoWork(objectsender,DoWorkEventArgse){Bitmapimgbox=newBitmap(pictureBox.Image);intimgHeight=imgbox.Height;intimgWidth=imgbox.Width;intcounter=1;MinMaxWidth=imgWidth-50;MaxWidth=imgWidth;try{Colorc;//Colorc2;for(inti=0;i但是当我开始Do

c# - VS 2017 : The security debugging option is set but it requires the Visual Studio hosting process which is unavailable

我的解决方案(包含十几个项目)在VisualStudio2013中完美运行。在VisualStudio2017中,我可以打开解决方案并进行编译。但如果我开始调试,我会系统地收到此错误消息:ThesecuritydebuggingoptionissetbutitrequirestheVisualStudiohostingprocesswhichisunavailableinthisdebuggingconfiguration.Thesecuritydebuggingoptionwillbedisabled.Thisoptionmaybere-enabledintheSecuritypro

c# - 错误 : Reference to type claims it is defined, 但找不到

我有一个包含3个项目的解决方案:ParsersBase,它定义了一个接口(interface)IParseRuleParsersLibrary,它引用了ParsersBase并定义了一个类HtmlImageUrlParseRule:IParseRuleParsersLibraryTest,它引用了ParsersBase和ParsersLibrary并定义了一个带有一些测试方法的测试类当我尝试构建它时,我收到警告:Referencetotype'AVSoft.ParsersBase.IParseRule'claimsitisdefinedin'c:\Users\Tim\Dropbox\p

c# - 获取 "Tuple element name is inferred. Please use language version 7.1 or greater to access an element by its inferred name."

直到今天,在我们将VisualStudio2017更新到最新的15.3之后,我们的UWP应用程序中的以下代码一直运行良好。privatevoidTest(){vargroups=newListitems)>();varitems=newList{("a",true),("b",false),("c",false)};vargroup=(Guid.NewGuid(),items);groups.Add(group);}在输出窗口中没有错误信息但是这个Tupleelementname'items'isinferred.Pleaseuselanguageversion7.1orgreater

c# - 生成错误 : "The process cannot access the file because it is being used by another process"

我有一个C#webforms应用程序,直到今天它一直运行顺畅。现在,突然之间,每次我尝试运行该应用程序时,我都会收到文件锁定错误:Unabletocopyfile"obj\Debug\MyProject.exe"to"bin\Debug\MyProject.exe".Theprocesscannotaccessthefile"bin\Debug\MyProject.exe"becauseitisbeingusedbyanotherprocess.谷歌搜索错误并没有发现明显的错误,即VS认为文件已锁定。绝对锁定文件的是VisualStudio本身,因为当我关闭VS并重新打开它时,项目执行