Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

Evaluate Runtime Expression in a Custom Attribute?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lucius - 18 Jun 2007 16:20 GMT
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 )
    {
        ...
    }
}
Steven Cheng[MSFT] - 19 Jun 2007 06:05 GMT
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.
lucius - 20 Jun 2007 16:12 GMT
I am the class designer.I would like to see class code showing an
example of how to do what I need.

Thanks.

>Hi lucius,
>
[quoted text clipped - 57 lines]
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 25 Jun 2007 04:07 GMT
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.
Steven Cheng[MSFT] - 27 Jun 2007 13:14 GMT
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.
Robert Simpson - 19 Jun 2007 06:23 GMT
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

> 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
[quoted text clipped - 40 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.