Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# CodeDOM
Hello Folks,
 
I am new using codeDom and after some tutorials I was able to build interesting things. However I am facing a limitation regarding how to set custom attributes declaration for a method. e.g.
 
[PexMethod (Max Branches = 1000)]
public void myMethod()
....
 
I am able to declare just [PexMethod()], but I am not able to set the "Max Branches..". My intention is to build parameterized unit tests and then let Pex explore my custom PUT method.
If someone could please give a hint I will really appreciate it.
 
Thanks in advance.
 
Pedro
Posted 30-Apr-12 12:18pm
Comments
SAKryukov at 30-Apr-12 17:51pm
   
How come it's turned out to be a problem? Just use valid .NET identifier, like MaxBranches.
--SA
satrio_budidharmawan at 30-Apr-12 23:29pm
   
Yes, agree wit --SA.
How come you change the way .Net works...declaring something with with_white_space..
petBorromeo at 1-May-12 8:57am
   
Yep, it was a mistake by writting the declaration. However the solution provided by SA has solve my problem. Thanks for your help guys!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Attributes are powerful, useful, but very limited in several aspects. But — rejoice! — not that limited as you might think. They still allow some good set of parameter types:
http://msdn.microsoft.com/en-us/library/ms177221%28v=vs.100%29.aspx[^].
 
This is how it may look in your case:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
class PexMethodAttribute : System.Attribute {
    public int MaxBranches { internal get; set; }
}
 
Pay attention for internal. You can check the attribute valued only in the same assembly where it is declared, which is pretty reasonable. Most likely, you will need to have the setter set public, because you set it in the code applying the attribute.
 
Here is how you can apply your attribute type to some method:
 
[PexMethod(MaxBranches = 1000)]
void SomeMethod(/* ... */) {/* ... */}
 
Alternatively, you can use constructor syntax for the parameter:
 
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
class PexMethodAttribute : System.Attribute {
    internal int MaxBranches { get; set; }
    public PexMethodAttribute(int maxBranches) { this.MaxBranches = maxBranches; }
}
 
And the application of it would look a bit different:
[PexMethod(1010)]
void SomeMethod(/* ... */) {/* ... */}
 
—SA
  Permalink  
v5
Comments
Sandeep Mewara at 1-May-12 3:30am
   
My 5+ !
petBorromeo at 1-May-12 8:58am
   
Thank you very much SA, your quick answer has help me a lot.!!
SAKryukov at 1-May-12 10:53am
   
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100