Exemple #1
0
        private IAstAttribute InitializeProperties(IAstAttribute aa)
        {
            var initialized = true;

            foreach (var p in _attribute.NamedArguments)
            {
                var success = SetFieldOrProperty(aa, p);
                initialized = initialized && success;
            }
            return(initialized ? aa : null);
        }
Exemple #2
0
        bool SetFieldOrProperty(IAstAttribute aa, ExpressionPair p)
        {
            ReferenceExpression name = p.First as ReferenceExpression;

            if (null == name)
            {
                _context.Errors.Add(CompilerErrorFactory.NamedParameterMustBeIdentifier(p));
                return(false);
            }
            else
            {
                Reflection.MemberInfo[] members = _type.FindMembers(
                    Reflection.MemberTypes.Property | Reflection.MemberTypes.Field,
                    Reflection.BindingFlags.Instance | Reflection.BindingFlags.Public,
                    Type.FilterName, name.Name);
                if (members.Length > 0)
                {
                    if (members.Length > 1)
                    {
                        _context.Errors.Add(CompilerErrorFactory.AmbiguousReference(name, members));
                        return(false);
                    }
                    else
                    {
                        Reflection.MemberInfo   m        = members[0];
                        Reflection.PropertyInfo property = m as Reflection.PropertyInfo;
                        if (null != property)
                        {
                            property.SetValue(aa, p.Second, null);
                        }
                        else
                        {
                            Reflection.FieldInfo field = m as Reflection.FieldInfo;
                            if (null != field)
                            {
                                field.SetValue(aa, p.Second);
                            }
                            else
                            {
                                throw new InvalidOperationException();
                            }
                        }
                    }
                }
                else
                {
                    _context.Errors.Add(CompilerErrorFactory.NotAPublicFieldOrProperty(name, name.Name, _type.FullName));
                    return(false);
                }
            }
            return(true);
        }
Exemple #3
0
 public void Execute()
 {
     try
     {
         IAstAttribute aa = CreateAstAttributeInstance();
         if (null != aa)
         {
             aa.Initialize(_context);
             using (aa)
             {
                 aa.Apply(_attribute.ParentNode);
             }
         }
     }
     catch (Exception x)
     {
         _context.Errors.Add(CompilerErrorFactory.AttributeApplicationError(x, _attribute, _type));
     }
 }
 public void Execute()
 {
     try
     {
         IAstAttribute aa = CreateAstAttributeInstance();
         if (null != aa)
         {
             aa.Initialize(_context);
             using (aa)
             {
                 aa.Apply(_targetNode);
             }
         }
     }
     catch (Exception x)
     {
         _context.TraceError(x);
         _context.Errors.Add(CompilerErrorFactory.AttributeApplicationError(x, _attribute, _type));
         System.Console.WriteLine(x.StackTrace);
     }
 }
Exemple #5
0
        bool SetFieldOrProperty(IAstAttribute aa, ExpressionPair p)
        {
            var name = p.First as ReferenceExpression;

            if (name == null)
            {
                _context.Errors.Add(CompilerErrorFactory.NamedParameterMustBeIdentifier(p));
                return(false);
            }

            var members = FindMembers(name);

            if (members.Length <= 0)
            {
                _context.Errors.Add(CompilerErrorFactory.NotAPublicFieldOrProperty(name, name.Name, Type()));
                return(false);
            }

            if (members.Length > 1)
            {
                _context.Errors.Add(CompilerErrorFactory.AmbiguousReference(name, members));
                return(false);
            }

            var member   = members[0];
            var property = member as PropertyInfo;

            if (property != null)
            {
                property.SetValue(aa, p.Second, null);
                return(true);
            }

            var field = (FieldInfo)member;

            field.SetValue(aa, p.Second);
            return(true);
        }
Exemple #6
0
        public IAstAttribute CreateAstAttributeInstance()
        {
            object[] parameters = _attribute.Arguments.Count > 0 ? _attribute.Arguments.ToArray() : new object[0];

            IAstAttribute aa = null;

            try
            {
                aa = (IAstAttribute)Activator.CreateInstance(_type, parameters);
            }
            catch (MissingMethodException x)
            {
                _context.Errors.Add(CompilerErrorFactory.MissingConstructor(x, _attribute, _type, parameters));
                return(null);
            }

            aa.Attribute = _attribute;

            if (_attribute.NamedArguments.Count > 0)
            {
                bool initialized = true;

                foreach (ExpressionPair p in _attribute.NamedArguments)
                {
                    bool success = SetFieldOrProperty(aa, p);
                    initialized = initialized && success;
                }

                if (!initialized)
                {
                    return(null);
                }
            }

            return(aa);
        }
 bool SetFieldOrProperty(IAstAttribute aa, ExpressionPair p)
 {
     ReferenceExpression name = p.First as ReferenceExpression;
     if (null == name)
     {
         _context.Errors.Add(CompilerErrorFactory.NamedParameterMustBeIdentifier(p));
         return false;
     }
     else
     {
         Reflection.MemberInfo[] members = _type.FindMembers(
             Reflection.MemberTypes.Property | Reflection.MemberTypes.Field,
             Reflection.BindingFlags.Instance | Reflection.BindingFlags.Public,
             Type.FilterName, name.Name);
         if (members.Length > 0)
         {
             if (members.Length > 1)
             {
                 _context.Errors.Add(CompilerErrorFactory.AmbiguousReference(name, members));
                 return false;
             }
             else
             {
                 Reflection.MemberInfo m = members[0];
                 Reflection.PropertyInfo property = m as Reflection.PropertyInfo;
                 if (null != property)
                 {
                     property.SetValue(aa, p.Second, null);
                 }
                 else
                 {
                     Reflection.FieldInfo field = m as Reflection.FieldInfo;
                     if (null != field)
                     {
                         field.SetValue(aa, p.Second);
                     }
                     else
                     {
                         throw new InvalidOperationException();
                     }
                 }
             }
         }
         else
         {
             _context.Errors.Add(CompilerErrorFactory.NotAPublicFieldOrProperty(name, name.Name, _type.FullName));
             return false;
         }
     }
     return true;
 }
        bool SetFieldOrProperty(IAstAttribute aa, ExpressionPair p)
        {
            var name = p.First as ReferenceExpression;
            if (name == null)
            {
                _context.Errors.Add(CompilerErrorFactory.NamedParameterMustBeIdentifier(p));
                return false;
            }

            var members = FindMembers(name);
            if (members.Length <= 0)
            {
                _context.Errors.Add(CompilerErrorFactory.NotAPublicFieldOrProperty(name, name.Name, Type()));
                return false;
            }

            if (members.Length > 1)
            {
                _context.Errors.Add(CompilerErrorFactory.AmbiguousReference(name, members));
                return false;
            }

            var member = members[0];
            var property = member as PropertyInfo;
            if (property != null)
            {
                property.SetValue(aa, p.Second, null);
                return true;
            }

            var field = (FieldInfo)member;
            field.SetValue(aa, p.Second);
            return true;
        }
		private IAstAttribute InitializeProperties(IAstAttribute aa)
		{
			var initialized = true;
			foreach (var p in _attribute.NamedArguments)
			{
				var success = SetFieldOrProperty(aa, p);
				initialized = initialized && success;
			}
			return initialized ? aa : null;
		}