c# - Can an attribute references an embeded resource? -


i'm working on application generates tree structure of nodes. there many types of nodes, each specific behavior , properties. want attribute each node type properties including display name, description, , 16x16 icon.

here's code custom attribute 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 = 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;     }  } 

note have constructor specifies icon file path, , constructor specifies icon system.drawing.image.

so i'd able use attribute embedded image resource this.

[nodetypeinfo("my node","sample description",properties.resources.customicon)] public class customnode:node { ... 

however, code returns error

an attribute argument must constant expression, typeof expression or  array creation` expression of attribute parameter type 

is there other way can associate type (not instance) of class icon image?

the arguments attribute constructor stored in assembly metadata. puts severe restrictions on kind of argument types can use. requires code not supported. fails here, accessing properties.resources requires calling property getter.

no great alternative here long want refer resource. resource name, string, can think of. obtain resource object in attribute constructor properties.resources.resourcemanager.getobject()


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -