From a9fb8ac1c3d4d1947bc856fd616dc91c48437703 Mon Sep 17 00:00:00 2001 From: Willy David Jr Date: Wed, 21 Aug 2019 00:32:05 +0800 Subject: [PATCH] Corrected access on constant variable and access on attribute properties. (#36586) First on constant variable issue: This will not compile: [Plugin(MyPluginName)] // Won't compile because MyPluginName isn't const [Plugin(MyConstPluginName)] // OK [Plugin("My Cool Plugin")] // OK In order to access properties on the static class, it must specify the name of the class before the properties name as proposed on this change: [Plugin(Variables.MyPluginName)] // Won't compile because MyPluginName isn't const [Plugin(Variables.MyConstPluginName)] // OK [Plugin("My Cool Plugin")] // OK Second on access on attribute properties: In order to access plugin.Name on this code: var type = typeof(MyPlugin); // Returns a Type object representing our MyPlugin class var attributes = System.Attribute.GetCustomAttributes(type); // Returns an Attribute[] foreach (var a in attributes) { if (a is PluginAttribute plugin) Console.WriteLine($"Plugin Name: {plugin.Name}"); } You need to cast if first to the right class, then access the properties, as the above code will generate a compile-time error: var type = typeof(PluginAttribute); // Returns a Type object representing our PluginAttribute class var attributes = System.Attribute.GetCustomAttributes(type); // Returns an Attribute[] foreach (var a in attributes) { if (a is PluginAttribute) { PluginAttribute plug = (PluginAttribute)a; //Cast it first to PluginAttribute class before you can access all accessible properties Console.WriteLine($"Plugin Name: {plug.Name}"); } } --- guide/english/csharp/attributes/index.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/guide/english/csharp/attributes/index.md b/guide/english/csharp/attributes/index.md index ca65845ac5..92c1c506ae 100644 --- a/guide/english/csharp/attributes/index.md +++ b/guide/english/csharp/attributes/index.md @@ -37,8 +37,8 @@ public class PluginAttribute : Attribute } } -[Plugin(MyPluginName)] // Won't compile because MyPluginName isn't const -[Plugin(MyConstPluginName)] // OK +[Plugin(Variables.MyPluginName)] // Won't compile because MyPluginName isn't const +[Plugin(Variables.MyConstPluginName)] // OK [Plugin("My Cool Plugin")] // OK public class MyPlugin { @@ -51,13 +51,16 @@ The `System.Attribute.GetCustomAttributes(Type)` method returns an array of all ```csharp public void PrintPluginName() { - var type = typeof(MyPlugin); // Returns a Type object representing our MyPlugin class + var type = typeof(PluginAttribute); // Returns a Type object representing our PluginAttribute class var attributes = System.Attribute.GetCustomAttributes(type); // Returns an Attribute[] foreach (var a in attributes) { - if (a is PluginAttribute plugin) - Console.WriteLine($"Plugin Name: {plugin.Name}"); + if (a is PluginAttribute) + { + PluginAttribute plug = (PluginAttribute) a; //Cast it first to PluginAttribute class before you can access all accessible properties + Console.WriteLine($"Plugin Name: {plug.Name}"); + } } } ```