- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我最近遇到了 ConditionalWeakTable<TKey,TValue>
在我搜索 IDictionary
的类(class)中它使用弱引用,如答案 here 中所建议的那样和 here .
有a definitive MSDN article其中介绍了该类(class)并指出:
You can find the class ... in the System.Runtime.CompilerServices namespace. It’s in CompilerServices because it’s not a general-purpose dictionary type: we intend for it to only be used by compiler writers.
然后又是:
...the conditional weak table is not intended to be a general purpose collection... But if you’re writing a .NET language of your own and need to expose the ability to attach properties to objects you should definitely look into the Conditional Weak Table.
与此相符,该类的 MSDN 条目描述如下:
Enables compilers to dynamically attach object fields to managed objects.
很明显,它最初是为一个非常具体的目的而创建的——帮助 DLR,以及 System.Runtime.CompilerServices
命名空间体现了这一点。但它似乎找到了比这更广泛的用途——甚至在 CLR 中也是如此。如果我搜索 ConditionalWeakTable 的引用资料在 ILSpy ,例如,我可以看到在 MEF 类中使用了 CatalogExportProvider
并在内部 WPF DataGridHelper
类等。
我的问题是是否可以使用ConditionalWeakTable在编译器编写和语言工具之外,以及这样做是否存在任何风险,即在未来的 .NET 版本中产生额外的开销或实现发生重大变化。 (或者应该避免使用 this one 之类的自定义实现来代替)。
还有延伸阅读here , here和 here关于如何ConditionalWeakTable使用 ephemerons 的隐藏 CLR 实现(通过 System.Runtime.Compiler.Services. DependentHandle
)处理键和值之间的循环问题,以及如何以自定义方式轻松实现这一点。
最佳答案
我没有发现使用 ConditionalWeakTable
有什么问题。如果您需要临时表,您几乎别无选择。
我认为 future 的 .NET 版本不会成为问题 - 即使只有编译器会使用此类,Microsoft 仍然无法在不破坏与现有二进制文件的兼容性的情况下更改它。
至于开销 - 与普通字典相比肯定会有开销。拥有许多 DependentHandle
可能会很昂贵,类似于有多少 WeakReference
比普通引用更昂贵(GC 必须做额外的工作来扫描它们以查看它们是否需要被清零)。但这不是问题,除非您有很多(几百万)个条目。
关于c# - ConditionalWeakTable<TKey, TValue> 是否应该用于非编译器目的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10225727/
我正致力于通过 OAuth 合并外部 API,但对 expires_in 属性的用途有点迷惑。通过阅读,应该对 api token 的使用进行防御性编码,因为您应该预料到 token 在任何时候都可能
有人可以概述或总结一下 Spring 框架上下文中 bean 的用途吗? 我了解标准的 Java bean(没有 arg 构造函数、getter/setter,通常是序列化的),但 Spring be
使用 OpenGL 4.1 和 ARB_separate_shader_objects,我们能够在着色器程序中存储着色管道的不同阶段。众所周知,要使用这些,我们需要将它们附加到程序管道对象,然后绑定(
正如我从文档中了解到的那样,“MoveIteratorFactory”的目的是生成每一步都需要执行的 Action 。 “getSize”方法的移动子集有多大? “createOriginalMove
请解释 CMakeLists.txt 中这一行的目的是什么: 包括(InstallRequiredSystemLibraries) 我在 CMake 示例中看到这一行,但找不到好的解释,为什么我需要它
这里是新手。我仍在尝试理解在多个布局中运行单个进程或目的的概念。 例如,我想在我的申请中添加“提交后”功能。有一个包含标题、内容等文本框的主布局,以及一个链接到另一个布局以选择类别的按钮。我的问题是,
我在看 Box Oauth2.0 View Controller : https://github.com/box/box-ios-sdk-v2/blob/master/BoxSDK/OAuth2/B
我编写了一个将字符串复制到系统剪贴板的 Java 应用程序。构造函数使用 Clipboard.setContents(Transferable contents, ClipboardOwner own
阅读此文后:http://sourcemaking.com/design_patterns/command 我还是不太明白为什么我们需要这个。 最佳答案 想法是,如果命令被封装为对象,那么这些命令可以
我知道 c++ 中的模板是做什么的,但是今天我看到了一些奇怪的代码: template <> void swap(foo &a, foo &b) { a.name = b.name; a.
我不太明白 C# Collections 中 IEnumerator 的用途是什么。它的用途是什么,为什么要使用它? 我试着在线查看 http://msdn.microsoft.com/en-us/l
不幸的是,我今天做了一些代码考古(同时重构了一些旧的危险代码)并发现了这样的小化石: # line 7 "foo.y" 能在里面找到如此古老的宝藏,我完全惊呆了。我在 C 编程的网站上阅读了它。然而,
您能否澄清一下此注释的实际用途? - 如果我们没有使用数据库中的 SQL 表定义定义相应的约束,会发生什么情况。当我们尝试插入时,hibernate 会检查唯一性吗?或者这就是DB的目的吗?如果 hi
我在视频教程中看到过这段代码: const navToggle = ["Menu"].join(""); $(".site-header").prepend(navToggle); 我明白它的基本作用
我想知道这个成员函数的 scroll_to(TextBuffer::iterator& iter, double within_margin = 0)参数 within_margin。 API 是这样
我想知道是否可以将子目录提交到目录例如,假设您有 site.com/directory 可以将子目录提交到目录。我即将开始为希望她的网站在搜索引擎中排名靠前的客户进行一些搜索引擎优化。我知道实现此目的
STL 迭代器的用途是什么?为什么程序员要创造这个概念? 最佳答案 迭代器允许您将算法与容器分开。只要您有开始和结束迭代器,并且知道迭代器的功能(随机访问等),您就可以在迭代器指定的范围内进行操作。例
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&respons
我正在编写代码,使用通用的 linux i2c 驱动程序 linux/i2c-dev.h 实现一个简单的 i2c 读/写功能 我对 ioctl 感到困惑:I2C_SLAVE 内核文档说明如下: You
在尝试克隆可变集合时,我最初的方法是对 mutable.Cloneable 特征使用 clone() 方法。但是,这取决于创建引用副本的 java.Object.clone 实现,而不是深拷贝。通过测
我是一名优秀的程序员,十分优秀!