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); }
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); }
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); } }
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); }
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; }