Click here to Skip to main content
Rate this: bad
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.
Posted 30-Apr-12 12:18pm
SAKryukov at 30-Apr-12 17:51pm
How come it's turned out to be a problem? Just use valid .NET identifier, like MaxBranches.
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
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:[^].
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:
void SomeMethod(/* ... */) {/* ... */}
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.

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

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 350
2 ProgramFOX 275
3 Maciej Los 275
4 CHill60 260
0 OriginalGriff 360
1 ProgramFOX 265
2 Maciej Los 235
3 CHill60 200
4 RyanDev 170

Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 30 Apr 2012
Copyright © CodeProject, 1999-2015
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