I am working on an application that generates a tree structure of nodes. There are many types of nodes, each of which has a specific behavior and properties. I want to bind each node type to properties, including display name, description and 16x16 icon.
Here is the code for the custom attribute that I created:
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;
}
}
Notice that I have a constructor that points the icon as the path to the file, and a constructor that points the icon as System.Drawing.Image.
So, ultimately, I would like to use this attribute with an embedded image resource like this.
[NodeTypeInfo("My Node","Sample Description",Properties.Resources.CustomIcon)]
public class CustomNode:Node
{
...
However, this code returns an error
An attribute argument must be a constant expression, typeof expression or
array creation` expression of an attribute parameter type
Is there any other way to associate a type (not an instance) of a class with an icon image?