本文整理汇总了C#中ObjectWriter.WriteObject方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectWriter.WriteObject方法的具体用法?C# ObjectWriter.WriteObject怎么用?C# ObjectWriter.WriteObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectWriter
的用法示例。
在下文中一共展示了ObjectWriter.WriteObject方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldHandleTwoWritesAndReads
public void ShouldHandleTwoWritesAndReads()
{
var strings = new [] { "One", "Two" };
var stream = new MemoryStream();
var writer = new ObjectWriter(stream);
writer.WriteObject(strings[0]);
writer.WriteObject(strings[1]);
var position = stream.Position;
stream.Seek(0, SeekOrigin.Begin);
var reader = new ObjectReader(stream);
Assert.AreEqual(strings[0], reader.ReadObject<string>());
Assert.AreEqual(strings[1], reader.ReadObject<string>());
Assert.AreEqual(position, stream.Position);
}
示例2: Main
static void Main(string[] args)
{
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Loopback, 4040);
Socket ss = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var productos = new ObservableCollection<Producto>();
for (int i = 0; i < 20; i++)
{
productos.Add(new Producto()
{
ProductId = i,
Nombre = "Producto servidor" + i,
Precio = (decimal)i,
CantidadDisponible = i * 10,
Descripcion = "The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. Usually it is best to"
});
}
try
{
ss.Bind(localEndPoint);
ss.Listen(10);
while (true)
{
Console.WriteLine("Servidor escuchando por conexiones");
Socket cliente = ss.Accept();
string descCliente = cliente.LocalEndPoint.ToString();
Console.WriteLine("Conexion aceptada " + descCliente);
ObjectWriter w = new ObjectWriter(cliente);
ObjectReader r = new ObjectReader(cliente);
Transacciones transaccion = (Transacciones)r.ReadInt32();
switch (transaccion)
{
case Transacciones.SolicitarCarrito:
Console.WriteLine("\tSolicitud de carrito por: " + descCliente);
w.WriteInt32(productos.Count);
for (int i = 0; i < productos.Count; i++)
{
w.WriteObject<Producto>(productos[i]);
}
break;
case Transacciones.RealizarCompra:
Console.WriteLine("\tOrden de compra de " + descCliente);
Orden o = r.ReadObject<Orden>();
productos[o.ProductId].CantidadDisponible -= o.Cantidad;
break;
}
Console.WriteLine("Conexion terminada " + descCliente);
cliente.Shutdown(SocketShutdown.Both);
cliente.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
示例3: ShouldHandleTwoWritesAndReads
public void ShouldHandleTwoWritesAndReads()
{
var strings = new [] { "One", "Two" };
var stream = new MemoryStream();
var writer = new ObjectWriter(stream, Serializer.GetReflectionBasedWriteMethods());
writer.WriteObject(strings[0]);
writer.WriteObject(strings[1]);
writer.Flush();
var position = stream.Position;
stream.Seek(0, SeekOrigin.Begin);
var reader = new ObjectReader(stream, Serializer.GetReflectionBasedReadMethods(false));
Assert.AreEqual(strings[0], reader.ReadObject<string>());
Assert.AreEqual(strings[1], reader.ReadObject<string>());
reader.Flush();
Assert.AreEqual(position, stream.Position);
}
示例4: ShouldHandleTwoWritesAndReads
public void ShouldHandleTwoWritesAndReads()
{
var strings = new [] { "One", "Two" };
var typeIndices = new List<Type>
{
typeof(string)
};
var stream = new MemoryStream();
var writer = new ObjectWriter(stream, typeIndices);
writer.WriteObject(strings[0]);
writer.WriteObject(strings[1]);
var position = stream.Position;
stream.Seek(0, SeekOrigin.Begin);
var types = typeIndices.ToArray();
var reader = new ObjectReader(stream, types, false);
Assert.AreEqual(strings[0], reader.ReadObject<string>());
Assert.AreEqual(strings[1], reader.ReadObject<string>());
Assert.AreEqual(position, stream.Position);
}
示例5: RealizaCompra
public int RealizaCompra(int productId, int cantidad)
{
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Loopback, 4040);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(remotePoint);
ObjectReader r = new ObjectReader(s);
ObjectWriter w = new ObjectWriter(s);
w.WriteInt32((int)Transacciones.RealizarCompra);
Orden o = new Orden { ProductId = productId, Cantidad = cantidad };
w.WriteObject<Orden>(o);
s.Shutdown(SocketShutdown.Both);
s.Close();
return 1;
}
示例6: WriteChild
protected virtual XmlElement WriteChild (ObjectWriter writer, Widget wrapper)
{
XmlElement child_elem = writer.XmlDocument.CreateElement ("child");
XmlElement widget_elem = writer.WriteObject (wrapper);
child_elem.AppendChild (widget_elem);
Container.ContainerChild childwrapper = ChildWrapper (wrapper);
if (childwrapper != null) {
XmlElement packing_elem;
if (writer.Format == FileFormat.Glade)
packing_elem = GladeUtils.CreatePacking (writer.XmlDocument, childwrapper);
else
packing_elem = WidgetUtils.CreatePacking (writer.XmlDocument, childwrapper);
// Sets the child Id to be used in undo/redo operations
if (writer.CreateUndoInfo)
child_elem.SetAttribute ("undoId", childwrapper.UndoId);
if (packing_elem.HasChildNodes)
child_elem.AppendChild (packing_elem);
} else {
// There is no container child, so make up an id.
if (writer.CreateUndoInfo)
child_elem.SetAttribute ("undoId", "0");
}
return child_elem;
}
示例7: Write
public override XmlElement Write (ObjectWriter writer)
{
XmlElement elem = WriteProperties (writer);
WriteActionGroups (writer, elem);
XmlElement child_elem;
if (ClassDescriptor.AllowChildren) {
foreach (Gtk.Widget child in GladeChildren) {
Widget wrapper = Widget.Lookup (child);
if (wrapper != null) {
// Iternal children are written later
if (wrapper.InternalChildProperty != null)
continue;
child_elem = WriteChild (writer, wrapper);
if (child_elem != null)
elem.AppendChild (child_elem);
} else if (child is Stetic.Placeholder) {
child_elem = writer.XmlDocument.CreateElement ("child");
if (writer.CreateUndoInfo)
child_elem.SetAttribute ("undoId", ((Stetic.Placeholder)child).UndoId);
child_elem.AppendChild (writer.XmlDocument.CreateElement ("placeholder"));
elem.AppendChild (child_elem);
}
}
}
foreach (PropertyDescriptor prop in this.ClassDescriptor.InternalChildren) {
Gtk.Widget child = prop.GetValue (Wrapped) as Gtk.Widget;
if (child == null)
continue;
child_elem = writer.XmlDocument.CreateElement ("child");
Widget wrapper = Widget.Lookup (child);
if (wrapper == null) {
child_elem.AppendChild (writer.XmlDocument.CreateElement ("placeholder"));
elem.AppendChild (child_elem);
continue;
}
string cid = writer.Format == FileFormat.Glade ? prop.InternalChildId : prop.Name;
XmlElement widget_elem = writer.WriteObject (wrapper);
child_elem.SetAttribute ("internal-child", cid);
// Sets the child Id to be used in undo/redo operations
if (writer.CreateUndoInfo)
child_elem.SetAttribute ("undoId", cid);
child_elem.AppendChild (widget_elem);
elem.AppendChild (child_elem);
}
if (DesignWidth != 0 || DesignHeight != 0)
elem.SetAttribute ("design-size", DesignWidth + " " + DesignHeight);
return elem;
}
示例8: Write
public override XmlElement Write (ObjectWriter writer)
{
XmlElement group = writer.XmlDocument.CreateElement ("action-group");
group.SetAttribute ("name", name);
if (writer.CreateUndoInfo)
group.SetAttribute ("undoId", UndoId);
foreach (Action ac in actions) {
if (ac.Name.Length > 0)
group.AppendChild (writer.WriteObject (ac));
}
return group;
}
示例9: GenSha1
private ObjectId GenSha1(string data)
{
byte[] buffer = Constants.encode(data);
var input = new MemoryStream(buffer);
var writer = new ObjectWriter(db);
try
{
return writer.WriteObject(ObjectType.Blob, buffer.Length, input, true);
}
catch (IOException exception)
{
Assert.Fail(exception.ToString());
}
return null;
}