首页 \ 问答 \ 为什么我不能将事件标记为NotNull?(Why can't I mark an event as NotNull?)

为什么我不能将事件标记为NotNull?(Why can't I mark an event as NotNull?)

public class EventBus<T>
{
    [NotNull] // annotation not valid on this declaration type
    private static event Action<T> Events;

    static EventBus()
    {
        // we always have a do-nothing event handler so we don't have to worry about null checks and race conditions
        Events += T => { };
    }

正如评论中所见,我明确地不想在任何地方处理null检查事件。 这是通过在从未调用的构造中分配默认的do-nothing事件来解决的。 Resharper不能自动解决这个问题并不奇怪,所以我想用NotNull注释来注释它。 不幸的是,似乎NotNull不能应用于事件,但Resharper在我调用我的事件时随时警告我“可能的'System.NullReferenceException'”。

如果resharper会注意到错误,那么应该可以通过注释来避免它。


public class EventBus<T>
{
    [NotNull] // annotation not valid on this declaration type
    private static event Action<T> Events;

    static EventBus()
    {
        // we always have a do-nothing event handler so we don't have to worry about null checks and race conditions
        Events += T => { };
    }

As seen in the comment, I explicitly don't want to have to deal with null checking the Events everywhere. This is solved by assigning a default do-nothing event at construction that is never called. It is not surprising that Resharper can't automatically figure this out so I wanted to annotate it with the NotNull annotation. Unfortunately, it would appear that NotNull cannot be applied to events, yet Resharper doesn't hesitate to warn me about a "Possible 'System.NullReferenceException'" anytime I call my events.

If resharper is going to notice the error, it should be possible to get avoid it with annotations.


原文:https://stackoverflow.com/questions/18928136
更新时间:2023-10-23 11:10

最满意答案

鉴于您的左侧图像显示癌症 ,这是阴影射线击中被反射的物体的经典案例。 在对阴影射线进行击中测试时,您需要排除生成射线的表面。 只需将源对象传递给阴影函数,然后忽略它。

此方法仅适用于凸形。 如果你的形状有自阴影(例如圆环),你需要更加通用。 通常的方法是定义epsilon (浮点误差容差)并忽略任何比它更近的交叉点。

另一种方法是检测您击中的表面的哪一侧 。 你不应该在球体上自阴影,因为射线的投射方向与曲面法线相同( 射出光线和曲面法线的点积为正) - 这不应算作阴影。


Given that your left image shows cancer, this is a classic case of the shadow ray hitting the object off which it was reflected. When hit-testing a shadow ray, you need to exclude the surface that generated the ray. Just pass the source object into your shadow function, and ignore it.

This method only works for convex shapes. If you have shapes that do self-shadow (a torus, for example), you need to be more general. The usual approach is to define an epsilon (floating-point error tolerance) and ignore any intersection points that are nearer than that.

The other approach is to detect which side of a surface you hit. You should not self-shadow on a sphere because the ray is being cast in the same general direction as the surface normal (ie the dot product of the outgoing ray and the surface normal is positive) - this should not be counted as a shadow.

相关问答

更多
  • 我没有检查过您的折射公式,但是这看起来不正确: //into air out of obj if(dot(ray,normal) < 0){ n1 = ior; n2 = 1.0f; *cos = dot(ray,-normal); } 如果入射光线和法线的点积小于零,并且假设物体air -> inside的正常点(它可能应该),那么这种情况对应于air -> inside ,所以您的折射率应该交换。 因为它现在是用ior 1 / ior渲染一个球体,并且由于折射率小于1,所以你正在观察边缘的 ...
  • 你应该使用Phong-Bui Tong在1975年创建的Phong Shading方法.Phong方程将光照简化为三个部分:环境光,漫反射光和镜面光。 环境光是在完全黑暗中物体的照明。 环境照明不受光源影响。 漫射光是基于交叉点的表面法线与来自交叉点的光矢量之间的角度的光的亮度。 镜面光是我相信你正在寻找的。 它基于从相交角度到相机位置的矢量与关于表面的光矢量的反射矢量之间的角度。 以下是我通常使用Phong Shading的方法: 对于场景中的任何对象,定义三个常量:Ka(环境光),Kd(漫反射光)和Ks ...
  • 我开始这个小组是因为我对这种事感兴趣, http://groups.google.com/group/python-ray-tracing-community/web/list-of-python-statistical-ray-tracers 。 在这里,您将找到一个(非详尽的)python光线跟踪器列表,它应该指向正确的方向。 我还有一个用Python编写的光线跟踪器,它可以满足您的需求,但尚未发布! I started this group because I'm interested in this ...
  • 检查平面并行性可能是值得的。 如果你知道自己在做什么,就可以获得相当不错的性能,但为了获得出色的性能,有时需要阅读并检查核心输出,以了解为什么事物没有被拆箱。 不过,这取决于。 如果你正在编写绝对性能的C代码,那么你很可能不会用Repa来击败 C,因为别名分析目前看起来并不是很好。 但除非您将C代码手工融合到一个单一的函数中,否则我怀疑从融合中获得的好处以及Repa中的“自由并行”可能有助于它的竞争。 http://code.ouroborus.net/gloss/gloss-head/gloss-exam ...
  • 这是IE与不透明度过滤器结合使用时如何处理8位alpha通道的错误。 我所知道的唯一解决方案是使图像成为AlphaImageLoader过滤器(就像IE 6在任何不透明度下正确显示alpha通道一样)而不是背景图像。 This is a bug in how IE handles the 8-bit alpha channel when combined with the opacity filter. The only solution I'm aware of is to make the image ...
  • 您可以通过更改半径来尝试这个:
  • 我发现http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtrace0.htm很有用。 它不会一直回到基础,但可能仍然有帮助(提示:如果你看到像我一样破碎的字体字符,它意味着是一个点积)。 I found http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtrace0.htm useful. It doesn't go all the way back ...
  • 鉴于您的左侧图像显示癌症 ,这是阴影射线击中被反射的物体的经典案例。 在对阴影射线进行击中测试时,您需要排除生成射线的表面。 只需将源对象传递给阴影函数,然后忽略它。 此方法仅适用于凸形。 如果你的形状有自阴影(例如圆环),你需要更加通用。 通常的方法是定义epsilon (浮点误差容差)并忽略任何比它更近的交叉点。 另一种方法是检测您击中的表面的哪一侧 。 你不应该在球体上自阴影,因为射线的投射方向与曲面法线相同( 即射出光线和曲面法线的点积为正) - 这不应算作阴影。 Given that your l ...
  • 绝对看起来像箱子阴影中的错误(当使用传播arg时)。 作为一种解决方法,只需使用叠加div。 这是代码: HTML:
    CSS: div { margin:10px; width: 100px; height: 100px; background-color: transparent; box-shadow: 0 0 15px 20px #000 inset; border: 10 ...
  • 除了编码时永远不会很好的硬编码常数之外,还有一个更微妙的问题,尽管你的图像整体看起来很好。 蒙特卡罗积分包括将被积函数除以生成这些样本的概率密度函数 (pdf)。 因此,您的代码中存在两个问题: 虽然你似乎已经使用了pdf for Phong模型(如果我认得它很好;至少它不是一个统一的pdf),你没有用pdf分 您已经进一步按比例缩放x和y分量1./16. 因为没有理由进一步改变你的pdf。 我们的想法是, 如果你能够根据Phong的模型和余弦定律完全采样你的光线,那么你甚至不必将你的被积函数乘以BRDF。 ...

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)