草庐IT

is_object

全部标签

c# - System.Drawing.Graphics.DrawString - "Parameter is not valid"异常

有时,Microsoft的异常消息毫无帮助,令人恼火。我创建了一个漂亮的小MVC方法来呈现文本。方法体如下。当它到达“DrawString”方法时,我得到一个异常抛出说“参数无效”。请注意,据我所知,字体构造正确(我只是使用10pt的Arial),矩形大小为正且看起来有效,画笔为白色SolidBrush,格式标志不影响输出;也就是说,如果我从调用中排除格式标志,我仍然会收到错误消息。DrawString调用就在底部附近。publicActionResultRenderText(stringfontFamily,floatpointSize,stringforeColor,stringb

c# - XmlWriter : is calling Close() required if using a using block?

使用usingblock(不调用Close())创建XmlWriter是否足够,还是使用try/finallyblock并在finally中调用Close()更好? 最佳答案 usingblock是try/finallyblock的快捷方式,它在任何实现IDisposable的对象上调用Dispose()。对于流和流编写器,Dispose()通常手动调用Close()。使用反射器,这里是XmlWriter的Dispose方法:protectedvirtualvoidDispose(booldisposing){if(this.Wri

c# - SQL 服务器 : invalid object name in query execution

我正在尝试执行Insert语句,但一直收到Invalidobjectname错误。这是我的代码:publicstringaddNewComment(intuserID,intpageID,stringtitle,stringcomment){stringquery="INSERTINTOdbo.nokernok_kommentarer(userID,pageID,commentTitle,comment)"+"VALUES("+userID+","+pageID+",'"+title+"','"+comment+"')";adapter.InsertCommand=newSqlComm

c# - C# 窗体程序中的 "Specified cast is not valid"错误

我遇到“指定的转换无效”错误。C#中的Windows窗体应用程序。我正在尝试从表中检索值。该值要么是smallint,要么是数字(我尝试了两个字段,都给我同样的错误),我尝试将它存储在一个int变量中。这是来源:using(SqlDataReaderrdr=cmd.ExecuteReader())//"select*fromtablewherefieldname="+value{while(rdr.Read()){intnumber=(int)rdr["quantity"];//errorishere 最佳答案 rdr["quant

C# 将 Func<T1, object> 转换为 Func<T1, T2>

我毫不怀疑这很容易做到,但我有一个函数创建器库,可以为我创建以下形式的lambda函数:Func我希望更具体地指定out参数-基本上,我希望能够按照以下方式创建一些东西:privateFuncGetFunc(stringexpression){FuncobjFunc=CreateFunction(expression));returnobjFuncasFunc;}但是,当我尝试这个时,我得到了一个null(顺便说一句,如果我返回objFunc作为Func它不是空的,所以我知道那不是我的问题所在)。如何正确执行此操作? 最佳答案 尝试

c# - 将 Dictionary<string, string> 转换为 Dictionary<string, object> 的最简单方法是什么?

我使用的API返回键值集合作为Dictionary.我需要将其转换为Dictionary.我觉得应该有一种方法可以在不“手动”遍历每个键值对的情况下进行这种转换/映射,但谷歌搜索或C#对象引用并没有立即产生解决方案。 最佳答案 尝试以下操作varnewMap=oldMap.ToDictionary(pair=>pair.Key,pair=>(object)pair.Value); 关于c#-将Dictionary转换为Dictionary的最简单方法是什么?,我们在StackOverfl

c# - 当我使用 is 运算符时,为什么 IL 代码中只有空检查?

我想知道运算符是如何在C#中实现的。我写了一个简单的测试程序(没什么特别的,只是为了演示目的):classBase{publicvoidDisplay(){Console.WriteLine("Base");}}classDerived:Base{}classProgram{staticvoidMain(string[]args){vard=newDerived();if(disBase){varb=(Base)d;d.Display();}}}查看生成的IL代码:.methodprivatehidebysigstaticvoidMain(string[]args)cilmanaged

c# - Resharper 中的警告 "Return value of pure method is not used"

我有一个快速的问题,关于我正在工作的c#项目中从VisualStudio中的Resharper收到的警告。警告是:"ReturnValueofpuremethodisnotused"发生这种情况的方法如下:privatestaticboolFilePathHasInvalidChars(stringuserInputPath){try{//thisiswherethewarningoccurs:Path.GetFullPath(userInputPath);}catch(Exceptione){Log.Error(String.Format("TheProgramfailedtorun

c# - 多线程,Task.Run 错误 'The call is ambiguous between the following methods or properties'

当我尝试构建项目时,显示以下错误消息。Thecallisambiguousbetweenthefollowingmethodsorproperties:'System.Threading.Tasks.Task.Run(System.Action)'and'System.Threading.Tasks.Task.Run(System.Func)'我该如何解决这个问题?publicstaticclassMaintananceManager{privatestaticThreadSafeSocialMediaListPostList=newThreadSafeSocialMediaList(

C# 应用程序设置 : Is there a easy way to put a collection into <appSetting>

我试过了和System.Configuration.ConfigurationManager.AppSettings.GetValues("List");但我只得到最后一个成员。我怎样才能轻松解决这个问题? 最佳答案 我处理过类似的问题,我是用这段代码解决的。希望这对您的问题有所帮助。在这种情况下,列表(类似于我的URLSection)将在web.config中有一个完整的配置部分,然后您可以从该部分获取所有值。我为此创建了三个类:ConfigElement、ConfigElementCollection、WebConfigSect