Skip to content

Commit 4fc8eb5

Browse files
authored
Delete C# BinaryFormatter snippets (dotnet#9101)
1 parent fb66aaf commit 4fc8eb5

File tree

81 files changed

+535
-3801
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+535
-3801
lines changed

snippets/csharp/System.ComponentModel/ComponentEditor/Overview/componenteditorexamplecomponent.cs

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
using System;
33
using System.ComponentModel;
44
using System.ComponentModel.Design;
5-
using System.Collections;
65
using System.Drawing;
7-
using System.IO;
8-
using System.Runtime.Serialization;
9-
using System.Runtime.Serialization.Formatters.Binary;
106
using System.Windows.Forms;
11-
using System.Windows.Forms.Design;
127

138
// This example demonstrates how to implement a component editor that hosts
149
// component pages and associate it with a component. This example also
@@ -23,65 +18,62 @@ public class ExampleComponentEditor : System.Windows.Forms.Design.WindowsFormsCo
2318
// This method override returns an type array containing the type of
2419
// each component editor page to display.
2520
protected override Type[] GetComponentEditorPages()
26-
{
27-
return new Type[] { typeof(ExampleComponentEditorPage),
28-
typeof(ExampleComponentEditorPage) };
21+
{
22+
return new Type[] { typeof(ExampleComponentEditorPage),
23+
typeof(ExampleComponentEditorPage) };
2924
}
3025
//</Snippet2>
31-
26+
3227
//<Snippet3>
3328
// This method override returns the index of the page to display when the
3429
// component editor is first displayed.
3530
protected override int GetInitialComponentEditorPageIndex()
36-
{
37-
return 1;
31+
{
32+
return 1;
3833
}
3934
//</Snippet3>
4035
}
41-
36+
4237
//<Snippet6>
4338
// This example component editor page type provides an example
4439
// ComponentEditorPage implementation.
4540
internal class ExampleComponentEditorPage : System.Windows.Forms.Design.ComponentEditorPage
4641
{
47-
Label l1;
48-
Button b1;
42+
Label l1;
43+
Button b1;
4944
PropertyGrid pg1;
5045

51-
// Base64-encoded serialized image data for the required component editor page icon.
52-
string icon = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuNTAwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABNTeXN0ZW0uRHJhd2luZy5JY29uAgAAAAhJY29uRGF0YQhJY29uU2l6ZQcEAhNTeXN0ZW0uRHJhd2luZy5TaXplAgAAAAIAAAAJAwAAAAX8////E1N5c3RlbS5EcmF3aW5nLlNpemUCAAAABXdpZHRoBmhlaWdodAAACAgCAAAAAAAAAAAAAAAPAwAAAD4BAAACAAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAABAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgADExAAAgICAAMDAwAA+iPcAY77gACh9kwD/AAAAndPoADpw6wD///8AAAAAAAAAAAAHd3d3d3d3d8IiIiIiIiLHKIiIiIiIiCco///////4Jyj5mfIvIvgnKPnp////+Cco+en7u7v4Jyj56f////gnKPmZ8i8i+Cco///////4JyiIiIiIiIgnJmZmZmZmZifCIiIiIiIiwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACw==";
53-
5446
public ExampleComponentEditorPage()
5547
{
5648
// Initialize the page, which inherits from Panel, and its controls.
57-
this.Size = new Size( 400, 250 );
58-
this.Icon = DeserializeIconFromBase64Text(icon);
49+
this.Size = new Size(400, 250);
50+
this.Icon = new Icon("myicon.ico");
5951
this.Text = "Example Page";
60-
52+
6153
b1 = new Button();
6254
b1.Size = new Size(200, 20);
6355
b1.Location = new Point(200, 0);
6456
b1.Text = "Set a random background color";
6557
b1.Click += new EventHandler(this.randomBackColor);
66-
this.Controls.Add( b1 );
58+
this.Controls.Add(b1);
6759

6860
l1 = new Label();
6961
l1.Size = new Size(190, 20);
7062
l1.Location = new Point(4, 2);
7163
l1.Text = "Example Component Editor Page";
72-
this.Controls.Add( l1 );
64+
this.Controls.Add(l1);
7365

7466
pg1 = new PropertyGrid();
7567
pg1.Size = new Size(400, 280);
76-
pg1.Location = new Point(0,30);
77-
this.Controls.Add( pg1 );
68+
pg1.Location = new Point(0, 30);
69+
this.Controls.Add(pg1);
7870
}
79-
71+
8072
// This method indicates that the Help button should be enabled for this
8173
// component editor page.
8274
public override bool SupportsHelp()
83-
{
84-
return true;
75+
{
76+
return true;
8577
}
8678

8779
//<Snippet4>
@@ -95,9 +87,9 @@ public override void ShowHelp()
9587

9688
// Retrieve the Site of the component, and return if null.
9789
ISite componentSite = selectedComponent.Site;
98-
if(componentSite == null)
90+
if (componentSite == null)
9991
return;
100-
92+
10193
// Acquire the IHelpService to display a help topic using a indexed keyword lookup.
10294
IHelpService helpService = (IHelpService)componentSite.GetService(typeof(IHelpService));
10395
if (helpService != null)
@@ -107,10 +99,10 @@ public override void ShowHelp()
10799

108100
// The LoadComponent method is raised when the ComponentEditorPage is displayed.
109101
protected override void LoadComponent()
110-
{
111-
this.pg1.SelectedObject = this.Component;
102+
{
103+
this.pg1.SelectedObject = this.Component;
112104
}
113-
105+
114106
// The SaveComponent method is raised when the WindowsFormsComponentEditor is closing
115107
// or the current ComponentEditorPage is closing.
116108
protected override void SaveComponent()
@@ -121,32 +113,19 @@ protected override void SaveComponent()
121113
// This method is invoked by the button on this ComponentEditorPage.
122114
private void randomBackColor(object sender, EventArgs e)
123115
{
124-
if( typeof(System.Windows.Forms.Control).IsAssignableFrom( this.Component.GetType() ) )
116+
if (typeof(System.Windows.Forms.Control).IsAssignableFrom(this.Component.GetType()))
125117
{
126118
// Sets the background color of the Control associated with the
127119
// WindowsFormsComponentEditor to a random color.
128120
Random rnd = new Random();
129-
((System.Windows.Forms.Control)this.Component).BackColor =
121+
((System.Windows.Forms.Control)this.Component).BackColor =
130122
Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
131123
pg1.Refresh();
132124
}
133125
}
134-
135-
// This method can be used to retrieve an Icon from a block
136-
// of Base64-encoded text.
137-
private Icon DeserializeIconFromBase64Text(string text)
138-
{
139-
Icon img = null;
140-
byte[] memBytes = Convert.FromBase64String(text);
141-
IFormatter formatter = new BinaryFormatter();
142-
MemoryStream stream = new MemoryStream(memBytes);
143-
img = (Icon)formatter.Deserialize(stream);
144-
stream.Close();
145-
return img;
146-
}
147126
}
148127
//</Snippet6>
149-
128+
150129
//<Snippet5>
151130
// This example control is associated with the ExampleComponentEditor
152131
// through the following EditorAttribute.
@@ -156,4 +135,4 @@ public class ExampleUserControl : System.Windows.Forms.UserControl
156135
}
157136
//</Snippet5>
158137
}
159-
//</Snippet1>
138+
//</Snippet1>

snippets/csharp/System.ComponentModel/PropertyTabAttribute/Overview/class1.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
//<Snippet1>
2-
using System;
32
using System.ComponentModel;
4-
using System.ComponentModel.Design;
53
using System.Drawing;
6-
using System.IO;
7-
using System.Reflection;
8-
using System.Runtime.Serialization;
9-
using System.Runtime.Serialization.Formatters.Binary;
10-
using System.Windows.Forms;
114
using System.Windows.Forms.Design;
125

136
namespace TypeCategoryTabExample
14-
{
7+
{
158
// This component adds a TypeCategoryTab to the property browser
169
// that is available for any components in the current design mode document.
1710
[PropertyTabAttribute(typeof(TypeCategoryTab), PropertyTabScope.Document)]
@@ -26,10 +19,6 @@ public TypeCategoryTabComponent()
2619
// category of the type of each property.
2720
public class TypeCategoryTab : PropertyTab
2821
{
29-
[BrowsableAttribute(true)]
30-
// This string contains a Base-64 encoded and serialized example property tab image.
31-
private string img = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuMzMwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABVTeXN0ZW0uRHJhd2luZy5CaXRtYXABAAAABERhdGEHAgIAAAAJAwAAAA8DAAAA9gAAAAJCTfYAAAAAAAAANgAAACgAAAAIAAAACAAAAAEAGAAAAAAAAAAAAMQOAADEDgAAAAAAAAAAAAD///////////////////////////////////9ZgABZgADzPz/zPz/zPz9AgP//////////gAD/gAD/AAD/AAD/AACKyub///////+AAACAAAAAAP8AAP8AAP9AgP////////9ZgABZgABz13hz13hz13hAgP//////////gAD/gACA/wCA/wCA/wAA//////////+AAACAAAAAAP8AAP8AAP9AgP////////////////////////////////////8L";
32-
3322
public TypeCategoryTab()
3423
{
3524
}
@@ -75,22 +64,10 @@ public override System.Drawing.Bitmap Bitmap
7564
{
7665
get
7766
{
78-
Bitmap bmp = new Bitmap(DeserializeFromBase64Text(img));
67+
Bitmap bmp = new Bitmap(@"myproperty.bmp", true);
7968
return bmp;
8069
}
8170
}
82-
83-
// This method can be used to retrieve an Image from a block of Base64-encoded text.
84-
private Image DeserializeFromBase64Text(string text)
85-
{
86-
Image img = null;
87-
byte[] memBytes = Convert.FromBase64String(text);
88-
IFormatter formatter = new BinaryFormatter();
89-
MemoryStream stream = new MemoryStream(memBytes);
90-
img = (Image)formatter.Deserialize(stream);
91-
stream.Close();
92-
return img;
93-
}
9471
}
9572
}
9673
//</Snippet1>

snippets/csharp/System.Data/EntityKey/Overview/Program.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
using System;
2-
using System.Linq;
3-
using System.Collections.Generic;
4-
using System.Text;
5-
using System.Data;
6-
using System.Data.Common;
7-
using System.Data.Objects;
8-
using System.Data.Objects.DataClasses;
9-
using System.Runtime.Serialization;
10-
using System.Runtime.Serialization.Formatters.Binary;
11-
using System.IO;
12-
using System.Xml.Serialization;
13-
using System.Data.Common.CommandTrees;
14-
using System.Data.Metadata.Edm;
15-
using System.Data.EntityClient;
16-
using System.ComponentModel;
17-
using System.Data.SqlClient;
18-
19-
namespace ObjectServicesConceptsCS
1+
namespace ObjectServicesConceptsCS
202
{
213
class Program
224
{

snippets/csharp/System.Data/EntityKey/Overview/Source.cs

Lines changed: 7 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
//<snippetUsingSerialization>
22
//<snippetUsing>
33
using System;
4-
using System.Linq;
54
using System.Collections.Generic;
6-
using System.Text;
5+
//<snippetUsingEvents>
6+
using System.ComponentModel;
77
using System.Data;
88
using System.Data.Common;
9+
using System.Data.EntityClient;
10+
using System.Data.Metadata.Edm;
911
using System.Data.Objects;
1012
using System.Data.Objects.DataClasses;
13+
//</snippetUsingEvents>
14+
using System.Data.SqlClient;
1115
//</snippetUsing>
12-
using System.Runtime.Serialization;
13-
using System.Runtime.Serialization.Formatters.Binary;
1416
using System.IO;
17+
using System.Linq;
1518
//</snippetUsingSerialization>
1619
using System.Xml.Serialization;
17-
using System.Data.Common.CommandTrees;
18-
using System.Data.Metadata.Edm;
19-
using System.Data.EntityClient;
20-
//<snippetUsingEvents>
21-
using System.ComponentModel;
22-
//</snippetUsingEvents>
23-
using System.Data.SqlClient;
2420

2521
namespace ObjectServicesConceptsCS
2622
{
@@ -1581,102 +1577,6 @@ public static void SerializeToXml()
15811577
Console.WriteLine(writer.ToString());
15821578
}
15831579
}
1584-
#region StreamToBinary
1585-
//<snippetStreamToBinary>
1586-
public static void ReadFromBinaryStream()
1587-
{
1588-
BinaryFormatter formatter = new BinaryFormatter();
1589-
using (AdventureWorksEntities context = new AdventureWorksEntities())
1590-
{
1591-
try
1592-
{
1593-
// Get the object graph for the selected customer
1594-
// as a binary stream.
1595-
MemoryStream stream = SerializeToBinaryStream(@"Adams");
1596-
1597-
// Read from the begining of the stream.
1598-
stream.Seek(0, SeekOrigin.Begin);
1599-
1600-
// Deserialize the customer graph from the binary stream
1601-
// and attach to an ObjectContext.
1602-
Contact contact = (Contact)formatter.Deserialize(stream);
1603-
context.Attach(contact);
1604-
1605-
// Display information for each item
1606-
// in the orders that belong to the first contact.
1607-
foreach (SalesOrderHeader order in contact.SalesOrderHeaders)
1608-
{
1609-
Console.WriteLine(String.Format("PO Number: {0}",
1610-
order.PurchaseOrderNumber));
1611-
Console.WriteLine(String.Format("Order Date: {0}",
1612-
order.OrderDate.ToString()));
1613-
Console.WriteLine("Order items:");
1614-
foreach (SalesOrderDetail item in order.SalesOrderDetails)
1615-
{
1616-
Console.WriteLine(String.Format("Product: {0} "
1617-
+ "Quantity: {1}", item.ProductID.ToString(),
1618-
item.OrderQty.ToString()));
1619-
}
1620-
}
1621-
}
1622-
1623-
catch (SerializationException ex)
1624-
{
1625-
Console.WriteLine("The object graph could not be deserialized from "
1626-
+ "the binary stream because of the following error:");
1627-
Console.WriteLine(ex.ToString());
1628-
}
1629-
}
1630-
}
1631-
private static MemoryStream SerializeToBinaryStream(string lastName)
1632-
{
1633-
BinaryFormatter formatter = new BinaryFormatter();
1634-
MemoryStream stream = new MemoryStream();
1635-
1636-
using (AdventureWorksEntities context = new AdventureWorksEntities())
1637-
{
1638-
//<snippetQueryTimeout>
1639-
// Specify a timeout for queries in this context, in seconds.
1640-
context.CommandTimeout = 120;
1641-
//</snippetQueryTimeout>
1642-
1643-
// Define a customer contact.
1644-
Contact customer;
1645-
1646-
// Create a Contact query with a path that returns
1647-
// orders and items for a contact.
1648-
ObjectQuery<Contact> query =
1649-
context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails");
1650-
1651-
try
1652-
{
1653-
// Return the first contact with the specified last name
1654-
// along with its related orders and items.
1655-
customer = query.Where("it.LastName = @lastname",
1656-
new ObjectParameter("lastname", lastName)).First();
1657-
1658-
// Serialize the customer object graph.
1659-
formatter.Serialize(stream, customer);
1660-
}
1661-
catch (EntitySqlException ex)
1662-
{
1663-
throw new InvalidOperationException("The object query failed", ex);
1664-
}
1665-
catch (EntityCommandExecutionException ex)
1666-
{
1667-
throw new InvalidOperationException("The object query failed", ex);
1668-
}
1669-
catch (SerializationException ex)
1670-
{
1671-
throw new InvalidOperationException("The object graph could not be serialized", ex);
1672-
}
1673-
1674-
// Return the streamed object graph.
1675-
return stream;
1676-
}
1677-
}
1678-
//</snippetStreamToBinary>
1679-
#endregion
16801580

16811581
public static Boolean CleanupOrders()
16821582
{

0 commit comments

Comments
 (0)