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}");
                    }
	}
			
			
This commit is contained in:
		
				
					committed by
					
						
						Randell Dawson
					
				
			
			
				
	
			
			
			
						parent
						
							ad5b99ed09
						
					
				
				
					commit
					a9fb8ac1c3
				
			@@ -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}");
 | 
			
		||||
                    }
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user