English 中文(简体)
提及C#中的物体
原标题:Storing a reference to a object in C#


I m currently working on a VariableWatcher class in C#.
What I want to do, is to raise a event every time a variable is changed to a specific value.
For example: I have a bool a , which is false. I pass it to the VariableWatcher class thogether with the value on which the event should be raised, true.
I already made an approach to the solution of this problem, but I obviously misunderstood some point about boxing/unboxing.

这是我的法典:

  public class ValueWatch
  {
    public delegate void ValueTriggeredD(string ID, string valuename, object source);
    public event ValueTriggeredD ValueTriggered;

    Dictionary<string, object[]> LookUp = new Dictionary<string, object[]>(); //Key = ID, Value[0] = PropertyName, Value[1] = ObjTrigger, Value[2] = ObjWatch
    Timer ttt;

    public void Initialize()
    {
        ttt = new Timer(new TimerCallback(CB_T), null, 0, 50);  
    }
    void CB_T(object o)
    {
        if (LookUp.Count > 0)
        {
            foreach (KeyValuePair<string, object[]> kvp in LookUp)
            {
                Type tp = kvp.Value[2].GetType();
                FieldInfo inf = tp.GetField((string)kvp.Value[0]);
                if (inf != null)
                {
                    object curval = inf.GetValue(kvp.Value[2]);
                    if (curval == kvp.Value[1])
                    {
                        if (ValueTriggered != null)
                            ValueTriggered(kvp.Key, (string)kvp.Value[0], kvp.Value[2]);
                    }
                }
            }
        }
    }
    public void StartWatching(string ID, object ListObj, object ValueForTrigger, string PropertyName)
    {
        if (LookUp.ContainsKey(ID))
            System.Diagnostics.Debug.Print("already there");
        else
        {
            LookUp.Add(ID, new object[] { PropertyName, ValueForTrigger, ListObj });
        }
    }
    public void StopWatching(string ID)
    {
        LookUp.Remove(ID);
    }
}

我的问题是:在我改变价值时,为什么储存在数值中的物体不会改变?

例:

bool b = false;
valueWatch.ValueTriggered += new ValueTriggeredD(this.VT);
valueWatch.StartWatching("ID1", this, true, "b");
...
b = true;
...
void VT(string ID, string VN, object Source)
{
    //Never triggered
}
最佳回答
问题回答

我最后指出:

我在标的上设定了数值,而不是将其放在标的上。 www.un.org/chinese/ga/president





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签