Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.NonSerializedAttribute F# snippet #7796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="s.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Runtime.Serialization.Formatters.Soap" />
</ItemGroup>
</Project>
52 changes: 52 additions & 0 deletions snippets/fsharp/System/NonSerializedAttribute/Overview/s.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <Snippet1>
open System
open System.IO
open System.Runtime.Serialization.Formatters.Soap

// A test object that needs to be serialized.
[<Serializable>]
type TestSimpleObject() =
let member1 = 11
let member2 = "hello"
let member3 = "hello"
let member4 = 3.14159265

// A field that is not serialized.
[<NonSerialized>]
let member5 = "hello world!"

member _.Print() =
printfn $"member1 = '{member1}'"
printfn $"member2 = '{member2}'"
printfn $"member3 = '{member3}'"
printfn $"member4 = '{member4}'"
printfn $"member5 = '{member5}'"

[<EntryPoint>]
let main _ =
// Creates a new TestSimpleObject object.
let obj = TestSimpleObject()

printfn "Before serialization the object contains: "
obj.Print()

// Opens a file and serializes the object into it in binary format.
let stream = File.Open("data.xml", FileMode.Create)
let formatter = SoapFormatter()

//BinaryFormatter formatter = new BinaryFormatter()

formatter.Serialize(stream, obj)
stream.Close()

// Opens file "data.xml" and deserializes the object from it.
let stream = File.Open("data.xml", FileMode.Open)
let formatter = new SoapFormatter()

let obj = formatter.Deserialize stream :?> TestSimpleObject
stream.Close()

printfn "\nAfter deserialization the object contains: "
obj.Print()
0
// </Snippet1>
1 change: 1 addition & 0 deletions xml/System/NonSerializedAttribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public event ChangedEventHandler Changed;

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_Remoting/SerializationAttributes/CPP/s.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System/NonSerializedAttribute/Overview/s.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/NonSerializedAttribute/Overview/s.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Remoting/SerializationAttributes/VB/s.vb" id="Snippet1":::

]]></format>
Expand Down