diff --git a/snippets/fsharp/System/ObsoleteAttribute/IsError/fs.fsproj b/snippets/fsharp/System/ObsoleteAttribute/IsError/fs.fsproj new file mode 100644 index 00000000000..5501accc55c --- /dev/null +++ b/snippets/fsharp/System/ObsoleteAttribute/IsError/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.fs b/snippets/fsharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.fs new file mode 100644 index 00000000000..28dd0ca826b --- /dev/null +++ b/snippets/fsharp/System/ObsoleteAttribute/IsError/obsoleteattribute_message.fs @@ -0,0 +1,50 @@ +module Example + +// +open System + +type Example() = + // Mark OldProperty As Obsolete. + [] + member _.OldProperty = + "The old property value." + + member _.NewProperty = + "The new property value." + + // Mark OldMethod As Obsolete. + [] + member _.OldMethod() = + "You have called OldMethod." + + member _.NewMethod() = + "You have called NewMethod." + +// Get all public members of this type. +let members = typeof.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, 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/ObsoleteAttribute/Overview/fs.fsproj b/snippets/fsharp/System/ObsoleteAttribute/Overview/fs.fsproj new file mode 100644 index 00000000000..8714b36f844 --- /dev/null +++ b/snippets/fsharp/System/ObsoleteAttribute/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/ObsoleteAttribute/Overview/obsoleteattributeex1.fs b/snippets/fsharp/System/ObsoleteAttribute/Overview/obsoleteattributeex1.fs new file mode 100644 index 00000000000..eb90c4ecc1d --- /dev/null +++ b/snippets/fsharp/System/ObsoleteAttribute/Overview/obsoleteattributeex1.fs @@ -0,0 +1,29 @@ +(* + +// +open System + +// Mark oldValue As Obsolete. +[] +let oldValue = + "The old property value." + +let newValue = + "The new property value." + +// Mark callOldFunction As Obsolete. +[] +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. +// +*) diff --git a/xml/System/ObsoleteAttribute.xml b/xml/System/ObsoleteAttribute.xml index f5b1d073929..556f3173a70 100644 --- a/xml/System/ObsoleteAttribute.xml +++ b/xml/System/ObsoleteAttribute.xml @@ -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 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"::: ]]> @@ -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 attributes applied to members of the type and displays the values of their and 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"::: ]]> @@ -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 attributes applied to members of the type and displays the values of their and 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"::: ]]>