111,094
社区成员




static void Main(string[] args)
{
var value = CreateLiLeiFamily();
MemoryStream s = HproseFormatter.Serialize(value, HproseMode.PropertyMode);
Console.WriteLine(UTF8Encoding.UTF8.GetString(s.ToArray()));
Console.ReadKey();
}
#region 复杂的关系对象
static Male CreateFather(Family family)
{
Male father = new Male();
father.Family = family;
father.Name = "LiTonk";
father.Age = 40;
father.Sex = 'M';
father.Money = 12345654321M;
father.Weight = 75D;
father.Height = 175D;
father.Title = "Father";
return father;
}
static Female CreateMother(Family family)
{
Female mother = new Female();
mother.Family = family;
mother.Name = "HanMeiMei";
mother.Age = 36;
mother.Sex = 'F';
mother.Money = 456541M;
mother.Weight = 50D;
mother.Height = 165D;
mother.Title = "Mother";
return mother;
}
static Male CreateSun(Family family)
{
Male sun = new Male();
sun.Family = family;
sun.Name = "LiLei";
sun.Age = 16;
sun.Sex = 'M';
sun.Money = 121M;
sun.Weight = 54D;
sun.Height = 170D;
sun.Title = "Sun";
return sun;
}
static Family CreateLiLeiFamily()
{
Family family = new Family();
family.FamilyName = "LOVE";
family.Father = CreateFather(family);
family.Mother = CreateMother(family);
family.Sun = CreateSun(family);
return family;
}
#endregion
var value = CreateLiLeiFamily();
HproseFormatter.Serialize(value);
[Serializable]
public abstract class Person
{
public Family Family { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public char Sex { get; set; }
public decimal Money { get; set; }
public double Height { get; set; }
public double Weight { get; set; }
public string Title { get; set; }
public abstract void SayHello();
}
[Serializable]
public class Male : Person
{
public override void SayHello()
{
Console.WriteLine("I am a Male.The " + this.Title + " with " + this.Family.FamilyName + "'s Family");
}
}
[Serializable]
public class Female : Person
{
public override void SayHello()
{
Console.WriteLine("I am a Female.The " + this.Title + " with " + this.Family.FamilyName + "'s Family");
}
}
[Serializable]
public class Family
{
public string FamilyName { get; set; }
public Male Father { get; set; }
public Female Mother { get; set; }
public Male Sun { get; set; }
}