473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Protecting against null events

This question is a followup to my question here:

http://groups.google.com/group/micro...c99e4742fe5fdb

I've been reading
http://www.yoda.arachsys.com/csharp/...ckchoice.shtml, and have
gotten a lot of great information from that page in general. So based
on what I've learned, I want to create a class that encapsulates the
safe use of an event, and I came up with the class at the end of this
post.

But it's not quite satisfying. I'd like to make it's strongly typed at
compile-time, and I don't know of a way to do that.

I first tried making it a generic class, with the type constrained to
be a delegate. That's when I learned that C# doesn't allow you to
constrain on a generic class based on the delegate type (compiler error
CS0702). So now I do type-checking at run-time, and clients can't see
the expected type via intellisense. Is there a better way?

It would also be nice if clients could add themselves to the handler
list using event-like syntax (i.e., the += operator), but it doesn't
seem that I'm allowed to override that operator. Am I missing
something?

Thanks for the excellent replies to the original question. Here's the
class as it exists now:

using System;

// The SafeEvent class is to ease the burden of using events in a
// thread-safe manner, which is surprisingly difficult.
// This code is adapted from the following webpage:
// http://www.yoda.arachsys.com/csharp/...ckchoice.shtml
public class SafeEvent
{
private object m_eventLock;
private Delegate m_event;
private Type m_eventType;

public SafeEvent(Type t)
{
if(!(t is Delegate))
{
throw new ArgumentException(
"SafeEvent requires a delegate type argument");
}

m_eventType = t;
m_eventLock = new object();
m_event = null;
}

public void Invoke(object[] args)
{
Delegate d;
lock(m_eventLock)
{
d = m_event;
}
if(d != null)
{
d.DynamicInvoke(args);
}
}

public void AddHandler(Delegate handler)
{
if(handler.GetType() != m_eventType)
{
throw new ArgumentException(
"Expected handler of type "
+ m_eventType.ToString());
}

lock(m_eventLock)
{
m_event = Delegate.Combine(m_event, handler);
}
}

public void RemoveHandler(Delegate handler)
{
if(handler.GetType() != m_eventType)
{
throw new ArgumentException(
"Expected handler of type "
+ m_eventType.ToString());
}

lock(m_eventLock)
{
m_event = Delegate.Remove(m_event, handler);
}
}
}

Nov 21 '06 #1
1 1300
Sorry, missed a compiler warning before I posted the code. please
ignore this clause from the class constructor:
if(!(t is Delegate))
{
throw new ArgumentException(
"SafeEvent requires a delegate type argument");
}
Nov 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Alan Sheehan | last post by:
Hi pythonistas, I am looking for methods of deploying applications with end users so that the python code is tamper proof. What are my options ? I understand I can supply .pyc or .pyo files...
5
by: John | last post by:
Dear all, I've got a security question that is so difficult that "maybe" there will be no answer for it. It's regarding protecting asp code. I did write some asp code, that I sell to...
1
by: Brendon | last post by:
I have 2 Xsd's The one contains basic type definitions <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"...
7
by: Bobby C. | last post by:
My company is in the process of getting ready (well actually QTR 2 2004) to roll out a rewritten version of a vertical market application for the municipal market (small and medium sized cities). ...
6
by: Roman Werpachowski | last post by:
In a recent thread http://tinyurl.com/8n7fe I asked about preventing the user from deleting the object pointed to by a pointer/reference. Now I would like to ask about a different aspect of this...
17
by: (PeteCresswell) | last post by:
I've got apps where you *really* wouldn't want to delete certain items by accident, but the users just have to have a "Delete" button. My current strategies: Plan A:...
22
by: flit | last post by:
Hello All, I have a hard question, every time I look for this answer its get out from the technical domain and goes on in the moral/social domain. First, I live in third world with bad gov., bad...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: Evolution445 | last post by:
Hi, I have a small anti-cheat program for a game, and I want to protect it against people trying to bypass or modify the program. What I thought of myself was - no idea if this is possible -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.