Skip to main content

Events... Serializing without them..

Alsalam alikom wa ra7mat Allah wa barakatoh,

Today I was making some project for the college when I faced the following problem :


public class Picture : PictureBox
{
public List<ShapeBase> _shapes;
}
public class ShapeBase
{
#region DataMembers
#endregion

public event EventHandler PositionChanged;
}

the problem is, if u tried to save that _shapes list, u will get an error saying that Picture Class is not marked as Serializable..
Why does C# Compiler bother itself with Picture Class ?
- Because, it's one of the subscribers to the event PositionChanged...
What if I don't care about hte subscribers ? I just care about the DataMembers in ShapeBase
- Actually this is the problem :D, be patient and u will get the solution..
Can't I just mark the event as [Nonserialized] ?
- C# Compiler will shout on ur face saying that PositionChanged is not a field.

Now the solutions,
The simplest one is instead of marking it as [NonSerialized], mark it as [field:NonSerialized] and it'll just work fine..
Why ?
- cuz this tells the C# Compiler to consider the underlying delegate field only not the event itself (the event is not a field to consider... it's a custom modified field)

The other solution is somehow more complex.. actually before knowing the first solution, I did this (cuz I couldn't think of any other way to work around)
this is about making Custom Event... actually it'll be just dummy... like this :

[NonSerialized]
private EventHandler _positionChanged;
public event EventHandler PositionChanged
{
add { _positionChanged += value; }
remove { _positionChanged -= value; }
}

Now the _positionChanged can be considered a "field" and can be marked as "NonSerialized"..

Hope that's useful...

Alsalam Alikom wa ra7mat Allah wa barakatoh

Comments

Popular posts from this blog

Windows7 adds Math Input Panel

Alsalam alikom wa ra7mat Allah wa barakatoh… I was reading a windows team post about Input Panels improvements in Windows7 [ here ]. When at the end I saw a very interesting –intuitive if you wish- new thing… which is, as you guessed, the Math Input Panel… Yes, that crappy font is mine… I “drew” that by mouse as I don’t have a tablet pen/pc. You can then paste it directly into word and it’ll recognize it as an editable equation… During my tests, the output panel (the top part) hanged, but I liked that the drawing panel was still responsive and I could still write/erase… till the top one started to respond again… One other thing to know, after you click Insert (that button down there) it copies the equation in MathML [ Wikipedia link ] format.. which is a standard way of representing equations and hence any application that recognizes the format can insert it not as an image but as a nice editable equation… If you think it recognized something wrong, you can click “Sele...

Microsoft Web Platform Installer… coming near you

The Microsoft Web Platform Installer 2.0 (Web PI) is a free tool that makes it simple to download, install and keep up-to-date with the latest components of the Microsoft Web Platform, including Internet Information Services (IIS), SQL Server Express, .NET Framework and Visual Web Developer. In addition, install popular open source ASP.NET and PHP web apps with the Web PI. Here is the code snippet if you want to spread the word :) < a href ="http://go.microsoft.com/fwlink/?LinkId=146503" title=" Get the Microsoft Web Platform " > < img src ="http://www.microsoft.com/web/media/badge/get_microsoft_web_platform.png" alt ="Get the Microsoft Web Platform" border ="0" /> </ a >

Question Google Chrome Process Isolation Model..

Alsalam alikom wa ra7mat Allah wa barakatoh Google once published this comics book about Google chrome (their Open Source Web Browser) I've linked to one page that I'm concerning about for now... Page 4, Google Chrome Comics Book It explains that Chrome will have separate process per tab, away from the benefits/concerns about this... I was accidently checking chrome's task manager (Shift + Esc) and found something that -apparently- violates this rule... As you see, tab1 process has actually spanned 3 tabs... which is a similar behavior to what IE8 does... I'm not quite sure why this happens in Chrome... but it's just a question to ask... Thanks, Haytham