Example #1
0
        public List <MA.CustomAttribute> GetRawCustomAttributes(object obj, Type type, bool inherit)
        {
            ProjectDom dom;

            if (obj is ProjectDom)
            {
                dom = (ProjectDom)obj;
            }
            else if (obj is IType)
            {
                dom = ((IType)obj).SourceProjectDom;
            }
            else if (obj is IMember)
            {
                dom = ((IMember)obj).DeclaringType.SourceProjectDom;
            }
            else if (obj is IParameter)
            {
                dom = ((IParameter)obj).DeclaringMember.DeclaringType.SourceProjectDom;
            }
            else
            {
                throw new NotSupportedException();
            }

            List <MA.CustomAttribute> atts = new List <MA.CustomAttribute> ();

            foreach (IAttribute att in GetAttributes(obj))
            {
                MA.CustomAttribute catt = ConvertToRawAttribute(dom, att, type.FullName);
                if (catt != null)
                {
                    atts.Add(catt);
                }
            }
            if (inherit && (obj is IType))
            {
                IType td = (IType)obj;
                if (td.BaseType != null && td.BaseType.FullName != "System.Object")
                {
                    IType bt = td.SourceProjectDom.GetType(td.BaseType);
                    if (bt != null)
                    {
                        atts.AddRange(GetRawCustomAttributes(bt, type, true));
                    }
                }
            }
            return(atts);
        }
Example #2
0
        MA.CustomAttribute ConvertToRawAttribute(ProjectDom dom, IAttribute att, string expectedType)
        {
            IType attType = dom.GetType(att.AttributeType);

            if (attType == null || !TypeIsAssignableFrom(expectedType, attType))
            {
                return(null);
            }

            MA.CustomAttribute mat = new MA.CustomAttribute();
            mat.TypeName = att.AttributeType.FullName;

            var arguments = att.PositionalArguments;

            if (arguments.Count > 0)
            {
                IMethod constructor = FindConstructor(dom, att);
                if (constructor == null)
                {
                    throw new InvalidOperationException("Custom attribute constructor not found");
                }

                for (int n = 0; n < arguments.Count; n++)
                {
                    IParameter par = constructor.Parameters[n];
                    object     val = Evaluate(arguments [n]).Value;
                    if (val != null)
                    {
                        string name = par.Name;
                        NodeAttributeAttribute bat = (NodeAttributeAttribute)GetCustomAttribute(par, typeof(NodeAttributeAttribute), false);
                        if (bat != null)
                        {
                            name = bat.Name;
                        }
                        mat.Add(name, Convert.ToString(val, System.Globalization.CultureInfo.InvariantCulture));
                    }
                }
            }

            foreach (var namedArgument in att.NamedArguments)
            {
                string pname = namedArgument.Key;
                object val   = Evaluate(namedArgument.Value).Value;
                if (val == null)
                {
                    continue;
                }

                foreach (IType td in GetInheritanceChain(attType))
                {
                    IMember prop = GetMember(td.Members, pname);
                    if (prop == null)
                    {
                        continue;
                    }

                    NodeAttributeAttribute bat = (NodeAttributeAttribute)GetCustomAttribute(prop, typeof(NodeAttributeAttribute), false);
                    if (bat != null)
                    {
                        string name = string.IsNullOrEmpty(bat.Name) ? prop.Name : bat.Name;
                        mat.Add(name, Convert.ToString(val, System.Globalization.CultureInfo.InvariantCulture));
                    }
                }
            }

            return(mat);
        }