Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel and re-enable a CheckListBox ItemCheck event

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I am trying to find out how to generically cancel and re-enable an event on a CheckListBox.

I could do it this way, if I know the event:

public void cancelevent()
{
checkedListBox1.ItemCheck -= new ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
}

but I have 5 CheckListBoxes that I want to do this for and want to pass the CheckListBox in an disable the ItemCheck event (whatever it is), do something and then re-enable it. In this case, to programmatically check or uncheck a checkbox without the event firing.

Is there something like:

checkListBox1.ItemEvent.<on or off>,

or someway I can get the event name and use that with the above method?

Thanks,

Tom
 
Hmm, well, the Type class does include a method called GetEvents(). So using some reflection we should be able to get the list of FieldInfo objects and EventInfo objects. Once you have the EventInfo you can use that to register/deregister events from an object. Something like this MIGHT work. As a HUGE warning, this will cause some some issues. I would never use this just to clear events (without removing the dictionary portion) because it is going to basically keep a reference to whichever object provided the Delegate, meaning it won't ever get garbage collected. I would recommend against using the remove all events (in this form) without always calling the rehook so that things can proceed normally after that. May want to consider trying to work some weak references into this. I will test it later.

EDIT: Re-posted with color because god that was hard to read.

Code:
[COLOR=#0000FF]using[/color] System;
[COLOR=#0000FF]using[/color] System.Collections.Generic;
[COLOR=#0000FF]using[/color] System.ComponentModel;
[COLOR=#0000FF]using[/color] System.Reflection;
 
[COLOR=#0000FF]namespace[/color] RemoveAllEventsTest
{
    [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]EventHelper[/color]
    {
        [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]Dictionary[/color]&lt;[COLOR=#0000FF]object[/color], [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;&gt; removedEvents = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Dictionary[/color]&lt;[COLOR=#0000FF]object[/color], [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;&gt;();
 
        [COLOR=#0000FF]private[/color] [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]FieldInfo[/color]&gt; GetEventFields([COLOR=#2B91AF]Type[/color] t)
        {
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]FieldInfo[/color]&gt; fields = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]FieldInfo[/color]&gt;();
 
            [COLOR=#2B91AF]EventInfo[/color]&#91;&#93; eventInfos = t.GetEvents([COLOR=#2B91AF]BindingFlags[/color].IgnoreCase | [COLOR=#2B91AF]BindingFlags[/color].Public | [COLOR=#2B91AF]BindingFlags[/color].Instance | [COLOR=#2B91AF]BindingFlags[/color].Static | [COLOR=#2B91AF]BindingFlags[/color].NonPublic);
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]EventInfo[/color] eventInfo [COLOR=#0000FF]in[/color] eventInfos)
            {
                [COLOR=#2B91AF]Type[/color] declaringType = eventInfo.DeclaringType;
                [COLOR=#2B91AF]FieldInfo[/color] fieldInfo = declaringType.GetField(eventInfo.Name, [COLOR=#2B91AF]BindingFlags[/color].IgnoreCase | [COLOR=#2B91AF]BindingFlags[/color].Public | [COLOR=#2B91AF]BindingFlags[/color].Instance | [COLOR=#2B91AF]BindingFlags[/color].Static | [COLOR=#2B91AF]BindingFlags[/color].NonPublic);
                [COLOR=#0000FF]if[/color] (fieldInfo != [COLOR=#0000FF]null[/color])
                    fields.Add(fieldInfo);
            }
 
            [COLOR=#0000FF]return[/color] fields;
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] RehookAllEvent([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#0000FF]if[/color] (!removedEvents.ContainsKey(obj))
                [COLOR=#0000FF]throw[/color] [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Exception[/color]([COLOR=#A31515]&quot;Object has not had its events previously removed&quot;[/color]);
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt; events = removedEvents&#91;obj&#93;;
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]RemovedEvent[/color] ev [COLOR=#0000FF]in[/color] events)
                ev.EventInfo.AddEventHandler(obj, ev.Method);
            removedEvents.Remove(obj);
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] RemoveAllEvents([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#2B91AF]Type[/color] t = obj.GetType();
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]FieldInfo[/color]&gt; eventFields = GetEventFields(t);
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]FieldInfo[/color] eventField [COLOR=#0000FF]in[/color] eventFields)
            {
                [COLOR=#2B91AF]EventInfo[/color] eventInfo = t.GetEvent(eventField.Name, [COLOR=#2B91AF]BindingFlags[/color].IgnoreCase | [COLOR=#2B91AF]BindingFlags[/color].Public | [COLOR=#2B91AF]BindingFlags[/color].Instance | [COLOR=#2B91AF]BindingFlags[/color].Static | [COLOR=#2B91AF]BindingFlags[/color].NonPublic);
                [COLOR=#0000FF]if[/color] (eventInfo == [COLOR=#0000FF]null[/color])
                    [COLOR=#0000FF]continue[/color];
 
                [COLOR=#2B91AF]Delegate[/color] eventDelegate = [COLOR=#0000FF]null[/color];
 
                [COLOR=#0000FF]if[/color] (!eventField.IsStatic)
                    eventDelegate = eventField.GetValue(obj) [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]Delegate[/color];
                [COLOR=#0000FF]else[/color]
                {
                    [COLOR=#2B91AF]MethodInfo[/color] methodInfo = t.GetMethod([COLOR=#A31515]&quot;get_Events&quot;[/color], [COLOR=#2B91AF]BindingFlags[/color].IgnoreCase | [COLOR=#2B91AF]BindingFlags[/color].Public | [COLOR=#2B91AF]BindingFlags[/color].Static | [COLOR=#2B91AF]BindingFlags[/color].NonPublic);
                    [COLOR=#2B91AF]EventHandlerList[/color] eventList = methodInfo.Invoke(obj, [COLOR=#0000FF]new[/color] [COLOR=#0000FF]object[/color]&#91;&#93; { }) [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]EventHandlerList[/color];
                    [COLOR=#0000FF]if[/color] (eventList == [COLOR=#0000FF]null[/color])
                        [COLOR=#0000FF]continue[/color];
 
                    [COLOR=#0000FF]object[/color] staticField = eventField.GetValue(obj);
                    eventDelegate = eventList&#91;staticField&#93;;
                }
 
                [COLOR=#0000FF]if[/color] (eventDelegate == [COLOR=#0000FF]null[/color])
                    [COLOR=#0000FF]continue[/color];
 
                [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt; eventMethods = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;();
                [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]Delegate[/color] method [COLOR=#0000FF]in[/color] eventDelegate.GetInvocationList())
                {
                    [COLOR=#2B91AF]RemovedEvent[/color] re = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]RemovedEvent[/color]() { EventInfo = eventInfo, Method = method };
                    eventMethods.Add(re);
                    eventInfo.RemoveEventHandler(obj, method);
                }
                removedEvents.Add(obj, eventMethods);
            }
        }
        [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]RemovedEvent[/color]
        {
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]EventInfo[/color] EventInfo { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]Delegate[/color] Method { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
        }
    }
}
 
I usually set a global boolean flag to true at the start of the code where I'm changing the values of the checkboxes and then set it back to false when I'm done. Then, in the event handler, I add a check for the value of the flag and only process the code in the event handler if the flag is false.

The code I use to force the checked state into all of the child nodes of a tree node that has changed looks something like this:

private void tvRptFolders_AfterCheck(object sender, TreeViewEventArgs e)
{
if (!nodeChecking)
{
nodeChecking = true;
((TreeFolderNode)e.Node).Check(e.Node.Checked);
if (!e.Node.Checked && cbSelAll.Checked)
{
selectAll = true;
cbSelAll.Checked = false;
selectAll = false;
}
nodeChecking = false;
}
}

Something like this could be adapted for use in your CheckListBox.

-Dell

DecisionFirst Technologies - Seven-time SAP BusinessObjects Solution Partner of the Year
 
Ah. If all you're worried about is the single checked event, then yes, it would be much simpler to just disable the event via a bool when you're pro-grammatically updating.

Also, if anyone is looking at this.. after some testing, it seems reflection doesn't make getting at the event's field easy. This will still miss events, but most events follow a pattern where this code will work:

Code:
[COLOR=#0000FF]using[/color] System;
[COLOR=#0000FF]using[/color] System.Collections.Generic;
[COLOR=#0000FF]using[/color] System.ComponentModel;
[COLOR=#0000FF]using[/color] System.Reflection;
 
[COLOR=#0000FF]namespace[/color] RemoveAllEventsTest
{
    [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]EventHelper[/color]
    {
        [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]Dictionary[/color]&lt;[COLOR=#0000FF]object[/color], [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;&gt; removedEvents = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Dictionary[/color]&lt;[COLOR=#0000FF]object[/color], [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;&gt;();
        [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]BindingFlags[/color] AllFlags = [COLOR=#2B91AF]BindingFlags[/color].IgnoreCase | [COLOR=#2B91AF]BindingFlags[/color].Public | [COLOR=#2B91AF]BindingFlags[/color].Instance | [COLOR=#2B91AF]BindingFlags[/color].Static | [COLOR=#2B91AF]BindingFlags[/color].NonPublic;
        [COLOR=#0000FF]private[/color] [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt; GetEventFields([COLOR=#2B91AF]Type[/color] t)
        {
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt; fields = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt;();
 
            [COLOR=#2B91AF]EventInfo[/color]&#91;&#93; eventInfos = t.GetEvents();
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]EventInfo[/color] eventInfo [COLOR=#0000FF]in[/color] eventInfos)
            {
                [COLOR=#2B91AF]Type[/color] declaringType = eventInfo.DeclaringType;
                [COLOR=#2B91AF]FieldInfo[/color] fieldInfo = declaringType.GetField(eventInfo.Name, AllFlags);
                [COLOR=#0000FF]if[/color] (fieldInfo == [COLOR=#0000FF]null[/color])
                    fieldInfo = declaringType.GetField([COLOR=#A31515]&quot;Event&quot;[/color] + eventInfo.Name, AllFlags);
                [COLOR=#0000FF]if[/color] (fieldInfo != [COLOR=#0000FF]null[/color])
                    fields.Add([COLOR=#0000FF]new[/color] [COLOR=#2B91AF]EventFound[/color]() { FieldInfo = fieldInfo, EventInfo = eventInfo });
            }
 
            [COLOR=#0000FF]return[/color] fields;
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] ToggleAllEvents([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#0000FF]if[/color] (removedEvents.ContainsKey(obj))
                RehookAllEvent(obj);
            [COLOR=#0000FF]else[/color]
                RemoveAllEvents(obj);
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] RehookAllEvent([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#0000FF]if[/color] (!removedEvents.ContainsKey(obj))
                [COLOR=#0000FF]throw[/color] [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Exception[/color]([COLOR=#A31515]&quot;Object has not had its events previously removed&quot;[/color]);
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt; events = removedEvents&#91;obj&#93;;
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]RemovedEvent[/color] ev [COLOR=#0000FF]in[/color] events)
                ev.EventInfo.AddEventHandler(obj, ev.Method);
            removedEvents.Remove(obj);
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] RemoveAllEvents([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#2B91AF]Type[/color] t = obj.GetType();
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt; eventFields = GetEventFields(t);
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]EventFound[/color] eventField [COLOR=#0000FF]in[/color] eventFields)
            {
                [COLOR=#2B91AF]Delegate[/color] eventDelegate = [COLOR=#0000FF]null[/color];
 
                [COLOR=#0000FF]if[/color] (!eventField.FieldInfo.IsStatic)
                    eventDelegate = eventField.FieldInfo.GetValue(obj) [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]Delegate[/color];
                [COLOR=#0000FF]else[/color]
                {
                    [COLOR=#2B91AF]MethodInfo[/color] methodInfo = t.GetMethod([COLOR=#A31515]&quot;get_Events&quot;[/color], AllFlags);
                    [COLOR=#2B91AF]EventHandlerList[/color] eventList = methodInfo.Invoke(obj, [COLOR=#0000FF]new[/color] [COLOR=#0000FF]object[/color]&#91;&#93; { }) [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]EventHandlerList[/color];
                    [COLOR=#0000FF]if[/color] (eventList == [COLOR=#0000FF]null[/color])
                        [COLOR=#0000FF]continue[/color];
 
                    [COLOR=#0000FF]object[/color] staticField = eventField.FieldInfo.GetValue(obj);
                    eventDelegate = eventList&#91;staticField&#93;;
                }
 
                [COLOR=#0000FF]if[/color] (eventDelegate == [COLOR=#0000FF]null[/color])
                    [COLOR=#0000FF]continue[/color];
 
                [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt; eventMethods = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;();
                [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]Delegate[/color] method [COLOR=#0000FF]in[/color] eventDelegate.GetInvocationList())
                {
                    [COLOR=#2B91AF]RemovedEvent[/color] re = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]RemovedEvent[/color]() { EventInfo = eventField.EventInfo, Method = method };
                    eventMethods.Add(re);
                    eventField.EventInfo.RemoveEventHandler(obj, method);
                }
                removedEvents.Add(obj, eventMethods);
            }
        }
        [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]RemovedEvent[/color]
        {
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]EventInfo[/color] EventInfo { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]Delegate[/color] Method { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
        }
        [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]EventFound[/color]
        {
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]EventInfo[/color] EventInfo { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]FieldInfo[/color] FieldInfo { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
        }
    }
}

Working test code:

Code:
[COLOR=#0000FF]using[/color] System;
[COLOR=#0000FF]using[/color] System.Windows.Forms;
 
[COLOR=#0000FF]namespace[/color] RemoveAllEventsTest
{
    [COLOR=#0000FF]public[/color] [COLOR=#0000FF]partial[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Form1[/color] : [COLOR=#2B91AF]Form[/color]
    {
        [COLOR=#0000FF]public[/color] Form1()
        {
            InitializeComponent();
        }
 
        [COLOR=#0000FF]private[/color] [COLOR=#0000FF]void[/color] button1_Click([COLOR=#0000FF]object[/color] sender, [COLOR=#2B91AF]EventArgs[/color] e)
        {
            [COLOR=#2B91AF]MessageBox[/color].Show([COLOR=#A31515]&quot;Hello!&quot;[/color]);
        }
 
        [COLOR=#0000FF]private[/color] [COLOR=#0000FF]void[/color] button2_Click([COLOR=#0000FF]object[/color] sender, [COLOR=#2B91AF]EventArgs[/color] e)
        {
            [COLOR=#2B91AF]EventHelper[/color].ToggleAllEvents(button1);
        }
    }
}
 
Changed it to include Weak References. Tested by disposing an object I had done an remove events with. Seems to work fine.

Code:
[COLOR=#0000FF]using[/color] System;
[COLOR=#0000FF]using[/color] System.Collections.Generic;
[COLOR=#0000FF]using[/color] System.ComponentModel;
[COLOR=#0000FF]using[/color] System.Linq;
[COLOR=#0000FF]using[/color] System.Reflection;
 
[COLOR=#0000FF]namespace[/color] CustomLibraries
{
    [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]EventHelper[/color]
    {
        [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvents[/color]&gt; removedEvents = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvents[/color]&gt;();
        [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]BindingFlags[/color] AllFlags = [COLOR=#2B91AF]BindingFlags[/color].IgnoreCase | [COLOR=#2B91AF]BindingFlags[/color].Public | [COLOR=#2B91AF]BindingFlags[/color].Instance | [COLOR=#2B91AF]BindingFlags[/color].Static | [COLOR=#2B91AF]BindingFlags[/color].NonPublic;
        [COLOR=#0000FF]private[/color] [COLOR=#0000FF]static[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt; GetEventFields([COLOR=#2B91AF]Type[/color] t)
        {
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt; fields = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt;();
 
            [COLOR=#2B91AF]EventInfo[/color]&#91;&#93; eventInfos = t.GetEvents();
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]EventInfo[/color] eventInfo [COLOR=#0000FF]in[/color] eventInfos)
            {
                [COLOR=#2B91AF]Type[/color] declaringType = eventInfo.DeclaringType;
                [COLOR=#2B91AF]FieldInfo[/color] fieldInfo = declaringType.GetField(eventInfo.Name, AllFlags);
                [COLOR=#0000FF]if[/color] (fieldInfo == [COLOR=#0000FF]null[/color])
                    fieldInfo = declaringType.GetField([COLOR=#A31515]&quot;Event&quot;[/color] + eventInfo.Name, AllFlags);
                [COLOR=#0000FF]if[/color] (fieldInfo != [COLOR=#0000FF]null[/color])
                    fields.Add([COLOR=#0000FF]new[/color] [COLOR=#2B91AF]EventFound[/color]() { FieldInfo = fieldInfo, EventInfo = eventInfo });
            }
 
            [COLOR=#0000FF]return[/color] fields;
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] ToggleAllEvents([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#0000FF]if[/color] (removedEvents.Where(r =&gt; r.Object == obj).Count() &gt; 0)
                RehookAllEvent(obj);
            [COLOR=#0000FF]else[/color]
                RemoveAllEvents(obj, [COLOR=#0000FF]true[/color]);
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]bool[/color] RehookAllEvent([COLOR=#0000FF]object[/color] obj)
        {
            [COLOR=#0000FF]var[/color] item = removedEvents.Where(r =&gt; r.Object == obj);
            [COLOR=#0000FF]if[/color] (item.Count() == 0)
                [COLOR=#0000FF]return[/color] [COLOR=#0000FF]false[/color];
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]RemovedEvent[/color] ev [COLOR=#0000FF]in[/color] item.First().Events)
                [COLOR=#0000FF]if[/color] (ev.Method != [COLOR=#0000FF]null[/color] &amp;&amp; ev.EventInfo != [COLOR=#0000FF]null[/color])
                    ev.EventInfo.AddEventHandler(obj, ev.Method);
            removedEvents.Remove(item.First());
            [COLOR=#0000FF]return[/color] [COLOR=#0000FF]true[/color];
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] RemoveAllEvents([COLOR=#0000FF]object[/color] obj, [COLOR=#0000FF]bool[/color] storeEvents)
        {
            [COLOR=#2B91AF]Type[/color] t = obj.GetType();
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]EventFound[/color]&gt; eventFields = GetEventFields(t);
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]EventFound[/color] eventField [COLOR=#0000FF]in[/color] eventFields)
            {
                [COLOR=#2B91AF]Delegate[/color] eventDelegate = [COLOR=#0000FF]null[/color];
 
                [COLOR=#0000FF]if[/color] (!eventField.FieldInfo.IsStatic)
                    eventDelegate = eventField.FieldInfo.GetValue(obj) [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]Delegate[/color];
                [COLOR=#0000FF]else[/color]
                {
                    [COLOR=#2B91AF]MethodInfo[/color] methodInfo = t.GetMethod([COLOR=#A31515]&quot;get_Events&quot;[/color], AllFlags);
                    [COLOR=#2B91AF]EventHandlerList[/color] eventList = methodInfo.Invoke(obj, [COLOR=#0000FF]new[/color] [COLOR=#0000FF]object[/color]&#91;&#93; { }) [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]EventHandlerList[/color];
                    [COLOR=#0000FF]if[/color] (eventList == [COLOR=#0000FF]null[/color])
                        [COLOR=#0000FF]continue[/color];
 
                    [COLOR=#0000FF]object[/color] staticField = eventField.FieldInfo.GetValue(obj);
                    eventDelegate = eventList&#91;staticField&#93;;
                }
 
                [COLOR=#0000FF]if[/color] (eventDelegate == [COLOR=#0000FF]null[/color])
                    [COLOR=#0000FF]continue[/color];
 
                [COLOR=#2B91AF]RemovedEvents[/color] res = [COLOR=#0000FF]null[/color];
                [COLOR=#0000FF]if[/color] (storeEvents)
                    res = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]RemovedEvents[/color](obj);
                [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]Delegate[/color] method [COLOR=#0000FF]in[/color] eventDelegate.GetInvocationList())
                {
                    [COLOR=#0000FF]if[/color] (storeEvents)
                    {
                        [COLOR=#2B91AF]RemovedEvent[/color] re = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]RemovedEvent[/color](eventField.EventInfo, method, obj);
                        res.AddEvent(re);
                    }
                    eventField.EventInfo.RemoveEventHandler(obj, method);
                }
                [COLOR=#0000FF]if[/color] (storeEvents)
                    removedEvents.Add(res);
            }
        }
        [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]RemovedEvents[/color]
        {
            [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt; _RemovedEvents;
            [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]WeakReference[/color] _Object;
 
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]object[/color] Object { [COLOR=#0000FF]get[/color] { [COLOR=#0000FF]return[/color] _Object.Target; } }
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt; Events { [COLOR=#0000FF]get[/color] { [COLOR=#0000FF]return[/color] _RemovedEvents; } }
 
            [COLOR=#0000FF]public[/color] RemovedEvents([COLOR=#0000FF]object[/color] obj)
            {
                _Object = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]WeakReference[/color](obj, [COLOR=#0000FF]false[/color]);
                _RemovedEvents = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]RemovedEvent[/color]&gt;();
            }
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]void[/color] AddEvent([COLOR=#2B91AF]RemovedEvent[/color] re)
            {
                _RemovedEvents.Add(re);
            }
        }
        [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]RemovedEvent[/color]
        {
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]EventInfo[/color] EventInfo { [COLOR=#0000FF]get[/color] { [COLOR=#0000FF]return[/color] _EventInfo.Target [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]EventInfo[/color]; } }
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]Delegate[/color] Method { [COLOR=#0000FF]get[/color] { [COLOR=#0000FF]return[/color] _Method.Target [COLOR=#0000FF]as[/color] [COLOR=#2B91AF]Delegate[/color]; } }
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]object[/color] Object { [COLOR=#0000FF]get[/color] { [COLOR=#0000FF]return[/color] _Object.Target; } }
 
            [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]WeakReference[/color] _EventInfo;
            [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]WeakReference[/color] _Method;
            [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]WeakReference[/color] _Object;
 
            [COLOR=#0000FF]public[/color] RemovedEvent([COLOR=#2B91AF]EventInfo[/color] ev, [COLOR=#2B91AF]Delegate[/color] m, [COLOR=#0000FF]object[/color] target)
            {
                _EventInfo = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]WeakReference[/color](ev, [COLOR=#0000FF]false[/color]);
                _Method = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]WeakReference[/color](m, [COLOR=#0000FF]false[/color]);
                _Object = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]WeakReference[/color](target, [COLOR=#0000FF]false[/color]);
            }
        }
        [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]EventFound[/color]
        {
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]EventInfo[/color] EventInfo { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#2B91AF]FieldInfo[/color] FieldInfo { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
        }
    }
}
 
New to C#, simple question.

I was told many times: "Nothing equals to NULL, not even a NULL".

But what I see here:
Code:
if (fieldInfo == null)

[ponder]

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
If you're working in SQL a database, you need to use "is null" or "is not null" - in those terms, nothing equals to null". However, in many programming languages, including C# and Java, you use "== null" to determine whether an object has a null value.

-Dell

DecisionFirst Technologies - Seven-time SAP BusinessObjects Solution Partner of the Year
 
In C# all objects are just references - they don't store the value, they store a reference to where the object is in memory. This is done in the background of course and you don't have to map it out yourself, but you have to have some way to check whether your variable actually has a valid reference or not. If the variable doesn't currently point to an instantiated object, it said to be null. null is a literal in the C# language that means "a reference to nothing", basically.

Edit: See the MSDN for more information if you want it
C# Value Types:
C# Reference Types:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top