2

I'm reading an opensource c# project, and in some basic classes, there are a lot of custom attributes, like the following:

[Parameter("aaa", typeof(int), "this is aaa")]
[Parameter("bbb", typeof(bool), "this is bbb")]
[Serializable]
public class Number : DataElement
{
       ...
}

Now I need to understand these custom Attributes but there is no documentation. So I need to find which function do things based on these custom Attributes.And from reading these function,I can get the meaning.

Is there a thorough way to locate the function in the project,the function will do sth based on certain custom Attributes?

aicco
  • 47
  • 5
  • What exactly do you mean by "uses these Attributes"? Attributes are nothing more than metadata that describe a code member, and this metadata can be looked up (via any piece of code) through .NET Reflection. See http://stackoverflow.com/questions/720157/finding-all-classes-with-a-particular-attribute – NightOwl888 Jan 06 '16 at 08:08
  • I think I should clarify the meaning of "uses these Attributes",what I mean IS NOT directly use it to some class,like the example in my question.IT IS do something to the class ALREADY described by the Attribute.I want to find some function do something to the class depending on the Attributes it already has. – aicco Jan 06 '16 at 08:17
  • Is this a custom attribute class? In that case, searching for `ParameterAttribute` should lead you to the code that's using it (if it's being used at all). Otherwise, I'd start by looking for the documentation of those attributes. I can find at least two classes by that name: `System.Management.Automation.ParameterAttribute` and `System.Data.Linq.Mapping.ParameterAttribute`, so you'd have to figure out which one you're dealing with first, of course. – Pieter Witvoet Jan 06 '16 at 08:25
  • Note that using 'find all references' on `[Parameter(...)]` looks for references to the attribute's *constructor*. You'll want to find all references to that attribute *class*. Go to its definition, click on the class name and then 'find all references'. – Pieter Witvoet Jan 06 '16 at 08:28
  • oh,I'm sure it is a custom Attribute,I find its definition.And you said that I can search "ParameterAttribute" in entire solution,I've tried that,and I find nothing I want,does this means these Attributes just be defined and give it to the class,but no functions use these Attributes to do things,i.e.they do nothing to the program now? – aicco Jan 06 '16 at 08:31
  • That's possible. Maybe the author intended to use them but didn't get around to it, or they used them as some sort of 'documentation', or maybe this code is part of a library that allows other code to inspect these attributes, but doesn't do so itself. – Pieter Witvoet Jan 06 '16 at 08:39
  • Is there a way I can get a certain answer about the program do sth or not on these Attributes?Or more generally,is there a thorough way to locate the function in the project,the function will do sth based on certain custom Attributes? – aicco Jan 06 '16 at 09:20

3 Answers3

2

Attributes are designed to add metadata to a code element (such as a class). This metadata can be read by any piece of code via .NET Reflection as in this answer.

Some Attributes are read by the compiler (such as [Serializable]), some are read by specific .NET frameworks such as ASP.NET MVC (such as [Route], which only works if you call RouteTable.Routes.MapMvcAttributeRoutes()), and you can make custom Attributes for your own purposes.

In general, I would recommend reading the documentation about the specific Attribute you are interested in finding what it does. If you need to view the code that uses it, you can use a tool such as .NET Reflector or ILSpy (assuming you know which .NET assembly that code is in - again via documentation).

It might help if you would explain why you need this, as there doesn't seem to be much benefit in knowing what function looks up a particular Attribute (unless it is your own custom Attribute), as long as you know what it is for.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • oh,all the Attributes I interested in are the custom Attributes which implemented by the project author,so it is hard to get a documentation for the Attributes.I want to add some new features to the project,thus I need add some new parameters.And I find that every old parameter has a corresponding Attribute,like parameter aaa has [Parameter("aaa", typeof(int), "this is aaa")]. – aicco Jan 06 '16 at 09:00
  • First,the Attributes I interested in are custom Attributes.And second,I want to know the meaning of these Attributes,this open-source project doesn't has Attributes' documentation.So I need to locate the function gather the Attributes' information and use it to do things. – aicco Jan 06 '16 at 09:10
  • Forget the first comment,it has a lot of redundant description. – aicco Jan 06 '16 at 09:11
  • @aicco: if it's an open source project then you probably have access to its revision history. Perhaps searching for the commits in which that attribute class was introduced or modified could yield some useful information. – Pieter Witvoet Jan 06 '16 at 10:34
  • Ok, it is open source. You should be able to create a local copy or clone of the latest release and then do a search for `ParameterAttribute` in the source code using Visual Studio to find out how it is used. You could also open an issue on the project to find out how it is used from the project owner. – NightOwl888 Jan 06 '16 at 10:54
2

Attribute is nothing but an information. This information can be attached to your method, class, namespace, assembly etc.

You will be very much clear after visiting the following URL.

Mahedee
  • 166
  • 7
1

I would suggest you write a snippet of code somewhere, then output to file, console or debugger.

First You need to loop through every method on every type, then check whether it has your attribute you are looking for. I presume it's ParameterAttribute.

For example:

var methods = assembly.GetTypes()
                      .SelectMany(t => t.GetMethods())
                      .Where(m => m.GetCustomAttributes(typeof(ParameterAttribute), false).Length > 0)
                      .ToArray();
foreach (var assemblyMethod in methods)
{
    Console.WriteLine(assemblyMethod.Name);
    // or do other stuff here
}

For more info on getting custom attributes, read Here. Also, this is part of reflection which you can learn more about Here

Eon
  • 3,833
  • 10
  • 46
  • 75
  • I think I should clarify the meaning of "uses these Attributes",what I mean IS NOT directly use it to some class,like the example in my question.IT IS do something to the class ALREADY described by the Attribute.I want to find some function do something to the class depending on the Attributes it already has. – aicco Jan 06 '16 at 08:16