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

Skip to content

System.ObsoleteAttribute F# snippets #7816

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
10 changes: 10 additions & 0 deletions snippets/fsharp/System/ObsoleteAttribute/IsError/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="obsoleteattribute_message.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Example

// <Snippet1>
open System

type Example() =
// Mark OldProperty As Obsolete.
[<ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)>]
member _.OldProperty =
"The old property value."

member _.NewProperty =
"The new property value."

// Mark OldMethod As Obsolete.
[<ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", true)>]
member _.OldMethod() =
"You have called OldMethod."

member _.NewMethod() =
"You have called NewMethod."

// Get all public members of this type.
let members = typeof<Example>.GetMembers()
// Count total obsolete members.
let mutable n = 0

// Try to get the ObsoleteAttribute for each public member.
printfn "Obsolete members in the Example class:\n"
for m in members do
let attribs = m.GetCustomAttributes(typeof<ObsoleteAttribute>, false) |> box :?> ObsoleteAttribute[]
if attribs.Length > 0 then
let attrib = attribs[0]
printfn $"Member Name: {m.DeclaringType.FullName}.{m.Name}"
printfn $" Message: {attrib.Message}"
printfn $""" Warning/Error: {if attrib.IsError then "Error" else "Warning"}"""
n <- n + 1

if n = 0 then
printfn "The Example type has no obsolete attributes."
// The example displays the following output:
// Obsolete members in the Example class:
//
// Member Name: Example+Example.OldMethod
// Message: This method is obsolete. Call NewMethod instead.
// Warning/Error: Error
// Member Name: Example+Example.OldProperty
// Message: This property is obsolete. Use NewProperty instead.
// Warning/Error: Warning
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/ObsoleteAttribute/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="obsoleteattributeex1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(*

// <Snippet1>
open System

// Mark oldValue As Obsolete.
[<ObsoleteAttribute("This value is obsolete. Use newValue instead.", false)>]
let oldValue =
"The old property value."

let newValue =
"The new property value."

// Mark callOldFunction As Obsolete.
[<ObsoleteAttribute("This function is obsolete. Call callNewFunction instead.", true)>]
let callOldFunction () =
"You have called CallOldMethod."

let callNewFunction () =
"You have called CallNewMethod."

printfn $"{oldValue}"
printfn ""
printfn $"{callOldFunction ()}"
// The attempt to compile this example produces output like the following output:
// Example.fs(23,12): error FS0101: This construct is deprecated. This function is obsolete. Call callNewFunction instead.
// Example.fs(21,12): warning FS0044: This construct is deprecated. This value is obsolete. Use newValue instead.
// </Snippet1>
*)
3 changes: 3 additions & 0 deletions xml/System/ObsoleteAttribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ For more information about using attributes, see [Attributes](/dotnet/standard/a
The following example defines a class that contains a property and a method that are marked with the <xref:System.ObsoleteAttribute> attribute. Accessing the value of the `OldProperty` property in code generates a compiler warning, but calling the `CallOldMethod` method generates a compiler error. The example also shows the output that results when you attempt to compile the source code.

:::code language="csharp" source="~/snippets/csharp/System/ObsoleteAttribute/Overview/obsoleteattributeex1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/ObsoleteAttribute/Overview/obsoleteattributeex1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/ObsoleteAttribute/vb/obsoleteattributeex1.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -363,6 +364,7 @@ This property represents the unique ID that can be used to suppress the warnings
The following example defines a class that contains two members marked as obsolete. The first, a property named `OldProperty`, produces a compiler warning if it is called. The second, a method named `CallOldMethod`, produces a compiler error. The example uses reflection to get information about the <xref:System.ObsoleteAttribute> attributes applied to members of the type and displays the values of their <xref:System.ObsoleteAttribute.Message%2A> and <xref:System.ObsoleteAttribute.IsError%2A> properties.

:::code language="csharp" source="~/snippets/csharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.obsoleteattribute.message/vb/obsoleteattribute_message.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -416,6 +418,7 @@ This property represents the unique ID that can be used to suppress the warnings
The following example defines a class that contains two members marked as obsolete. The first, a property named `OldProperty`, produces a compiler warning if it is called. The second, a method named `CallOldMethod`, produces a compiler error. The example uses reflection to get information about the <xref:System.ObsoleteAttribute> attributes applied to members of the type and displays the values of their <xref:System.ObsoleteAttribute.Message%2A> and <xref:System.ObsoleteAttribute.IsError%2A> properties.

:::code language="csharp" source="~/snippets/csharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.obsoleteattribute.message/vb/obsoleteattribute_message.vb" id="Snippet1":::

]]></format>
Expand Down