Setting a parameter's default value and validating datatype withattributes.

J

Jimmo

I have written a parameter attribute class
("AttributeTargets.Parameter). One of several the constructor
arguments is a parameter to specify a default value for the method's
parameter.

I would like to emulate InteropServices.DefaultParameterValueAttribute
behavior where at compile time, the specified default value is
validated against the method's data type and results in a compilation
failure if the validation fails. I don't see:
a. How I can gain access to the method's parameter info (specifically
datatype) to perform the validation.
b. How the resultant failed validation causes the compiler to fail
with error.

Any suggestions? I prefer not to have to call
DefaultParameterValueAttribute in addition to my attribute class?
Alternatively, is there a way to hijack the
DefaultParameterValueAttribute behavior in my class to achieve the
same result?

Thanks,

- Jim
 
N

Nicholas Paldino [.NET/C# MVP]

Jimmo,

You will have to do this ^after^ compile time. Currently, the compiler
doesn't have any hooks that allow you to do this sort of thing.

However, you can create a custom MSBUILD task which you place in your
project which will fail the build process.

In your task, you would simply reflect on all the methods on all the
types in the assembly output by the project. Since attributes don't know
what they are attached to, you have to take a look at the attributes applied
to all the methods to find where your attribute is applied, then perform the
check.
 
J

Jimmo

Jimmo,

    You will have to do this ^after^ compile time.  Currently, the compiler
doesn't have any hooks that allow you to do this sort of thing.

    However, you can create a custom MSBUILD task which you place in your
project which will fail the build process.

    In your task, you would simply reflect on all the methods on all the
types in the assembly output by the project.  Since attributes don't know
what they are attached to, you have to take a look at the attributes applied
to all the methods to find where your attribute is applied, then perform the
check.

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




I have written a parameter attribute class
("AttributeTargets.Parameter).  One of several the constructor
arguments is a parameter to specify a default value for the method's
parameter.
I would like to emulate InteropServices.DefaultParameterValueAttribute
behavior where at compile time, the specified default value is
validated against the method's data type and results in a compilation
failure if the validation fails.  I don't see:
a.  How I can gain access to the method's parameter info (specifically
datatype) to perform the validation.
b.  How the resultant failed validation causes the compiler to fail
with error.
Any suggestions?  I prefer not to have to call
DefaultParameterValueAttribute in addition to my attribute class?
Alternatively, is there a way to hijack the
DefaultParameterValueAttribute behavior in my class to achieve the
same result?

- Jim- Hide quoted text -

- Show quoted text -

Hi Nicholas,

Not the answer I was hoping for, but the one I expected. I'll give
your approach a shot...

Thanks for responding....

- jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top