Introduction
The last blog I did about .NET attributes was to add a simple attribute to an existing method. This entry is about building a "Dictionary" of data into a set of attributes on a method. For example, I want the attributes to document each parameter of a method. So what I want is something that looks like this:[Method(Description="This is the fibonacci sequence"
,Parameters={ {"in", "The input parameter" } })]
int Fibonacci(int in)
{
if (in == 0 || in == 1) return 1;
return Fibonacci(in - 2) + Fibonacci(in - 1);
}
Keep in mind this is NOT valid syntax in .NET (at least I don't think it is).
Attribute Definition
The above description won't work- only because I don't think that C# allows parameter definitions with complex objects as values. However, I found a solution that is pretty good at [1]. The partial solution is to separate each element that would be in the dictionary into a unique attribute. The attribute I put together looks like this:[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
public class SomeParameter : Attribute
{
private string m_name;
private string m_description;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
public string Description
{
get
{
return m_description;
}
set
{
m_description = value;
}
}
}
I put one of the critical sections in bold. The trick is to set the AllowMultiple flag to true, which lets us actually add more than one of these attributes to the method [2]. To use this:
[SomeParameter(Name="input1",Description="The input to fib.")]
public int Fibonacci(int input1)
{
}
Note: Remember to put the public in front of the method! If it is not listed as public, it will not show up when using the reflection method I'm going to show below. This isn't a very good example since fibonacci only takes one parameter. However if I add a new method:
[SomeParameter(Name="parm1",Description="The first input to Calculate2.")]
[SomeParameter(Name="parm2",Description="The second input to Calculate2.")]
public int Calculate2(int parm1, int parm2)
{
}
Why do this? The reason I want to do this is to add a way to query more information about parameters and functions. Here is my complete program to illustrate how the multiple parameters can also be queried:
using System;
[AttributeUsage(AttributeTargets.All)]
public class SomeMethod : Attribute
{
private string m_description;
public string Description
{
get
{
return m_description;
}
set
{
m_description = value;
}
}
}
[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
public class SomeParameter : Attribute
{
private string m_name;
private string m_description;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
public string Description
{
get
{
return m_description;
}
set
{
m_description = value;
}
}
}
public class HelloWorld
{
HelloWorld()
{
}
[SomeMethod(Description="Sample calculation")]
[SomeParameter(Name="parm1", Description="Parameter #1")]
[SomeParameter(Name="parm2", Description="Parameter #2")]
public int Calculate2(int parm1, int parm2)
{
return parm1 + parm2;
}
static public void Main()
{
HelloWorld h = new HelloWorld();
int output = h.Calculate2(4, 5);
Type hwInfo = typeof(HelloWorld);
System.Reflection.MemberInfo[] memberInfo = hwInfo.GetMembers();
foreach(System.Reflection.MemberInfo mInfo in memberInfo)
{
Console.WriteLine(mInfo.ToString());
object[] attributes = mInfo.GetCustomAttributes(true);
Console.WriteLine("\tNumber of attributes: " + attributes.Length);
for(int i=0; i<attributes.Length; i++)
{
System.Console.WriteLine("\t\t" + attributes[i]);
}
}
}
}
This program goes through and prints out all of the methods available in the C# class.
REFERENCES
[1] - http://stackoverflow.com/questions/21265431/how-can-i-accept-a-dictionary-of-data-as-a-custom-net-attribute-parameter
[2] - https://msdn.microsoft.com/en-us/library/tw5zxet9.aspx
It 's an amazing article and useful for developers
ReplyDeleteDot Net Online Training Bangalore
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
selenium training in chennai
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
ReplyDeletejava training in marathahalli | java training in btm layout
java training in jayanagar | java training in electronic city
java training in chennai | java training in USA
selenium training in chennai
Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.
ReplyDeletepython training in pune
python online training
python training in OMR
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteBlueprism training in marathahalli
Blueprism training in btm
Blueprism online training
Well done! Pleasant post! This truly helps me to discover the solutions for my inquiry. Trusting, that you will keep posting articles having heaps of valuable data. You're the best!
ReplyDeleteangularjs Training in chennai
angularjs-Training in pune
angularjs-Training in chennai
angularjs Training in chennai
angularjs-Training in tambaram
I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
ReplyDeleteindustrial course in chennai
Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
ReplyDeleteAWS Interview Questions And Answers
AWS Training in Bangalore | Amazon Web Services Training in Bangalore
AWS Training in Pune | Best Amazon Web Services Training in Pune
Amazon Web Services Training in Pune | Best AWS Training in Pune
AWS Online Training | Online AWS Certification Course - Gangboard
Simply wish to say your article is as astonishing. The clarity in your post is simply great, and I could assume you are an expert on this subject.
ReplyDeletehealth and safrety courses in chennai
Thanks for sharing this information admin, it helps me to learn new things. Continue sharing more like this.
ReplyDeleteRPA Training in Chennai
RPA Training near me
Robotics Process Automation Training in Chennai
Blue Prism Training in Chennai
UiPath Training in Chennai
Data Science Course in Chennai
Data Science Training in Chennai
Its is good and very informative.
ReplyDeleteRegards,
Data Science Course in Chennai | Data Science Training Institute
Informative Blog, Thank you to share this
ReplyDeleteRegards,
Data Science Course in Chennai | R Programming Training in Chennai | Python Training in Chennai
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteDotnet Training in Chennai |Best Dotnet Training course in Chennai
Android Training in Chennai |Best Android Training course in Chennai
CCNA Training in Chennai | Best CCNA Training course in Chennai
MCSE Training in Chennai |Best MCSE Training course in Chennai
Embedded Systems Training in Chennai |Best Embedded Systems Training course in Chennai
Nice Post. the post is really very interesting. while reading this blog the blog is very Informative.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
"Nice Article you have made here, It’s an informative and interesting post, keep it up.Thanks!!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training