草庐IT

C# LINQ to SQL : Refactoring this Generic GetByID method

我写了下面的方法。publicTGetByID(intid){vardbcontext=DB;vartable=dbcontext.GetTable();returntable.ToList().SingleOrDefault(e=>Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e,null))==id);}基本上它是通用类中的一个方法,其中T是DataContext中的一个类。该方法从T的类型(GetTable)获取表格,并检查输入参数的第一个属性(始终是ID)。问题是我必须先将元素表转换为列表才能对属性执行

c# - 为什么visual studio中的extract method命令会创建静态方法?

为什么VisualStudio在重构代码和选择提取方法时默认创建私有(private)静态方法?如果我正在重构一个非静态类并且该方法仅在该类中可见,为什么它是静态的?与在非静态类中调用非静态方法相比,在非静态类中调用私有(private)静态方法是否有一些性能优势?为了可读性,只是简单地表明该方法不依赖于类的任何成员变量吗? 最佳答案 WhydoesVisualStudiobydefaultcreateaprivatestaticmethodwhenrefactoringcodeandselectingextractmethod?只

c# "task method"也可以是 "async"方法吗?

我正在尝试掌握新的异步CTP内容,我可能在这里感到困惑......我可以有这个“任务方法”,没有问题:publicstaticTaskLongTaskAAsync(){returnTask.Run(()=>{return("AAA");});}但是如果我需要任务执行另一个任务,我可以将其标记为“async”并使用“await”吗?我试过这个:publicasyncstaticTaskLongTaskAAsync(){awaitTask.Delay(2000);returnTask.Run(()=>{return("AAA");});}但随后莫名其妙地得到了这个编译器错误:因为这是一个异

c# - Invoke Method (Delegate) 和直接调用有什么区别?

这个问题在这里已经有了答案:DifferencebetweenDelegate.InvokeandDelegate()(4个答案)关闭9年前。可能是之前有人问过这个问题,我用谷歌搜索但没有得到答案。委托(delegate)原型(prototype)delegatevoidmethod1(stringstr);添加回调方法method1objDel2;objDel2=newmethod1(TestMethod1);objDel2("test");objDel2.Invoke("Invoke");在上面的代码中,objDel2("test");和objDel2.Invoke("Invoke

C# "No suitable method found to override."——但是有一个

我在C#中覆盖父类的方法时遇到问题。父类设置如下:publicclassBase{publicBase(Game1game){this.game=game;}publicvirtualvoidDraw(){}}...还有子类:publicclassExt:Base{publicExt(Game1game):base(game){}publicoverridevoidDraw(SpriteBatchbatch){}}我知道我过去已经成功地覆盖了一个父方法,而现在我可能忽略了一些非常简单的事情......它是什么?编辑:那实际上是一个拼写错误:在实际的脚本中,Ext确实派生自Base。问题

C# 语言设计 : method group inside `is` operator

我对C#语言的一些设计选择很感兴趣。C#规范中有一条规则允许使用方法组作为is的表达式。运算符(operator):classFoo{staticvoidMain(){if(MainisFoo)Main();}}如规范所述,上述条件始终为假:7.10.10Theisoperator•IfEisamethodgrouporthenullliteral,ofifthetypeofEisareferencetypeoranullabletypeandthevalueofEisnull,theresultisfalse.我的问题:允许在CLR中使用没有运行时表示的C#语言元素的目的/要点/原因

C# 语言设计 : method group inside `is` operator

我对C#语言的一些设计选择很感兴趣。C#规范中有一条规则允许使用方法组作为is的表达式。运算符(operator):classFoo{staticvoidMain(){if(MainisFoo)Main();}}如规范所述,上述条件始终为假:7.10.10Theisoperator•IfEisamethodgrouporthenullliteral,ofifthetypeofEisareferencetypeoranullabletypeandthevalueofEisnull,theresultisfalse.我的问题:允许在CLR中使用没有运行时表示的C#语言元素的目的/要点/原因

C# 编译器 : cannot access static method in a non-static context

我有下面的代码:publicclassAnything{publicintData{get;set;}}publicclassMyGenericBase{publicvoidInstanceMethod(Tdata){//dosomejob}publicstaticvoidStaticMethod(Tdata){//dosomejob}//othersmembers...}publicsealedclassUsefulController:MyGenericBase{publicvoidProxyToStaticMethod(){StaticMethod(null);}//others

c# - "Property set method not found"反射时出错

我正在尝试反射(reflect)一些类属性并以编程方式设置它们,但看起来我的PropertyInfo过滤器之一不起作用://Getallpublicorprivatenon-staticpropertiesdeclaredinthisclass(noinheritedproperties)-thathaveagetterandsetter.PropertyInfo[]props=this.GetType().GetProperties(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public|BindingF

c# - 错误 : "an object reference is required for the non-static field, method or property..."

这个问题在这里已经有了答案:CS0120:Anobjectreferenceisrequiredforthenonstaticfield,method,orproperty'foo'(9个回答)关闭5年前。我正在用C#创建一个应用程序。它的功能是评估给定的是否为素数以及相同的交换数是否也是素数。当我在VisualStudio中构建我的解决方案时,它说“非静态字段、方法或属性需要对象引用...”。我在使用“volteado”和“siprimo”方法时遇到了这个问题。问题出在哪里,我该如何解决?namespaceConsoleApplication1{classProgram{static