草庐IT

duration_cast

全部标签

c# - "Object cannot be cast from DBNull to other types"

当我的网站执行到以下代码时,它会崩溃并出现如下异常:System.InvalidCastException:ObjectcannotbecastfromDBNulltoothertypes.为了简洁起见,我只展示了相关代码(我得到的是一个4000+LOC文件)。if(dr["STAGE"]isDBNull){dto.Stage=1;//Thisisthelinethrowingtheexception,accordingtostacktrace}else{dto.Stage=Convert.ToInt32(dr["STAGE"]);}这里,dr是一个DataRow对象,它是对数据库的查

C# 4.0 : casting dynamic to static

这是一个分支问题,与我问的另一个问题有关here.我把它分开是因为它真的是一个子问题:我在转换dynamic类型的对象时遇到困难到另一个(已知的)静态类型。我有一个用于执行此操作的IronPython脚本:importclrclr.AddReference("System")fromSystemimport*defGetBclUri():returnUri("http://google.com")请注意,它只是更新BCLSystem.Uri类型并返回它.所以我知道静态类型返回的对象。现在在C#领域,我正在更新脚本托管内容并调用这个getter来返回Uri对象:dynamicuri=sc

c# - "Unable to cast object of type ' System.Net.Http.Formatting.JsonContractResolver ' to type ' Newtonsoft.Json.Serialization.DefaultContractResolver '."

我们有一个最近被转移到新服务器的WEBAPI项目。在对项目的有效负载进行一些添加后,我正在运行我的项目,但它突然抛出以下错误:Unabletocastobjectoftype'System.Net.Http.Formatting.JsonContractResolver'totype'Newtonsoft.Json.Serialization.DefaultContractResolver'.有问题的代码行在global.asax中:protectedvoidApplication_Start(){GlobalConfiguration.Configure(WebApiConfig.R

c# - 为什么 Enumerable.Cast 不使用用户定义的转换?

比如说,我们有2个类:publicclassA{publicinta;}publicclassB{publicintb;publicstaticimplicitoperatorB(Ax){returnnewB{b=x.a};}}那为什么Aa=newA{a=0};Bb=a;//OKListlistA=newList{newA{a=0}};ListlistB=listA.Cast().ToList();//throwsInvalidCastException对于explicit运算符也是如此。P.S.:手动(单独)转换每个元素是可行的ListlistB=listA.Select(s=>s)

c# - LINQ 中 select 中的 cast 和 as 之间的区别

这段代码抛出异常:varquery=services.SomeQuery(bar).select(x=>(Foo)x).Where(x.PropertyOfFoo==FooState.SomeState);varresult=query.ToList();异常:Unabletocastthetype...LINQtoEntitiesonlysupportscastingEDMprimitiveorenumerationtypes.此代码有效:varquery=services.SomeQuery(bar).select(x=>xasFoo).Where(x.PropertyOfFoo=

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# 泛型 : cast generic type to value type

我有一个泛型类,它保存指定类型T的值。该值可以是int、uint、double或float。现在我想获取值的字节以将其编码为特定协议(protocol)。因此我想使用方法BitConverter.GetBytes()但不幸的是Bitconverter不支持泛型类型或undefinedobject。这就是为什么我要转换值并调用GetBytes()的特定重载。我的问题:如何将通用值转换为int、double或float?这不起作用:publicclassGenericClasswhereT:struct{T_value;publicvoidSetValue(Tvalue){this._va

c# - 委托(delegate)操作 : new Action or casting Action?

我发现了两种不同的方法来使用Action初始化Delegate:创建一个新的Action或转换为Action。Delegatefoo=newAction(()=>DoNothing(param));Delegatebar=(Action)(()=>DoNothing(param));这两种语法有区别吗?哪个更好,为什么?此示例中使用了委托(delegate),因为语法对于使用lambda表达式调用BeginInvoke或Invoke等方法很有用,并且将lambda表达式转换为操作很重要staticmain{Invoke((Action)(()=>DoNothing()));//OKIn

c# - Casting List<T> - 协方差/逆变问题

给定以下类型:publicinterfaceIMyClass{}publicclassMyClass:IMyClass{}我想知道如何转换List到List?我对协变/逆变主题不是很清楚,但我知道我不能因此而简单地使用List。我只能想出这个微不足道的解决方案;缺乏优雅,浪费资源:...publicListConvertItems(Listinput){varresult=newList(input.Count);foreach(varitemininput){result.Add(item);}returnresult;}....如何以更优雅/更高效的方式解决它?(请注意,我需要.N

c# - 为什么 Linq Cast<> 助手不能与隐式转换运算符一起使用?

在决定重复投票之前,请阅读到最后...我有一个实现implicitcast的类型运算符到另一种类型:classA{privateBb;publicstaticimplicitoperatorB(Aa){returna.b;}}classB{}现在,隐式和显式转换工作正常:Bb=a;Bb2=(B)a;...那么Linq的.Cast怎么来的呢?不是吗?A[]aa=newA[]{...};varbb=aa.Cast();//throwsInvalidCastException查看.Cast的源代码,没有太多的魔法:如果参数真的是一个IEnumerable的一些特殊情况,然后:foreach(