Guest User

Untitled

a guest
Jan 18th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. [XmlRpcUrl("https://blabala.com/rpc")] // описание методов
  2. public interface Trac : IXmlRpcProxy
  3. {
  4. [XmlRpcMethod("ticket.get")] //
  5. object[] get(int id);
  6.  
  7. [XmlRpcMethod("ticket.getActions")] //
  8. object[] getActions(int id);
  9. }
  10.  
  11. public partial class DHL : Form
  12. {
  13. public DHL()
  14. {
  15. InitializeComponent();
  16. }
  17.  
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. int id = 6;
  21. get(id);
  22. }
  23.  
  24. private void get(int id)
  25. {
  26. Trac proxy;
  27. string user = "uzer";
  28. string password = "parol";
  29. proxy = XmlRpcProxyGen.Create<Trac>();
  30. proxy.Credentials = new System.Net.NetworkCredential(user, password);
  31. object[] arr = proxy.get(id);
  32. BinaryFormatter formatter = new BinaryFormatter();
  33. using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
  34. {
  35. formatter.Serialize(fs, arr);// тут и ошибка, что Тип CookComputing.XmlRpc.XmlRpcStruct в сборке CookComputing.XmlRpcV2, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a7d6e17aa302004d не отмечен как сериализуемый.
  36. }
  37. }
  38.  
  39. private void button4_Click(object sender, EventArgs e)
  40. {
  41. int id = 6;
  42. getActions( id);
  43. }
  44.  
  45. private void getActions(int id)
  46. {
  47. Trac proxy;
  48. string user = "uzer";
  49. string password = "parol";
  50. proxy = XmlRpcProxyGen.Create<Trac>();
  51. proxy.Credentials = new System.Net.NetworkCredential(user, password);
  52. object[] arr = proxy.getActions(id);
  53. BinaryFormatter formatter = new BinaryFormatter();
  54. using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
  55. {
  56. formatter.Serialize(fs, arr);//а тут нет такой ошибки
  57. }
  58. }
  59. }
  60.  
  61. public class Person
  62. {
  63. public string Name { get; set; }
  64. public int Age { get; set; }
  65. public DriversLicense License;
  66. }
  67.  
  68.  
  69. // An instance of this type will be part of the object graph and will need to be
  70. // serialized also.
  71. public class DriversLicense
  72. {
  73. public string Number { get; set; }
  74. }
  75.  
  76. public class PersonSurrogate : ISerializationSurrogate
  77. {
  78. /// <summary>
  79. /// Manually add objects to the <see cref="SerializationInfo"/> store.
  80. /// </summary>
  81. public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
  82. {
  83. Person person = (Person) obj;
  84. info.AddValue("Name", person.Name);
  85. info.AddValue("Age", person.Age);
  86. info.AddValue("License", person.License);
  87. }
  88.  
  89. /// <summary>
  90. /// Retrieves objects from the <see cref="SerializationInfo"/> store.
  91. /// </summary>
  92. /// <returns></returns>
  93. public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  94. {
  95. Person person = (Person)obj;
  96. person.Name = info.GetString("Name");
  97. person.Age = info.GetInt32("Age");
  98. person.License = (DriversLicense) info.GetValue("License", typeof(DriversLicense));
  99. return person;
  100. }
  101. }
  102.  
  103. public class DriversLicenseSurrogate : ISerializationSurrogate
  104. {
  105. /// <summary>
  106. /// Manually add objects to the <see cref="SerializationInfo"/> store.
  107. /// </summary>
  108. public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
  109. {
  110. DriversLicense license = (DriversLicense)obj;
  111. info.AddValue("Number", license.Number);
  112. }
  113.  
  114. /// <summary>
  115. /// Retrieves objects from the <see cref="SerializationInfo"/> store.
  116. /// </summary>
  117. /// <returns></returns>
  118. public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  119. {
  120. DriversLicense license = (DriversLicense)obj;
  121. license.Number = info.GetString("Number");
  122. return license;
  123. }
  124. }
  125.  
  126. private static void SerializePerson(Person person)
  127. {
  128. if (person == null)
  129. throw new ArgumentNullException("person");
  130.  
  131. using (var memoryStream = new MemoryStream())
  132. {
  133. //Configure our surrogate selectors.
  134. var surrogateSelector = new SurrogateSelector();
  135. surrogateSelector.AddSurrogate(typeof (Person), new StreamingContext(StreamingContextStates.All),
  136. new PersonSurrogate());
  137. surrogateSelector.AddSurrogate(typeof (DriversLicense), new StreamingContext(StreamingContextStates.All),
  138. new DriversLicenseSurrogate());
  139.  
  140. //Serialize the object
  141. IFormatter formatter = new BinaryFormatter();
  142. formatter.SurrogateSelector = surrogateSelector;
  143. formatter.Serialize(memoryStream, person);
  144.  
  145. //Return to the beginning of the stream
  146. memoryStream.Seek(0, SeekOrigin.Begin);
  147.  
  148. //Deserialize the object
  149. Person deserializedPerson = (Person) formatter.Deserialize(memoryStream);
  150. }
  151. }
Add Comment
Please, Sign In to add comment