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
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
Post a Comment