草庐IT

value-type

全部标签

C#,将一个 bool 复制到另一个(通过 ref,而不是 val)

我在这里的一堵砖墙边。是否可以将一个bool复制到另一个bool的ref。考虑这段代码。..boola=false;boolb=a;b现在是一个完全独立的bool值,值为false。如果我随后更改a,它将不会影响b。是否可以通过ref使a=b?我该怎么做?非常感谢 最佳答案 没有。由于bool是值类型,它总是按值复制。最好的选择是将你的bool包装在一个类中——这将赋予它引用类型语义:publicclassBoolWrapper{publicboolValue{get;set;}publicBoolWrapper(boolvalue

c# - Cookie 在 ASP.net 中失去值(value)

我有以下设置cookie的代码:stringlocale=((DropDownList)this.LoginUser.FindControl("locale")).SelectedValue;HttpCookiecookie=newHttpCookie("localization",locale);cookie.Expires=DateTime.Now.AddYears(1);Response.Cookies.Set(cookie);但是,当我尝试读取cookie时,值为Null。cookie存在。我永远不会通过以下if检查:if(Request.Cookies["localizati

c# - 林克 : Delete and Insert same Primary Key values within TransactionScope

我想在一个事务中用新记录替换数据库中的现有记录。使用TransactionScope,我有using(varscope=newTransactionScope()){db.Tasks.DeleteAllOnSubmit(oldTasks);db.Tasks.SubmitChanges();db.Tasks.InsertAllOnSubmit(newTasks);db.Tasks.SubmitChanges();scope.Complete();}我的程序抛出System.InvalidOperationException:Cannotaddanentitythatalreadyexis

c# - 为什么 typeA == typeB 比 type == typeof(Type B) 慢?

我最近一直在优化/对一些代码进行基准测试并遇到了这个方法:publicvoidSomeMethod(TypemessageType){if(messageType==typeof(BroadcastMessage)){//...}elseif(messageType==typeof(DirectMessage)){//...}elseif(messageType==typeof(ClientListRequest)){//...}}这是从其他地方的性能关键循环调用的,所以我很自然地假设所有这些typeof(...)调用都增加了不必要的开销(我知道这是一种微优化)并且可以移动到类中的私有

c# - 简易喷油器 : Registering a type with constructor argument that's based on its parent

我目前正在从我的项目中删除Ninject,并转而使用SimpleInjector,但有一件事我无法正常工作。对于我的日志记录,在注册服务时,我以前能够将参数传递到我的日志记录类中_kernel.Bind().To().WithConstructorArgument("name",x=>x.Request.ParentContext.Request.Service.FullName);我正在寻找一种在SimpleInjector中重新创建它的方法。到目前为止,除了这个,我还有其他所有工作。通过执行以下操作,我可以使日志记录正常工作,尽管没有显示正确的记录器名称:_container.Re

c# - 如何修复 'T' 是 'type parameter' 但被用作 'variable' 编译错误

我需要检查泛型类型参数T是MyEntity还是它的子类。下面的代码会导致这个编译器错误:'T'isa'typeparameter'butisusedlikea'variable'如何修复?publicclassMyEntity{}staticvoidTest(){//Error34'T'isa'typeparameter'butisusedlikea'variable'if(TisMyEntity){}} 最佳答案 您可以使用IsAssignableFromType上的方法检查是否有一个Type可以分配给另一个。if(typeof(

使用 Json.Net : Error converting value to type 的 C# 枚举反序列化

我正在使用Json.NET序列化/反序列化一些JSONAPI。API响应有一些整数值映射到应用程序中定义的枚举。枚举是这样的:publicenumMyEnum{Type1,Type2,Type3}并且JSONAPI响应具有以下内容:{"Name":"abc","MyEnumValue":"Type1"}有时,API会为我的枚举中未定义的MyEnumValue字段返回一个值,如下所示:{"Name":"abc","MyEnumValue":"Type4"}抛出异常:Errorconvertingvalue"Type4"totype'MyEnum'有没有办法通过分配默认值或其他方法来避免应

c# - 通用扩展方法 : Type argument cannot be inferred from the usage

我正在尝试创建一个适用于类型化数据表的通用扩展方法:publicstaticclassExtensions{publicstaticTableTypeDoSomething(thisTableTypetable,paramExpression>[]predicates)whereTableType:TypedTableBasewhereRowType:DataRow{//dosomethingtoeachrowofthetablewheretherowmatchesthepredicatesreturntable;}[STAThread]publicstaticvoidmain(){M

c# - 将 null 分配给可为空的 int 时出错 - "The value ' null' 对属性无效”

我的View模型中有这个属性:[DisplayName("Region")]publicint?RegionId{get;set;}我将我的View模型传递给我的Controller,如果RegionId为null,它会在ModelState.IsValid处失败。如果我向它传递一个整数,它就可以正常工作。错误信息是:Thevalue'null'isnotvalidforRegion在检查ModelState.IsValid之前,我也试过调用它,但我得到了同样的错误:if(viewModel.RegionId==null)viewModel.RegionId=(int?)null;这里

c# - ProgressBar 没有值(value),只是加载

使用WinForms创建这样的进度条需要做什么? 最佳答案 这叫做跑马灯进度条。只需使用常规ProgressBar并设置:myProgressBar.Style=ProgressBarStyle.Marquee;和myProgressBar.MarqueeAnimationSpeed=yourDesiredSpeed; 关于c#-ProgressBar没有值(value),只是加载,我们在StackOverflow上找到一个类似的问题: https://sta