- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
C# 允许我们 to create custom event accessors .
Action _custom;
public event Action Custom
{
add { _custom = (Action)Delegate.Combine( _custom, value ); }
remove { _custom = (Action)Delegate.Remove( _custom, value ); }
}
如果您不指定它们,the compiler creates them for you . C# 语言规范:
When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and creates accessors for the event that add or remove event handlers to the delegate field.
使用dotPeek反编译的源代码对于一个简单的 public 事件 Action Public;
看起来如下:
private Action Public;
public event Action Public
{
add
{
Action action = this.Public;
Action comparand;
do
{
comparand = action;
action = Interlocked.CompareExchange<Action>(
ref this.Public, comparand + value, comparand);
}
while (action != comparand);
}
remove
{
Action action = this.Public;
Action comparand;
do
{
comparand = action;
action = Interlocked.CompareExchange<Action>(
ref this.Public, comparand - value, comparand);
}
while (action != comparand);
}
}
值得注意的是,字段和事件使用相同的名称。这导致了some people得出结论,您可以通过在类中查找与事件同名的字段来找到有关反射期间支持字段的信息。我按如下方式实现了这一点:
public static FieldInfo GetFieldInfo( this EventInfo eventInfo )
{
Contract.Requires( eventInfo != null );
return eventInfo.DeclaringType.GetField(
eventInfo.Name,
BindingFlags.DeclaredOnly | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic );
}
这可行,但提出了一个问题:编译器生成的事件的支持字段是否始终保证使用与事件相同的名称?
无法使用 Visual Studio 创建自定义事件访问器来访问具有相同名称的委托(delegate)。这导致消息:“已声明具有相同名称的成员。”我想知道您是否可以得出结论,任何没有可用的具有相同名称的支持委托(delegate)的事件都是具有自定义的事件访问器。
最佳答案
Is the backing field of a compiler generated event always guaranteed to use the same name as the event?
Jon 和 Marc 的回答“否”是完全正确的。
这是一个未记录的编译器实现细节,规范中明确注明,随时可能更改。
在实践中,这不太可能改变。我们使用字段和事件具有相同名称这一事实作为在编译器中将它们在逻辑上相互关联的最简单可能方式。
It's not possible to create custom event accessors which access a delegate with the same name using Visual Studio. This results in the message: "Member with the same name is already declared."
正确。
I am wondering whether you could conclude that any event for which no backing delegate with the same name is available is an event with custom accessors.
我不太愿意做出这样的结论。如果您知道有问题的程序集是从 C# 编译器发出的,您可能能够得出该结论。但就发射组件而言,我们并不是唯一的游戏。您可以使用 ILDASM 做一些非常奇怪的事情。
请问你为什么想知道这些东西?我同意马克;如果你通过反射访问一个字段,你可能做错了。您应该能够毫无问题地访问类内的字段(因为它只是一个私有(private)字段),并且从类外,您没有业务查看另一个类的私有(private)实现细节。 使用反射来绕过访问器强加的线程安全性是特别过分的。那些访问器是为了保护你;不要绕着他们跑。
关于c# - 编译器生成的事件的支持字段是否始终保证使用与事件相同的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9847424/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
有没有办法保证您的系统托盘图标被删除? 添加系统托盘图标: Shell_NotifyIcon(NIM_ADD, &m_tnd); 删除系统托盘图标: Shell_NotifyIcon(NIM_DELE
是否保证(-x) % m,其中x和m在c++中为正standard (c++0x) 为负数,等于 -(x % m)? 我知道它在我知道的所有机器上都是正确的。 最佳答案 除了Luchian的回答,这是
可能还有其他方法可以作为示例,但这不是我要问的重点。 我正在这样做: (future (clojure.java.shell/sh "sleep" "3" :dir "/tmp")) 启动对Shell
可以使用 XREAD(或者可能是另一个命令)以原子方式检测数据是否写入 Redis 流? 进一步来说: 假设您在一个进程中将一些数据添加到 Redis 流中,并看到数据已通过某个自动生成的 key 成
Kotlin 协程是否提供任何“发生之前”保证? 例如,在这种情况下,写入 mutableVar 和随后在(可能)其他线程上读取之间是否存在“发生之前”保证: suspend fun doSometh
我正在开发一个跟踪行程的应用程序。在搜索了这件事之后,我得出结论,实现这一点(持续跟踪用户的位置)的最好方法是使用前台服务。在某些情况下工作得很好,但在其他一些情况下(即使关闭 DOZE),我得到一些
我正在使用 ORM (sqlalchemy) 从 PG 数据库中获取数据。我想避免在我手工编写的 SQL 语句中指定所有表列名称*。 到目前为止,我的假设是返回的列按照用于创建数据库表的 DDL 语句
在 setState 的文档中这样说: setState() does not immediately mutate this.state but creates a pending state tr
我有一个与不同硬件接口(interface)的简单应用程序。对于每个硬件,我针对一个独特的监视器函数生成了一个 pthread_t,总共有 6 个线程:1 个管理线程和 5 个工作线程。 每个线程都有
目前,我有 private ThreadLocal shortDateFormat = new ThreadLocal() { @Override protected DateFormat i
我有一个使用 SolrCloud 将文档写入 Solr 的 Java 作业。输入数据被转换为不同实体的映射,然后将每个实体写入与其实体类型对应的 Solr 集合。 我的代码如下: public voi
我们使用嵌入式设备通过串行到以太网转换器将数据包从串行端口发送到服务器。我们使用的一家制造商 Moxa 将始终以与构建它们相同的方式发送数据包。意思是,如果我们构建一个大小为 255 的数据包,它将始
我是从 C++ 转到 Java 的。在 C++ 世界中,我们关注异常安全,并注意到变元器可以在变元器本身或其委托(delegate)的方法抛出异常时提供不同的保证(最小、强、不抛出)。实现具有强异常保
我想将来自 SAAJ 的 SOAPConnectionFactory 和 MessageFactory 类与多个线程一起使用,但事实证明我不能假设它们是线程安全的。一些相关的帖子: javax.xml
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
关于正确性,我找不到以下代码片段没有设计缺陷的证据/反证据。 template class MyDirtyPool { public: template std::size_t ad
对于这个问题,我找到了不同的答案,我知道一定有一个确定的答案。 C 中四种主要数据类型的最小分配内存大小是多少? int , double , float , 和 char是我在想什么。做 signe
我正在使用 Kafka Producer,我的应用程序将具有相同键的各个 ProducerRecords 发送到单个分区中,然后这些 ProducerRecords 在发送到代理之前进行批处理(使用
您好,我是服务器端编程 (java) 的新手,正在阅读 SendRedirect 与 Forward 之间的区别。来自 Post-redirect-get pattern它解释说这将阻止通过点击刷新按
我是一名优秀的程序员,十分优秀!