Evaluate Runtime Expression in a Custom Attribute?

L

lucius

I have a custom Attribute Class that decorates a primary class like in
the example below. I would now like to decorate the primary class with
the Attribute, but let the Attribute do a runtime evaluation of an
expression and throw if it fails the expression. Can someone show me
how to rewrite the CustomClassAttribute to support this?

Thanks.


Current pseudocode -

[CustomClassAttribute( SecondaryClass , ClassProperty , 1 )]
class PrimaryClass
{
....
}

[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method,
AllowMultiple = true)]
public class CustomClassAttribute : System.Attribute
{
public CustomClassAttribute ( Type someType , Int someProperty
, Int intValue )
{
...
}
}




Wanted pseudocode -

[CustomClassAttribute( "SomeType.InstanceProperty > 1" )]
class PrimaryClass
{
....
}

[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method,
AllowMultiple = true)]
public class CustomClassAttribute : System.Attribute
{
public CustomClassAttribute ( string checkExpression )
{
...
}
}
 
S

Steven Cheng[MSFT]

Hi lucius,

From your description, you're wantting to develop a custom attribute which
will help validate the value of some properties on the target
class/instance, correct?

Based on my understanding, .NET attributes can help add some additional
information on class or class members(method or property), however, unless
you're the class designer, you can only use external code to query the
attribute from class/objects and do some operations based on them(but not
let the classes automatically use them). How will you actually query the
attributes here?

Also, for expression evaluation, if your expression will contains both
string format expression and other numeric expression, I think it is
difficult to encapsulate it in a single attribute. I suggest you consider
develop a expression attribute collection , and adopt the .NET validation
control hierarchy. You can create multiple custom attributes that dedicated
on specific expressions. For example:

** numberic range expression
** max,min value expression
** regular expression (for string value)

This can make the work much more easier. Also for math expression related
evaluation, here are some web articles discuss on it:

#.NET Math Expression Parser
http://www.bestcode.com/html/bcparser_net.html

http://www.c-sharpcorner.com/UploadFile/patricklundin/MathExpParser120620050
62213AM/MathExpParser.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Robert Simpson

What kind of expression do you want to evaluate? Will there be a lot of
them, or is this a one-time-per-class thing?

You can use the Microsoft.CSharp.CSharpCodeProvider and dynamically compile
it if its complex, or parse it yourself and emit the IL code using the
System.Reflection.Emit namespace (DynamicMethod should do the trick).

You could also have a static constructor somewhere that iterates all the
loaded assemblies and finds all classes with your special attribute, then
compile all the expressions for all the classes into one big
"expressionclass" using CSharpCodeProvider -- then call into the methods
using reflection to evaluate the expression at runtime.

Robert
 
L

lucius

I am the class designer.I would like to see class code showing an
example of how to do what I need.

Thanks.
 
S

Steven Cheng[MSFT]

Hi Lucius,

If you wonder how to use the custom attributes and retrieve the
values(defined in attributes), you can refer to the following articles:

#Designing With Custom Attributes
http://msdn.microsoft.com/msdnmag/issues/05/05/BasicInstincts/

#Retrieving the Values of the Custom Attributes
http://www.code-magazine.com/article.aspx?quickid=0307041&page=4

and for class designer, in class's code, you can simpy use typeof(this) to
get the type reference of current class type and query the custom
attributes applied on it.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Lucius,

Have you got any further progress? If there is any further question, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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