jjzjj

c# - 为什么在 DataContract 上调用 Dispose,即使服务仍然引用它?

我定义了以下DataContract实现了IDisposable:[DataContract]publicclassRegularFileMetadata:FileMetadataBase,IDisposable{bool_Disposed=false;//notethis!//...protectedvirtualvoidDispose(booldisposing){if(!_Disposed){//..._Disposed=true;//notethistoo!}}publicvoidDispose(){Dispose(true);GC.SuppressFinalize(this)

c# - Controls.Clear() 清理多深?

我正在使用一个TableLayoutPanel,它动态填充了其他TablelayoutPanel。现在我想知道当我在动态填充的TableLayoutPanel上调用TableLayoutPanel.Controls.Clear时会发生什么。显然,所有的子布局都被移除了,但是他们的子布局呢?它们是否也得到妥善处理,还是我需要担心内存泄漏?我应该在调用Clear()之前递归地删除child的child吗? 最佳答案 Clear不处理控件,导致内存泄漏。来自链接:CallingtheClearmethoddoesnotremovecont

c# - 如何正确实现 IDisposable

作为一名开发人员,我见过太多C#代码试图通过将变量设置为null或在他们自己的类Dispose()方法中调用类(例如DataSet)上的Dispose()来帮助GC我一直想知道是否需要在托管环境中实现它。这段代码在其设计模式中是否浪费时间?classMyClass:IDisposable{#regionIDisposableMemberspublicvoidDispose(){otherVariable=null;if(dataSet!=null){dataSet.Dispose();}}#endregion} 最佳答案 GC不会调

c# - 在 C# 最佳实践中处理字典

我的session类中有一个ConcurrentDictionary。Key是一个表示管理器类的接口(interface)。值是在此session中用于该管理器的DataContracts类的列表。当我配置session类时,我想清除这个字典。我需要清除所有值和键,但我无法处理键-因为它们在课后仍然存在dispose..这就够了吗?-这会导致GC完成工作吗?_myDictionary=null;或者我需要在所有键上使用foreach进行迭代并使用Remove来清除值。 最佳答案 WhenIdisposethesessionclass

c# - Autofac:如何在不绕过 IoC 容器的情况下限制 IDisposable 对象的生命周期

我目前正在学习如何使用Autofac,而且我一直坚持处理IDisposable对象确定性。在我陈述我的问题之前,让我先介绍一下情况。起始位置:假设我的对象模型是通过以下接口(interface)定义的:interfaceIApple:IDisposable{voidConsume();}interfaceIHorse{voidEat(IAppleapple);//issupposedtocallapple.Consume()}interfaceIHorseKeeper{voidFeedHorse();//issupposedtocallhorse.Eat(apple)//where'h

c# - 是否所有一次性对象都在 using block 中实例化?

这是我过去多次问自己的问题,因为我嵌套了using语句5深。阅读docs并且没有发现任何关于block内实例化的其他一次性用品的提及,我认为这是一个很好的SO文件Q。考虑一下:using(varconn=newSqlConnection()){varconn2=newSqlConnection();}//isconn2disposed? 最佳答案 不,他们不是。只有在using子句中明确列出的变量集才会被自动释放。 关于c#-是否所有一次性对象都在usingblock中实例化?,我们在S

c# - connection.Close() 和 connection.Dispose() 有什么区别?

这个问题在这里已经有了答案:CloseandDispose-whichtocall?(8个答案)关闭9年前。我注意到System.Data.SQLite中的SQLiteConnection对象拥有两个相似的方法:关闭()Dispose()SQLiteDataReader对象也是如此。有什么区别?

c# - 返回用于在 using C# 中使用的变量

我在using语句中返回我在using语句中创建的变量(听起来很有趣):publicDataTablefoo(){using(DataTableproperties=newDataTable()){//dosomethingreturnproperties;}}这会Dispose属性变量吗??这样做之后我仍然收到这个警告:Warning34CA2000:Microsoft.Reliability:Inmethod'test.test',callSystem.IDisposable.Disposeonobject'properties'beforeallreferencestoitare

c# - 一次性问题

假设我有以下内容:publicabstractclassControlLimitBase:IDisposable{}publicabstractclassUpperAlarmLimit:ControlLimitBase{}publicclassCdsUpperAlarmLimit:UpperAlarmLimit{}两个问题:1.我对我的IDisposable成员何时真正被调用感到有点困惑。当CdsUpperAlarmLimit的实例超出范围时,它们会被调用吗?2。我将如何处理在CdsUpperAlarmLimit类中创建的对象的处置?这也应该派生自IDisposable吗?

c# - 何时处置以及为什么?

我问了一个question关于这个方法://SaveanobjectouttothediskpublicstaticvoidSerializeObject(thisTtoSerialize,Stringfilename){XmlSerializerxmlSerializer=newXmlSerializer(toSerialize.GetType());TextWritertextWriter=newStreamWriter(filename);xmlSerializer.Serialize(textWriter,toSerialize);textWriter.Close();}在回复