Extracting Attribute from the Ast classes

SyntaxEditor .NET Languages Add-on for Windows Forms Forum

Posted 17 years ago by Erik Pepping - RADVenture B.V
Version: 4.0.0236
Avatar
Hi,

I'm having trouble extracting attribute info from the Ast PropertyDeclaration. Could you please explain how I can retrieve the attribute values from a class structure like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace BO_Layer_ECG
{
    [Serializable]
    [ClassAttribute(IsDataSourceEntity = true)]
    public class Categories : INotifyPropertyChanged
    {
        [ClassAttribute(RelationID = "a3321f33-819e-4c67-8994-637548d22098", CollectionOfType = typeof(Products))]
        public System.Collections.IList Products
        {
            get
            {
                if (_products == null) _products = new System.Collections.ArrayList();

                return _products;
            }
            set
            {
                if (_products != value)
                {
                    _products = value;
                }
            }
        }
    }
}
After the semantic parsing I'm trying to reflect on the example class and need to determine the attributes of the Products property. I'm trying to do this with code like this:
foreach (AttributeSection attributeSection in pProperty.AttributeSections)
{
    foreach (ActiproSoftware.SyntaxEditor.Addons.DotNet.Ast.Attribute attribute in attributeSection.Attributes)
    {
        if (attribute.AttributeType.Name == "ClassAttribute")
        {
            foreach (AttributeArgument argument in attribute.Arguments)
            {
                //argument has argument.Expression
                //How do I extract the RelationID and CollectionOfType attribute values from the example code?
            }
        }
    }
}
I see that AttributeArgument has a Expression attribute, but it's not clear to me how I can extract the RelationID and CollectionOfType attribute values from the example code. Could you please give some hints?

Kind regards,

Erik Pepping

Comments (5)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
RelationID should be in the Name property of the AttributeArgument class. The rest of it will be in the Expression property.


Actipro Software Support

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Thanks for the sample, for the upcoming release we added these (which should have been there in the first place):

1) Added the LiteralExpression.LiteralValue property that will store literal values for number, string, and character types. Right now it is the "raw" value but in the future we may make it smarter so the value converts to remove escaped characters, delimiters, etc.

2) Fixed a bug where typeof expression AST nodes weren't storing the type reference.


Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Avatar
Thanks for fixing the LiteralExpression, but unfortunately I still have a problem with the Bindable(false) attribute. In the example source code I send you, there is a method IsValid which is declared with the following attributes:

[Bindable(false)]
public virtual System.Boolean IsValid

With the code beneath I'm able to extract the attribute name "Bindable", but not the value false
foreach (AttributeSection attributeSection in pProperty.AttributeSections)
{
    foreach (
        ActiproSoftware.SyntaxEditor.Addons.DotNet.Ast.Attribute attribute in attributeSection.Attributes)
    {
        if (attribute.AttributeType.Name == "Bindable")
        {
            if (attribute.Arguments.Count > 0)
            {
            //I'm unable to find the "false" value...
            }
        }
    }
}
Could you please check if I'm right?

Kind regards,

Erik Pepping
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Eric,

Yes that is by design. Since the literal type property will tell you either True or False we didn't store the string value for booleans to save on memory. So the LiteralValue will only return a value for number, string and character literals. All the others you can determine from the LiteralType.


Actipro Software Support

Posted 17 years ago by Erik Pepping - RADVenture B.V
Avatar
Thanks, I missed that!
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.