c# - Creating custom attributes for methods that has specific signature -


i created simple class has attributeusage attribute. when tried build got error:

attribute 'attributeusage' valid on classes derived system.attribute.

then made class inherits attribute , fine.

if use attributeusage attribute forces me inherit attribute class. question can make attribute forces methods have specific signature?

thanks help!

if understand correctly: looking tool controls compilation attributes. may check if stylecop or fxcop fit needs. alternatively might extend visual studio access source code model. extension may check source code (see http://blogs.msdn.com/b/sqlserverstorageengine/archive/2007/03/02/using-visual-studio-s-code-model-objects-for-code-base-understanding.aspx), evaluates attributes , generate compiler warnings, errors whatever like.

===================================

original answer:

answer: have provide corresponding constructor

[attributeusage(attributetargets.all)] public class helpattribute : system.attribute {     // required parameter     public helpattribute(string url)      {         this.url = url;     }      // optional parameter     public string topic { get; set; }      public readonly string url;   }  public class whatever {     [help("http://www.stackoverflow.com")]     public int id { get; set; }      [help("http://www.stackoverflow.com", topic="mytopic")]     public int wew { get; set; }      // [help] // without parameters not compile     public int wow { get; set; } } 

the allowed parameter types liste @ msdn: http://msdn.microsoft.com/en-us/library/aa664615(v=vs.71).aspx

further reading: http://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

java - More than one row with the given identifier was found: 1, for class: com.model.Diagnosis -