Framework
Framework
Well its high time that the hash table became XML serializable but i still havent figured out why not.
But while I was thinking about this just thought i'd put it down myself.
I would like to know if there is a more standard implementation for this. But I just managed to this chewing gum class done for now.
using System;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
namespace XMLSerializableHashTable
{
[Serializable]
public class SerializableHashtable:Hashtable,
System.Xml.Serialization.IXmlSerializable
{
#region IXmlSerializable Members
#region Node class
///
/// This class would be for custom serialization
///
[Serializable]
public class Node
{
public Node()
{}
public Node(string k,object v)
{
key = k;
val = v;
}
public string key;
public object val;
}
#endregion Node class for XML Serialization
///
/// Write the xml using an array list
/// using the Node to store key value pairs
///
///
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer xs = new XmlSerializer(typeof(System.Collections.ArrayList),
new System.Type[]{typeof(Node)});
ArrayList list = new ArrayList();
foreach(string key in this.Keys)
{
list.Add(new Node(key,this[key]));
}
xs.Serialize(writer,list);
}
public System.Xml.Schema.XmlSchema GetSchema()
{
// TODO: Add SerializableHashtable.GetSchema implementation
return null;
}
///
/// Deserialization using array list
/// and the node(key,value) pairs
///
///
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer xs = new XmlSerializer(typeof(System.Collections.ArrayList),
new System.Type[]{typeof(Node)});
//Move the reader into the ArrayList element.
reader.Read();
ArrayList list = xs.Deserialize(reader) as ArrayList;
Node node;
if(list == null)
return;
//Reload the hashTable.
for(int i=0;ithis.Add(node.key,node.val);
}
}
#endregion
}
}
class ClassTest
{
public static void S()
{ }
public void Test()
{
this.S();
}
}
This is the compiler error
StaticCall.cs(11,3): error CS0176: Static member 'ClassTest.S()' cannot be accessed with an instance reference; qualify it with a type name instead
Why did the J# spec allow this code to compile and not the C# ?
Should we be able to call static methods using a instance as they pretty much belong to the same type definition or should this work like C# which prevents
static method calls via instance variables because the instance could be null etc and more arguments hence forth.
Just a thought !
Last day a collegue of mine was trying to get the type of a class in J#.
Everyone is would be familiar with the typeof(<MyClass>) in C# right. The question was how to do this in j# as typeof is not supported ?
Loading the assembly was not an option and well we didnt want to make an instance and then get the type from it.
This was what i ended up with .
System.Type t = Class.ToType(System.Windows.Forms.Form.class);
J# java.lang.Class
Was pretty surprised when one of my collegues pointed out that the stackTrace was empty for a StackOverFlowException.
Did a little digging into that and found it was empty. So where do you get it ?
As usual Rotor to the rescue. http://www1.cs.columbia.edu/~lok/csharp/refdocs/System/types/StackOverflowException.html
Then i just tried it out with Environment.StackTrace. Yup it was there, even though it a small stackTrace was a useful though.. Wonder why the StackTrace of the exception didnt have it. If anyone can put somelight on this would be quite helpful.
class Program{
static void Main(string[] args){
try{
Method();
}
catch{
Console.WriteLine(Environment.StackTrace);
}
}
public static void Method()
{
Method(); //Infinite Recursion
}
}