|
Still, its better then the following
private int m_iData;
public int Data
{
get
{
return m_iData;
}
set
{
int iOldData = m_iData;
m_iData = value;
m_iData = iOldData
}
}
codito ergo sum
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Excellent code.A guy who wrote this code is genius.
|
|
|
|
|
COOL!!!!
That one is outstanding
Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
You guys are so negative! Try to see the good in this approach.
Using this approach one could perform consistency checks before not saving the value...
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Alternately, if at some point in time, the property was to become (or maybe was in the past) not 'readonly', applications relying on the dll wouldn't necessarily need to be recompiled to use the new dll, since the definition wouldn't change...on that item.
|
|
|
|
|
Yeach. And if some library user tried to write to a property and didn't notice that it took no effect, then after an upgrade his code could break because the old do-nothing setter would suddenly change something.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
mav.northwind wrote: Black holes are the places where God divided by 0...
Man: Hey, God, why did you make black holes?
God: I like a good BM in the morning, same as the next guy.
|
|
|
|
|
You need this scenario with XML deserialization.
|
|
|
|
|
Good point, but... Not if it's done right. I haven't done much serialization (XML or otherwise), but as I recall the class specifies which members get serialized and deserialized, so this shouldn't be a problem. You might have to override the base class' deserializer.
Or I may just be showing my ignorance.
|
|
|
|
|
PIEBALDconsult wrote: I recall the class specifies which members get serialized and deserialized, so this shouldn't be a problem.
Sometimes you want only readonly properties in XML serialization. Unfortunately for de/serialization to work, properties need to have both a getter and a setter.
|
|
|
|
|
The set accessor should then be private . However, the base class' contract may not allow that and then you're stuck.
|
|
|
|
|
PIEBALDconsult wrote: The set accessor should then be private.
No Xml serialization (I am getting tired typing that!) will choke on that. Just try it!
|
|
|
|
|
|
I had no trouble with it.
namespace Template
{
public partial class MyClass : System.Xml.Serialization.IXmlSerializable
{
public MyClass
(
)
{
}
public MyClass
(
string Name
)
{
this.Name = Name ;
return ;
}
public string
Name
{
get ;
private set ;
}
public void
WriteXml
(
System.Xml.XmlWriter Writer
)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.AppendChild ( doc.CreateElement ( "MyClass" ) ) ;
doc.DocumentElement.InnerText = this.Name ;
doc.WriteTo ( Writer ) ;
Writer.Close() ;
return ;
}
public void
ReadXml
(
System.Xml.XmlReader Reader
)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.Load ( Reader ) ;
Reader.Close() ;
this.Name = doc.DocumentElement.InnerText ;
return ;
}
public System.Xml.Schema.XmlSchema
GetSchema
(
)
{
return ( null ) ;
}
public override string
ToString
(
)
{
return ( this.Name ) ;
}
}
public partial class Template
{
private static System.Xml.Serialization.IXmlSerializable
Write
(
System.Xml.Serialization.IXmlSerializable Subject
)
{
Subject.WriteXml ( System.Xml.XmlWriter.Create ( @"C:\X.xml" ) ) ;
return ( Subject ) ;
}
private static System.Xml.Serialization.IXmlSerializable
Read
(
System.Xml.Serialization.IXmlSerializable Subject
)
{
Subject.ReadXml ( System.Xml.XmlReader.Create ( @"C:\X.xml" ) ) ;
return ( Subject ) ;
}
[System.STAThreadAttribute()]
public static int
Main
(
string[] args
)
{
int result = 0 ;
try
{
if ( args.Length > 0 )
{
MyClass x = new MyClass ( args [ 0 ] ) ;
Write ( x ) ;
MyClass y = new MyClass() ;
Read ( y ) ;
System.Console.WriteLine ( y.Name ) ;
}
}
catch ( System.Exception err )
{
System.Console.Write ( err.Message ) ;
}
return ( result ) ;
}
}
}
|
|
|
|
|
Implementing your own custom IXmlSerializable is cheating For starters you need to use the XmlSerializer. And you code should just represent a basic C# object.
Example:
<font color="Blue">using</font> System<font color="DarkBlue">;</font>
<font color="Blue">using</font> System<font color="DarkBlue">.</font>Xml<font color="DarkBlue">.</font>Serialization<font color="DarkBlue">;</font>
<font color="Blue">using</font> System<font color="DarkBlue">.</font>IO<font color="DarkBlue">;</font>
<font color="Blue">public</font> <font color="Blue">class</font> <font color="Teal">Foo</font>
<font color="DarkBlue">{</font>
<font color="Blue">public</font> <font color="Blue">int</font> Bar <font color="DarkBlue">{</font> <font color="Blue">get</font><font color="DarkBlue">;</font> <font color="Blue">private</font> <font color="Blue">set</font><font color="DarkBlue">;</font> <font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
<font color="Blue">class</font> <font color="Teal">Entrypoint</font>
<font color="DarkBlue">{</font>
<font color="Blue">static</font> <font color="Blue">void</font> Main<font color="DarkBlue">(</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="Teal">XmlSerializer</font> ser <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">XmlSerializer</font><font color="DarkBlue">(</font><font color="Blue">typeof</font><font color="DarkBlue">(</font><font color="Teal">Foo</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Blue">using</font> <font color="DarkBlue">(</font><font color="Teal">Stream</font> s <font color="DarkBlue">=</font> <font color="Teal">File</font><font color="DarkBlue">.</font>OpenWrite<font color="DarkBlue">(</font><font color="Maroon">"foo.xml"</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
ser<font color="DarkBlue">.</font>Serialize<font color="DarkBlue">(</font>s<font color="DarkBlue">,</font> <font color="Blue">new</font> <font color="Teal">Foo</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
Fails!
|
|
|
|
|
That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try.
This is like trying to use a DataAdapter on a query with a join or a view or something and then complaining that .Update won't work.
And as I said, "Not if it's done right"; I done it right, you didn't. I win, neener neener neener!
|
|
|
|
|
PIEBALDconsult wrote: That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try.
The way I showed you is exactly how web services does it.
[update] I think, it's 5:30 am, I just woke for a smoke, not thinking really [update]
modified on Saturday, March 22, 2008 11:38 PM
|
|
|
|
|
This is a continuation of the sample:
You might say, then use the XmlIgnore attribute, but in that case the property is never emitted, but what if you want to emit it, but you simply dont care about the result after deserializing it?
|
|
|
|
|
Why write a class in a particular (bad) way just to bypass a flaw in some other brain-dead class?
Dealing with a class that someone else wrote which doesn't implement the desired serialization is another matter entirely, but when you're writing the class you have control.
If you're writing a class intending it to be serialized, then it should be marked with SerializableAttribute and implement ISerializable and/or IXmlSerializable as appropriate.
"Use the right tool for the right job." -- Scotty, et al
|
|
|
|
|
THAT'S IT! That's why I had to do something similar to this coding horror.
|
|
|
|
|
You missed
public virtual Property { get {...} set {..}}
|
|
|
|
|
Well that is what I was alluding to when I said:
"
The set accessor should then be private. However, the base class' contract may not allow that and then you're stuck.
"
(In a different branch.)
However in your example, the derived class should at least call base.Property .
If the base class is abstract then you're stuck (but an exception should be thrown or something!). However, I argue that an abstract class should probably not specify that a set is required, which may be a whole new topic.
|
|
|
|
|
Found this C# beauty in 3 places in a particular class i'm updating.
<br />
if (this == null || .....<br />
{<br />
}<br />
I'm guessing the person that wrote that was going to be checking a field to see if it was null but none of the assignment code within the {} indicates what that might have been.
|
|
|
|
|
maybe it's some philosophic-thing...
(yes|no|maybe)*
|
|
|
|