Je travaille sur une application qui génère une structure arborescente de nœuds. Il existe plusieurs types de nœuds, chacun ayant un comportement et des propriétés spécifiques. Je veux attribuer à chaque type de nœud des propriétés incluant un nom d'affichage, une description et une icône 16x16.Un attribut peut-il faire référence à une ressource incorporée?
Voici le code pour l'attribut personnalisé que j'ai créé:
public class NodeTypeInfoAttribute : Attribute
{
public NodeTypeInfoAttribute(string displayName, string description, System.Drawing.Image icon)
: this(displayName, description)
{
this.Icon = icon;
}
public NodeTypeInfoAttribute(string displayName, string description, string iconPath):this(displayName,description)
{
String absPath;
if (System.IO.Path.IsPathRooted(iconPath))
{
absPath = iconPath;
}
else
{
string folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
absPath = System.IO.Path.Combine(folder, iconPath);
}
try
{
System.Drawing.Image i = System.Drawing.Image.FromFile(absPath);
}
catch (System.IO.FileNotFoundException)
{
Icon = null;
}
}
public NodeTypeInfoAttribute(string displayName, string description)
{
this.DisplayName = displayName;
this.Description = description;
}
public string DisplayName
{
get;
private set;
}
public string Description
{
get;
private set;
}
public System.Drawing.Image Icon
{
get;
set;
}
}
Notez que j'ai un constructeur qui spécifie l'icône comme un chemin de fichier, et un constructeur qui spécifie l'icône comme System.Drawing.Image
. En fin de compte, j'aimerais pouvoir utiliser cet attribut avec une ressource d'image intégrée comme celle-ci.
[NodeTypeInfo("My Node","Sample Description",Properties.Resources.CustomIcon)]
public class CustomNode:Node
{
...
Cependant, ce code renvoie l'erreur
An attribute argument must be a constant expression, typeof expression or
array creation` expression of an attribute parameter type
Y at-il une autre façon, je peux associer un type (pas une instance) d'une classe avec une image d'icône?