Files
freeCodeCamp/guide/english
Willy David Jr a9fb8ac1c3 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}");
                    }
	}
2019-08-20 09:32:05 -07:00
..
2019-03-31 19:45:51 -07:00
2019-06-23 07:33:09 -05:00
2019-07-19 16:37:17 -05:00
2019-03-23 16:34:51 -07:00
2019-07-19 16:36:14 -05:00
2019-07-05 12:20:54 -07:00
2019-05-11 17:11:24 -04:00
2019-08-20 06:28:56 -07:00
2019-08-07 12:58:39 -07:00
2019-07-07 19:14:08 -05:00
2019-06-27 23:43:06 -07:00
2019-06-21 11:45:57 +01:00
2019-03-22 13:01:58 -07:00
2019-06-28 00:00:46 -07:00
2019-07-05 20:32:58 -07:00
2019-05-26 08:41:12 -07:00
2019-03-20 15:39:02 +05:30
2019-06-28 00:13:55 -07:00
2019-06-23 16:39:50 -05:00