草庐IT

class_exists

全部标签

c# - SerialPort port.open "The port ' COM 2' does not exist."

SerialPort.Open();我遇到了一个大问题我正在与USB虚拟COM端口(cdc)通信,它被列为COM2。它在TeraTerm/hyperTerminal等中运行良好。但是当我尝试在C#中打开端口时,它给了我异常Theport'COM2'doesnotexist。我使用SerialPort.GetPortNames()函数获取端口名称,调试时它看起来很好。我曾尝试将名称设置为硬编码,但没有成功。现在真正奇怪的是,它在某些PC上运行良好,而在其他PC上运行失败。在一些PC上,它总是失败,而在其他PC上,它有10%的时间失败。更奇怪的是它取决于所使用的USB端口,一些端口工作正常

c# - C# 3.0 中 "special class"枚举的泛型类型约束是否有解决方法?

这个问题在这里已经有了答案:Anyoneknowagoodworkaroundforthelackofanenumgenericconstraint?(12个答案)关闭9年前。Update:SeethebottomofthisquestionforaC#workaround.你好,考虑以下扩展方法:publicstaticboolHasFlags(thisTvalue,Tflags)whereT:System.Enum{//...}如您所知,这将在编译时抛出错误,因为通常不允许类从System.Enum继承。问题在于使用enum关键字指定的任何枚举实际上都继承自System.Enum,

c# - 从 C# : Instance does not exist in the specified category 读取性能计数器

我正在尝试从另一个.NET4应用程序中读取正在运行的.NET4应用程序的许多性能计数器。一些计数器,例如Process\%ProcessorTime和Process\PrivateBytes工作正常。但是,一旦我尝试从.NET类别之一读取性能计数器,例如.NETCLRMemory\#Gen0Collections,我就会收到以下异常:Instance'MyApplication'doesnotexistinthespecifiedCategory当我打电话时:newPerformanceCounterCategory(".NETCLRMemory").GetInstanceNames(

c# - 不能同时指定约束类和 0​​x104567910 或 'class' 约束

我正在尝试解决mockingissue通过创建IDbSet的自定义模拟。自定义模拟:publicclassDbSetMock:IDbSet{/*hiddenallotherimplementedmethods/properties*/publicTDerivedEntityCreate()whereTDerivedEntity:class,Tenant{thrownewNotImplementedException();}}create方法给出了一个构建错误,我不知道如何解决:cannotspecifybothaconstraintclassandthe'class'or'struct

c# - Class B 继承自 ClassA 时,如何将 List<Class> 转换为 List<Class>?

我将json字符串反序列化为List现在我想把它转换到List在我把它从BindModel方法。我需要转换,因为这些方法期望得到List.为什么我在转换时出错?毕竟,ClassB继承自ClassA.我该怎么办?附言这个问题是从thispost扩展而来的.在线newDataContractJsonSerializer(typeof(List));而不是List该类型将在运行时构建。publicoverrideobjectBindModel(...){varserializer=newDataContractJsonSerializer(typeof(List));MemoryStream

c# - ASP.net MVC View 是 "class"吗?

...首先,我这样做只是出于好奇。这里没有实际应用,只是为了了解和修补......ASP.NETView具有类似Model的属性和ViewData甚至还有方法。您甚至可以使用@Using就像普通的class.cs文件一样。我知道它的类型是WebPageView我的主要问题是:是一门课吗?应该是因为它是一个类型,但是..我应该也能做到这一点(Razor引擎):@{publicclassPerson{//etc...}varp=newPerson();}@p.Name但是我不能..为什么?注意:目前是C#、ASP.net初学者。 最佳答案

c# - 简易喷油器 : Factory classes that need to create classes with dependencies

我有一个工厂类,它创建了几个不同类型的类。工厂在容器中注册。鉴于它们也具有依赖性,在工厂内部创建类的推荐方法是什么。我显然想避免对容器的依赖,但如果我新建这些类,那么它们将不会使用容器。例如publicclassMyFactory{publicIMyWorkerCreateInstance(WorkerTypeworkerType){if(workerType==WorkerType.A)returnnewWorkerA(dependency1,dependency2);returnnewWorkerB(dependency1);}}所以问题是我从哪里获得这些依赖项。一种选择是使它们成

c# - C++/命令行界面 : How do I declare abstract (in C#) class and method in C++/CLI?

以下C#代码在C++/CLI中的等价物是什么?publicabstractclassSomeClass{publicabstractStringSomeMethod();} 最佳答案 只需稍微混合关键字即可获得正确的语法。abstract在C#中位于前面,但在C++/CLI中位于末尾。与override关键字相同,今天也被C++11兼容的编译器识别,它们期望它位于函数声明的末尾。就像传统C++中的=0标记函数抽象一样:publicrefclassSomeClassabstract{public:virtualString^SomeM

c# - 部分类(class)的目的是什么?

我读了aboutpartialclasses并且,例如,我理解在VisualStudio创建Windows窗体时使用它们的原因,但不排除whenworkingonlargeprojects,spreadingaclassoverseparatefilesenablesmultipleprogrammerstoworkonitatthesametime.后来我看了thisexample我注意到大多数类都被声明为部分类。为什么? 最佳答案 部分类很有用的一个很好的例子是当类由工具(如VS)自动生成并且您希望能够扩展该类的功能并且在工具需

c# - 数字必杀技 : Where does a callvirt of a non-existent method end up?

我在其基类中标记为抽象的库类上调用属性集访问器。现在在运行时我force应用程序针对另一个版本的库运行,其中类仅实现基类的底层接口(interface),但不是从它派生的。有趣的是,.NET将运行代码,但设置该属性没有任何效果。幕后发生了什么?违规代码:MyDbParameterparam=newMyDbParameter();param.ParameterName="p";Console.Out.WriteLine("ParameterName:"+param.ParameterName);库2.0(已编译)publicsealedclassMyDbParameter:System.