在.NET中的只读域能成为空? [英] Can a readonly field in .NET become null?

查看:106
本文介绍了在.NET中的只读域能成为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程的应用程序一个奇怪的错误:

I have a strange bug in a multithreaded app:

public class MyClass
{
   private readonly Hashtable HashPrefs;
   public MyClass(int id)
  {
     HashPrefs = new Hashtable();
  } 

  public void SomeMethodCalledFromAnotherThread(string hashKey,string hashValue)
 {
    if (HashPrefs.Contains(hashKey))  // <-- throws NullReferenceException
    {

    }
 }
}

一个线程做的:

 SomeQueue.Add(new MyClass(1));

和另一个线程做的:

 SomeQueue.Dequeue().SomeMethodCalledFromAnotherThread(SomeClass.SomeMethod(),"const value");



但如何能在第二个线程调用的方法构造完成之前?

But how can the second thread call the method before the constructor is finished?

编辑:我用函数参数添加的部分,因为它似乎这可能是相关的。
至于我可以告诉大家,传递的hashKey不能为空,为的someMethod()总是返回相关的字符串。

I added the part with the function parameters, as it seems this might be relevant. As far as I can tell, the hashKey being passed on cannot be null, as SomeMethod() always returns a relevant string.

正如其他人所指出的如果问题是传递给包含()空haskKey参数,异常会ArgumentNullException。

As others have pointed out,if the problem was a null haskKey parameter passed to the Contains() , the exception would be ArgumentNullException.

推荐答案

思考一个办法做到这一点( SetField )。

Reflection is one way to do this (SetField).

要得到这种好奇心的第二种方法是,如果你放弃参考太早了,通过传递这个(或sometihng涉及一个隐含的这个,如捕获到一个匿名方法的领域/拉姆达)从 .ctor 的:

A second way to get this curiosity is if you give away the reference too early, by passing this (or sometihng involving an implicit this, such as a field captured into an anonymous method / lambda) out of the .ctor:

public MyClass(int id)
{
    Program.Test(this); // oopsie ;-p
    HashPrefs = new Hashtable();
}

或更可能(给定的问题):

Or more likely (given the question):

SomeQueue.Add(this);



另一个相关的问题可能是 - 能不摆在首位分配?而这个问题的答案是肯定的,特别是如果你使用的是序列化。流行的看法相反,你其实可以绕过构造函数,的如果的你有充分的理由; 的DataContractSerializer 是什么一个很好的例子,这是否......

Another relevant question might be - can it not be assigned in the first place? And the answer to that is yes, especially if you are using serialization. Contrary to popular belief, you can actually bypass the constructors, if you have a reason; DataContractSerializer is a good example of something that does this...

下面将创建一个 MyClass的与空字段:

The following will create a MyClass with a null field:

MyClass obj = (MyClass)
    System.Runtime.Serialization.FormatterServices.GetUninitializedObject(
        typeof(MyClass));

这篇关于在.NET中的只读域能成为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆