Questions tagged «typedreference»

3
为什么TypedReference在幕后?它是如此快速和安全……简直太神奇了!
警告:这个问题有点异端...宗教程序员始终恪守良好实践,请不要阅读它。:) 有谁知道为什么不鼓励使用TypedReference(隐式地,由于缺乏文档)? 我发现它有很好的用途,例如,当通过不应该是泛型的函数传递泛型参数时(object如果需要使用值类型,则使用over可能会导致过时或缓慢),需要不透明指针时;或者用于何时需要快速访问数组元素的信息,您可以在运行时找到其规格(使用Array.InternalGetReference)。由于CLR甚至不允许这种类型的错误使用,为什么不鼓励这样做?它似乎并不安全或其他任何东西。 我发现的其他用途TypedReference: C#中的“专业化”泛型(这是类型安全的): static void foo<T>(ref T value) { //This is the ONLY way to treat value as int, without boxing/unboxing objects if (value is int) { __refvalue(__makeref(value), int) = 1; } else { value = default(T); } } 编写可与通用指针一起使用的代码(如果滥用,这将非常不安全,但如果使用正确,则会非常安全): //This bypasses the restriction that you can't have …
128 c#  typedreference 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.