473,403 Members | 2,071 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,403 software developers and data experts.

Serialization of indexers

I'm writing an XML serializer (similar to the MS XMLSerializer). It uses
reflection to get the values from a class/struct. The one area I'm confused
on is indexers. I can get the indexer PropertyInfo and the ParameterInfo
stuff, but I can't possibly know the range of valid values nor what the
underlying storage is.

How is this normally handled?

Should I just ignore the indexer and simply serialize the fields?

Pete
Nov 17 '05 #1
6 2545
Pete Davis wrote:
I'm writing an XML serializer (similar to the MS XMLSerializer). It
uses reflection to get the values from a class/struct. The one area
I'm confused on is indexers. I can get the indexer PropertyInfo and
the ParameterInfo stuff, but I can't possibly know the range of valid
values nor what the underlying storage is.

How is this normally handled?

Should I just ignore the indexer and simply serialize the fields?


Yes, ignore it. Take a cue from .NET serialization. It will only
serialize fields, it will not serialize properties (an indexer is a
property). This is for good reason because if a property gives access to
a field then you will serialize it elsewhere, and if it gives access to
data elsewhere (for example a database or a webservice) then that is a
different serialization issue.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 17 '05 #2
> Yes, ignore it. Take a cue from .NET serialization. It will only serialize
fields, it will not serialize properties (an indexer is a property).
Actually, .Net *will* serialize properties, as long as they have a get and a
set accessor method.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Richard Grimes" <ri******@mvps.org> wrote in message
news:uL**************@TK2MSFTNGP10.phx.gbl... Pete Davis wrote:
I'm writing an XML serializer (similar to the MS XMLSerializer). It
uses reflection to get the values from a class/struct. The one area
I'm confused on is indexers. I can get the indexer PropertyInfo and
the ParameterInfo stuff, but I can't possibly know the range of valid
values nor what the underlying storage is.

How is this normally handled?

Should I just ignore the indexer and simply serialize the fields?


Yes, ignore it. Take a cue from .NET serialization. It will only serialize
fields, it will not serialize properties (an indexer is a property). This
is for good reason because if a property gives access to a field then you
will serialize it elsewhere, and if it gives access to data elsewhere (for
example a database or a webservice) then that is a different serialization
issue.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm

Nov 17 '05 #3
XmlSerializer serializes public properties and fields.

I've spent a couple hours going through the code in reflector, but it's just
a nightmare to understand, largely because the XmlSerializer is creating
compiling the actual serializer into a "temporary" assembly which is then
executed. What I really need, is to see the code from the temporary assembly
so I can see how it works.

BTW, I put "temporary" in quotes because the assembly is generated in the
application's appdomain, so it can't be released until the AppDomain shuts
down.

Pete

"Richard Grimes" <ri******@mvps.org> wrote in message
news:uL**************@TK2MSFTNGP10.phx.gbl...
Pete Davis wrote:
I'm writing an XML serializer (similar to the MS XMLSerializer). It
uses reflection to get the values from a class/struct. The one area
I'm confused on is indexers. I can get the indexer PropertyInfo and
the ParameterInfo stuff, but I can't possibly know the range of valid
values nor what the underlying storage is.

How is this normally handled?

Should I just ignore the indexer and simply serialize the fields?


Yes, ignore it. Take a cue from .NET serialization. It will only serialize
fields, it will not serialize properties (an indexer is a property). This
is for good reason because if a property gives access to a field then you
will serialize it elsewhere, and if it gives access to data elsewhere (for
example a database or a webservice) then that is a different serialization
issue.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm

Nov 17 '05 #4
Kevin Spencer wrote:
Yes, ignore it. Take a cue from .NET serialization. It will only
serialize fields, it will not serialize properties (an indexer is a
property).


Actually, .Net *will* serialize properties, as long as they have a
get and a set accessor method.


Rubbish. Read up about serialization and learn how it works.

Apart from anything else, it makes no sense for properties to be
serialized. If you have a property that is based on a field then *two*
values will be serialized and that then raises the question of which
value will be used to 'initialize' the property. If the property is used
to calculate a value at runtime (for example, reads an item from a
database) then it will be dependent upon the actual time when the data
is read and serialization of such values makes no sense.

Just to prove that you are wrong, try this:

[Serializable]
public class DataItem
{
int data;
public int Data
{
get{return data;}
set{data=value;}
}
}

Then:

SoapFormatter sf = new SoapFormatter();
DataItem dataitem = new DataItem();
dataitem.Data = 42;
using (FileStream fs = File.OpenWrite("test.soap"))
{
sf.Serialize(fs, dataitem);
}

Run this code and view the file. You'll find an entry for DataItem.data,
and not for DataItem.Data:

<DataItem>
<data>42</data>
</DataItem>

Change DataItem to:

[Serializable]
public class DataItem
{
public int Data
{
get{return 42;}
set{Console::WriteLine("dont know what to do with {0}", value);}
}
}

Run the code again. You'll find no mention of Data.

You're wrong on this one, Kevin.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 19 '05 #5
Pete Davis wrote:
XmlSerializer serializes public properties and fields.
Yes. But it is not .NET runtime serialization :-)
I've spent a couple hours going through the code in reflector, but
it's just a nightmare to understand, largely because the
XmlSerializer is creating compiling the actual serializer into a
"temporary" assembly which is then executed. What I really need, is
to see the code from the temporary assembly so I can see how it works.

BTW, I put "temporary" in quotes because the assembly is generated in
the application's appdomain, so it can't be released until the
AppDomain shuts down.


It is actually saved to the user's temporary folder. When I tested this
out a while ago I put a FileSystemWatcher on (IIRC) %USERPROFILE%\Local
Settings\Temp and saw the generated assembly. I don't have the code at
hand but I think I implemented the event handler to copy the file to
another folder. To be honest I cannot remember whether it was
successful.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 19 '05 #6
Richard Grimes wrote:
It is actually saved to the user's temporary folder. When I tested
this out a while ago I put a FileSystemWatcher on (IIRC)
%USERPROFILE%\Local Settings\Temp and saw the generated assembly. I
don't have the code at hand but I think I implemented the event
handler to copy the file to another folder. To be honest I cannot
remember whether it was successful.


Ah here it is, pretty rough and read, but it works:

using System;
using System.IO;

class App
{
static void Main()
{
string user = Environment.GetEnvironmentVariable("USERPROFILE");
user += @"\Local Settings\Temp";
FileSystemWatcher fsw = new FileSystemWatcher(user);
fsw.Created += new FileSystemEventHandler(newFile);
fsw.EnableRaisingEvents = true;
Console.WriteLine("press enter to finish");
Console.ReadLine();
fsw.EnableRaisingEvents = false;
fsw.Dispose();
}
// Make sure you create C:\temp
static void newFile(object o, FileSystemEventArgs args)
{
try
{
File.Copy(args.FullPath, @"C:\temp\" + args.Name);
}
catch{}
}
}

Its a bit hit and miss because the process calling XmlSerializer will
delete all the file when it has used them (I suspend the thread in the
code that calls XmlSerializer with another ReadLine so that the
XmlSerializer is not didposed before the watcher process has finished
copying file). On one run I got a .cs file, a zero length dll (obviously
it was deleted before I could completely copy it) and a .cmdline file
which was clearly the switches for the C# compiler

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 20 '05 #7

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

Similar topics

12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
5
by: Arjen | last post by:
Hello, Can somebody help me a little bit? I can't get it to work. Please see my code below... I have placed some comments like "// And whats next?". I'm sure that I have to code something...
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
8
by: Joe | last post by:
I have several classes which I need serialized. Here is their class definitions: public class TopContainer { private LevelTwoType m_levelTwo; public LevelTwoType LevelTwo { get {
3
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
6
by: Beorne | last post by:
I have to use XmlSerializer to serialize a class and I've found big problems serializing properties and indexers. I assumed that if you serialize a class with public properties (or an indexers)...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
0
by: saudamini | last post by:
i am trying to deserlize XML against the class which has this an indexed property, the deserializer just skips this indexed property, it's not setting it. I remember reading somewhere XML...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.