The document demonstrates how to mark members as obsolete using the ObsoleteAttribute in C#. It defines an Example class with an OldProperty and OldMethod marked as obsolete, as well as corresponding NewProperty and NewMethod. The Main method uses reflection to find any obsolete members in the Example class and print their details, including the obsolete message and whether they generate a warning or error.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
91 views1 page
.Metrotextbox1.Prompttext : "Enter Your Username"
The document demonstrates how to mark members as obsolete using the ObsoleteAttribute in C#. It defines an Example class with an OldProperty and OldMethod marked as obsolete, as well as corresponding NewProperty and NewMethod. The Main method uses reflection to find any obsolete members in the Example class and print their details, including the obsolete message and whether they generate a warning or error.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
this.metroTextBox1.CustomButton.
Image = null; using System;
this.metroTextBox1.CustomButton.Location = new using System.Reflection; System.Drawing.Point(106, 1); public class Example this.metroTextBox1.CustomButton.Name = ""; { this.metroTextBox1.CustomButton.Size = new // Mark OldProperty As Obsolete. System.Drawing.Size(21, 21); [ObsoleteAttribute("This property is obsolete. Use this.metroTextBox1.CustomButton.Style = MetroFramework.MetroColorStyle.Blue; NewProperty instead.", false)] this.metroTextBox1.CustomButton.TabIndex = 1; public string OldProperty this.metroTextBox1.CustomButton.Theme = { get { return "The old property value."; } } MetroFramework.MetroThemeStyle.Light; public string NewProperty this.metroTextBox1.CustomButton.UseSelectable = true; { get { return "The new property value."; } } this.metroTextBox1.CustomButton.Visible = false; // Mark OldMethod As Obsolete. this.metroTextBox1.Lines = new string[0]; [ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", this.metroTextBox1.Location = new true)] System.Drawing.Point(330, 153); public string OldMethod() this.metroTextBox1.MaxLength = 32767; { this.metroTextBox1.Name = "metroTextBox1"; return "You have called OldMethod."; this.metroTextBox1.PasswordChar = '\0'; } this.metroTextBox1.PromptText = public string NewMethod() "Enter your username"; { this.metroTextBox1.ScrollBars = return "You have called NewMethod."; System.Windows.Forms.ScrollBars.None; } this.metroTextBox1.SelectedText = ""; public static void Main() this.metroTextBox1.SelectionLength = 0; { this.metroTextBox1.SelectionStart = 0; // Get all public members of this type. this.metroTextBox1.ShortcutsEnabled = true; MemberInfo[] members = typeof(Example).GetMembers(); this.metroTextBox1.Size = new System.Drawing.Size(128, // Count total obsolete members. 23); int n = 0; this.metroTextBox1.TabIndex = 1; // Try to get the ObsoleteAttribute for each public member. this.metroTextBox1.UseSelectable = true; Console.WriteLine("Obsolete members in the Example class:\n"); this.metroTextBox1.WaterMark = "Enter your username"; foreach (var member in members) { this.metroTextBox1.WaterMarkColor = ObsoleteAttribute[] attribs = (ObsoleteAttribute[]) System.Drawing.Color.FromArgb(((int)(((byte)(109)))), ((int) member.GetCustomAttributes(typeof(ObsoleteAttribute), (((byte)(109)))), ((int)(((byte)(109))))); false); this.metroTextBox1.WaterMarkFont = new if (attribs.Length > 0) { System.Drawing.Font("Segoe UI", 12F, ObsoleteAttribute attrib = attribs[0]; System.Drawing.FontStyle.Italic, Console.WriteLine("Member Name: {0}.{1}", System.Drawing.GraphicsUnit.Pixel); member.DeclaringType.FullName, member.Name); this.metroTextBox1.Click += new Console.WriteLine(" Message: {0}", attrib.Message); System.EventHandler(this.metroTextBox1_Click); Console.WriteLine(" Warning/Error: {0}", attrib.IsError ? "Error" : "Warning"); n++; } } if (n == 0) Console.WriteLine("The Example type has no obsolete attributes."); } } // The example displays the following output: // Obsolete members in the Example class: // Member Name: Example.OldMethod // Message: This method is obsolete. Call NewMethod instead. // Warning/Error: Error // Member Name: Example.OldProperty // Message: This property is obsolete. Use NewProperty instead. // Warning/Error: Warning