Пример #1
0
        private void GenerateImplementsElement(IProcessingContext context, MemberInfo mInfo)
        {
            Type declaringType = mInfo.DeclaringType;

            if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
                declaringType = declaringType.GetGenericTypeDefinition();

            if (!declaringType.IsInterface)
            {
                Type[] interfaces = declaringType.GetInterfaces();
                foreach (Type ifType in interfaces)
                {
                    if (context.IsFiltered(AssetIdentifier.FromMemberInfo(ifType)))
                        continue;

                    InterfaceMapping ifMap = declaringType.GetInterfaceMap(ifType);
                    if (ifMap.TargetType != declaringType)
                        continue;

                    var targetMethod =
                        ifMap.TargetMethods.SingleOrDefault(mi => mi.MetadataToken == mInfo.MetadataToken &&
                                                                  mi.Module == mInfo.Module);

                    if (targetMethod != null)
                    {
                        int mIx = Array.IndexOf(ifMap.TargetMethods, targetMethod);

                        AssetIdentifier miAid;
                        if (ifMap.InterfaceMethods[mIx].DeclaringType.IsGenericType)
                        {
                            Type declType = ifMap.InterfaceMethods[mIx].DeclaringType.GetGenericTypeDefinition();
                            MethodInfo[] allMethods =
                                declType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                                                    BindingFlags.Static);

                            miAid =
                                AssetIdentifier.FromMemberInfo(allMethods.Single(mi =>
                                                                                 mi.MetadataToken == ifMap.InterfaceMethods[mIx].MetadataToken &&
                                                                                 mi.Module == ifMap.InterfaceMethods[mIx].Module));
                        }
                        else
                        {
                            miAid = AssetIdentifier.FromMemberInfo(ifMap.InterfaceMethods[mIx]);
                        }

                        context.Element.Add(new XElement("implements", new XAttribute("member", miAid)));
                        context.AddReference(miAid);
                    }
                }
            }
        }
Пример #2
0
        private XElement GenerateTypeElement(IProcessingContext context, AssetIdentifier assetId)
        {
            XElement ret;
            Type type = (Type)context.AssetResolver.Resolve(assetId);


            string elemName;

            if (type.IsClass)
                elemName = "class";
            else if (type.IsEnum)
                elemName = "enum";
            else if (type.IsValueType)
                elemName = "struct";
            else if (type.IsInterface)
                elemName = "interface";
            else
                throw new ArgumentException("Unknown asset type: " + assetId.Type.ToString(), "assetId");

            ret = new XElement(elemName,
                               new XAttribute("name", type.Name),
                               new XAttribute("assetId", assetId),
                               new XAttribute("phase", context.Phase));

            if (type.IsEnum)
            {
                AssetIdentifier aid = AssetIdentifier.FromType(type.GetEnumUnderlyingType());
                ret.Add(new XAttribute("underlyingType", aid));
                context.AddReference(aid);
            }


            if (!type.IsInterface && type.IsAbstract)
                ret.Add(new XAttribute("isAbstract", XmlConvert.ToString(type.IsAbstract)));

            if (!type.IsVisible || type.IsNested && type.IsNestedAssembly)
                ret.Add(new XAttribute("isInternal", XmlConvert.ToString(true)));

            if (type.IsPublic || type.IsNested && type.IsNestedPublic)
                ret.Add(new XAttribute("isPublic", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedPrivate)
                ret.Add(new XAttribute("isPrivate", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedFamily)
                ret.Add(new XAttribute("isProtected", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedFamANDAssem)
                ret.Add(new XAttribute("isProtectedAndInternal", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedFamORAssem)
                ret.Add(new XAttribute("isProtectedOrInternal", XmlConvert.ToString(true)));

            if (type.IsClass && type.IsSealed)
                ret.Add(new XAttribute("isSealed", XmlConvert.ToString(true)));

            if (type.BaseType != null)
            {
                AssetIdentifier baseAid = AssetIdentifier.FromType(type.BaseType);
                if (!context.IsFiltered(baseAid))
                {
                    var inheritsElem = new XElement("inherits");
                    ret.Add(inheritsElem);
                    GenerateTypeRef(context.Clone(inheritsElem), type.BaseType);
                }
            }

            if (type.ContainsGenericParameters)
            {
                Type[] typeParams = type.GetGenericArguments();
                foreach (Type tp in typeParams)
                {
                    this.GenerateTypeParamElement(context.Clone(ret), type, tp);
                }
            }

            if (type.IsClass)
            {
                foreach (Type interfaceType in type.GetInterfaces())
                {
                    InterfaceMapping mapping = type.GetInterfaceMap(interfaceType);
                    if (mapping.TargetType == type)
                    {
                        AssetIdentifier interfaceAssetId =
                            AssetIdentifier.FromType(interfaceType.IsGenericType
                                                         ? interfaceType.GetGenericTypeDefinition()
                                                         : interfaceType);
                        if (!context.IsFiltered(interfaceAssetId))
                        {
                            var implElement = new XElement("implements");
                            ret.Add(implElement);
                            GenerateTypeRef(context.Clone(implElement), interfaceType, "interface");
                        }
                    }
                }
            }


            foreach (IEnricher enricher in this._enrichers)
                enricher.EnrichType(context.Clone(ret), type);


            context.Element.Add(ret);

            return ret;
        }
Пример #3
0
        private XElement GenerateTypeElement(IProcessingContext context, Asset asset)
        {
            XElement ret;
            Type type = (Type)asset.Target;

            string elemName;

            if (type.IsClass)
                elemName = "class";
            else if (type.IsEnum)
                elemName = "enum";
            else if (type.IsValueType)
                elemName = "struct";
            else if (type.IsInterface)
                elemName = "interface";
            else
                throw new ArgumentException("Unknown asset type: " + asset.Type.ToString(), "asset");

            //XTypeBuilder typeBuilder = new XTypeBuilder(type.Name, asset.Id, context.Phase);
            //typeBuilder.A
            //ret = typeBuilder.ToElement();

            ret = new XElement(elemName,
                               new XAttribute("name", type.Name),
                               new XAttribute("assetId", asset.Id),
                               new XAttribute("phase", context.Phase));

            if (type.IsEnum)
            {
                Type underlyingType = type.GetEnumUnderlyingType();
                AssetIdentifier aid = AssetIdentifier.FromType(underlyingType);
                ret.Add(new XAttribute("underlyingType", aid));
                context.AddReference(new Asset(aid, underlyingType));
            }


            if (!type.IsInterface && type.IsAbstract)
                ret.Add(new XAttribute("isAbstract", XmlConvert.ToString(type.IsAbstract)));

            if (!type.IsVisible || type.IsNested && type.IsNestedAssembly)
                ret.Add(new XAttribute("isInternal", XmlConvert.ToString(true)));

            if (type.IsPublic || type.IsNested && type.IsNestedPublic)
                ret.Add(new XAttribute("isPublic", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedPrivate)
                ret.Add(new XAttribute("isPrivate", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedFamily)
                ret.Add(new XAttribute("isProtected", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedFamANDAssem)
                ret.Add(new XAttribute("isProtectedAndInternal", XmlConvert.ToString(true)));

            if (type.IsNested && type.IsNestedFamORAssem)
                ret.Add(new XAttribute("isProtectedOrInternal", XmlConvert.ToString(true)));

            if (type.IsClass && type.IsSealed)
                ret.Add(new XAttribute("isSealed", XmlConvert.ToString(true)));

            if (type.BaseType != null)
            {
                AssetIdentifier baseAid = AssetIdentifier.FromType(type.BaseType);
                Asset baseAsset = new Asset(baseAid, type.BaseType);
                if (!context.IsFiltered(baseAsset))
                {
                    var inheritsElem = new XElement("inherits");
                    ret.Add(inheritsElem);
                    GenerateTypeRef(context.Clone(inheritsElem), type.BaseType);
                }
            }

            if (type.ContainsGenericParameters)
            {
                Type[] typeParams = type.GetGenericArguments();
                if (type.IsNested && type.DeclaringType.ContainsGenericParameters)
                {
                    Type[] inheritedTypeParams = type.DeclaringType.GetGenericArguments();

                    Debug.Assert(typeParams.Length >= inheritedTypeParams.Length);

                    for (int paramPos = 0; paramPos < inheritedTypeParams.Length; paramPos++)
                    {
                        Debug.Assert(typeParams[paramPos].Name == inheritedTypeParams[paramPos].Name);
                        Debug.Assert(typeParams[paramPos].GenericParameterAttributes == inheritedTypeParams[paramPos].GenericParameterAttributes);
                    }

                    Type[] declaredTypeParams = new Type[typeParams.Length - inheritedTypeParams.Length];

                    for (int paramPos = inheritedTypeParams.Length; paramPos < typeParams.Length; paramPos++)
                    {
                        declaredTypeParams[paramPos - inheritedTypeParams.Length] = typeParams[paramPos];
                    }

                    typeParams = declaredTypeParams;
                }

                foreach (Type tp in typeParams)
                {
                    this.GenerateTypeParamElement(context.Clone(ret), type, tp);
                }
            }

            if (type.IsClass)
            {
                foreach (Type interfaceType in type.GetInterfaces())
                {
                    InterfaceMapping mapping = type.GetInterfaceMap(interfaceType);

                    if (mapping.TargetMethods.Any(mi => mi.DeclaringType == type))
                    {
                        Type ifType = interfaceType.IsGenericType
                                          ? interfaceType.GetGenericTypeDefinition()
                                          : interfaceType;
                        AssetIdentifier interfaceAssetId = AssetIdentifier.FromType(ifType);

                        Asset interfaceAsset = new Asset(interfaceAssetId, ifType);

                        if (!context.IsFiltered(interfaceAsset))
                        {
                            var implElement = new XElement("implements");
                            ret.Add(implElement);
                            GenerateTypeRef(context.Clone(implElement), interfaceType, "interface");
                        }
                    }
                }
            }


            foreach (IEnricher enricher in this._enrichers)
                enricher.EnrichType(context.Clone(ret), type);


            context.Element.Add(ret);

            return ret;
        }
        protected virtual void GenerateAttributeElements(IProcessingContext context, IEnumerable<CustomAttributeData> attrData)
        {
            foreach (CustomAttributeData custAttr in attrData)
            {
                Type originatingType = custAttr.Constructor.ReflectedType
                                       ?? custAttr.Constructor.DeclaringType;

                Asset typeAsset = ReflectionServices.GetAsset(originatingType);

                if (context.IsFiltered(typeAsset))
                    continue;

                Asset ctorAsset = ReflectionServices.GetAsset(custAttr.Constructor);
                context.AddReference(ctorAsset);

                var attrElem = new XElement("attribute",
                                            new XAttribute("type", typeAsset.Id),
                                            new XAttribute("constructor", ctorAsset.Id));

                foreach (CustomAttributeTypedArgument cta in custAttr.ConstructorArguments)
                {
                    XElement argElem = new XElement("argument");

                    this.GenerateValueLiteral(context.Clone(argElem), cta);

                    attrElem.Add(argElem);
                }

                foreach (CustomAttributeNamedArgument cta in custAttr.NamedArguments)
                {
                    Asset asset = ReflectionServices.GetAsset(cta.MemberInfo);
                    context.AddReference(asset);

                    XElement argElem = new XElement("argument",
                                                    new XAttribute("member", asset.Id));

                    this.GenerateValueLiteral(context.Clone(argElem), cta.TypedValue);

                    attrElem.Add(argElem);
                }


                context.Element.Add(attrElem);
            }
        }
Пример #5
0
        // TODO fix this, and the accompanying XSLT template, the construction of attribute 
        // arguments isn't very consitent 
        private static void GenerateAttributeElements(IProcessingContext context,
                                                      IEnumerable<CustomAttributeData> attrData)
        {
            foreach (CustomAttributeData custAttr in attrData)
            {
                AssetIdentifier typeAssetId =
                    AssetIdentifier.FromMemberInfo(custAttr.Constructor.ReflectedType
                                                   ?? custAttr.Constructor.DeclaringType);

                if (context.IsFiltered(typeAssetId))
                    continue;

                context.AddReference(AssetIdentifier.FromMemberInfo(custAttr.Constructor));

                var attrElem = new XElement("attribute",
                                            new XAttribute("type",
                                                           typeAssetId),
                                            new XAttribute("constructor",
                                                           AssetIdentifier.FromMemberInfo(custAttr.Constructor)));

                foreach (CustomAttributeTypedArgument cta in custAttr.ConstructorArguments)
                {
                    if (cta.Value is ReadOnlyCollection<CustomAttributeTypedArgument>)
                    {
                        AssetIdentifier elementAssetId =
                            AssetIdentifier.FromMemberInfo(cta.ArgumentType.GetElementType());
                        context.AddReference(elementAssetId);
                        attrElem.Add(new XElement("argument",
                                                  new XElement("array",
                                                               new XAttribute("type", elementAssetId),
                                                               ((IEnumerable<CustomAttributeTypedArgument>)cta.Value).
                                                                   Select(
                                                                          ata =>
                                                                          new XElement("element",
                                                                                       GenerateAttributeArgument(
                                                                                                                 context,
                                                                                                                 ata))))));
                    }
                    else
                    {
                        attrElem.Add(new XElement("argument",
                                                  GenerateAttributeArgument(context, cta)));
                    }
                }

                foreach (CustomAttributeNamedArgument cta in custAttr.NamedArguments)
                {
                    AssetIdentifier namedMember = AssetIdentifier.FromMemberInfo(cta.MemberInfo);
                    context.AddReference(namedMember);

                    if (cta.TypedValue.Value is ReadOnlyCollection<CustomAttributeTypedArgument>)
                    {
                        context.AddReference(namedMember);
                        AssetIdentifier elementAssetId =
                            AssetIdentifier.FromMemberInfo(cta.TypedValue.ArgumentType.GetElementType());
                        context.AddReference(elementAssetId);
                        attrElem.Add(new XElement("argument",
                                                  new XAttribute("member", namedMember),
                                                  new XElement("array",
                                                               new XAttribute("type", elementAssetId),
                                                               ((IEnumerable<CustomAttributeTypedArgument>)
                                                                cta.TypedValue.Value).Select(
                                                                                             ata =>
                                                                                             new XElement("element",
                                                                                                          GenerateAttributeArgument(context, ata))))));
                    }
                    else
                    {
                        attrElem.Add(new XElement("argument",
                                                  new XAttribute("member", namedMember),
                                                  GenerateAttributeArgument(context, cta.TypedValue)));
                    }
                }


                context.Element.Add(attrElem);
            }

            //using (var ms = new MemoryStream())
            //    context.Element.Save(ms);
        }
Пример #6
0
        // TODO fix this, and the accompanying XSLT template, the construction of attribute
        // arguments isn't very consitent
        private static void GenerateAttributeElements(IProcessingContext context,
                                                      IEnumerable <CustomAttributeData> attrData)
        {
            foreach (CustomAttributeData custAttr in attrData)
            {
                AssetIdentifier typeAssetId =
                    AssetIdentifier.FromMemberInfo(custAttr.Constructor.ReflectedType
                                                   ?? custAttr.Constructor.DeclaringType);

                if (context.IsFiltered(typeAssetId))
                {
                    continue;
                }

                context.AddReference(AssetIdentifier.FromMemberInfo(custAttr.Constructor));

                var attrElem = new XElement("attribute",
                                            new XAttribute("type",
                                                           typeAssetId),
                                            new XAttribute("constructor",
                                                           AssetIdentifier.FromMemberInfo(custAttr.Constructor)));

                foreach (CustomAttributeTypedArgument cta in custAttr.ConstructorArguments)
                {
                    if (cta.Value is ReadOnlyCollection <CustomAttributeTypedArgument> )
                    {
                        AssetIdentifier elementAssetId =
                            AssetIdentifier.FromMemberInfo(cta.ArgumentType.GetElementType());
                        context.AddReference(elementAssetId);
                        attrElem.Add(new XElement("argument",
                                                  new XElement("array",
                                                               new XAttribute("type", elementAssetId),
                                                               ((IEnumerable <CustomAttributeTypedArgument>)cta.Value).
                                                               Select(
                                                                   ata =>
                                                                   new XElement("element",
                                                                                GenerateAttributeArgument(
                                                                                    context,
                                                                                    ata))))));
                    }
                    else
                    {
                        attrElem.Add(new XElement("argument",
                                                  GenerateAttributeArgument(context, cta)));
                    }
                }

                foreach (CustomAttributeNamedArgument cta in custAttr.NamedArguments)
                {
                    AssetIdentifier namedMember = AssetIdentifier.FromMemberInfo(cta.MemberInfo);
                    context.AddReference(namedMember);

                    if (cta.TypedValue.Value is ReadOnlyCollection <CustomAttributeTypedArgument> )
                    {
                        context.AddReference(namedMember);
                        AssetIdentifier elementAssetId =
                            AssetIdentifier.FromMemberInfo(cta.TypedValue.ArgumentType.GetElementType());
                        context.AddReference(elementAssetId);
                        attrElem.Add(new XElement("argument",
                                                  new XAttribute("member", namedMember),
                                                  new XElement("array",
                                                               new XAttribute("type", elementAssetId),
                                                               ((IEnumerable <CustomAttributeTypedArgument>)
                                                                cta.TypedValue.Value).Select(
                                                                   ata =>
                                                                   new XElement("element",
                                                                                GenerateAttributeArgument(context, ata))))));
                    }
                    else
                    {
                        attrElem.Add(new XElement("argument",
                                                  new XAttribute("member", namedMember),
                                                  GenerateAttributeArgument(context, cta.TypedValue)));
                    }
                }


                context.Element.Add(attrElem);
            }

            //using (var ms = new MemoryStream())
            //    context.Element.Save(ms);
        }