private static IEnumerable <XObject> GenerateAttributeArgument(IProcessingContext context, CustomAttributeTypedArgument cata) { // TODO this needs to be cleaned up, and fixed context.AddReference(AssetIdentifier.FromMemberInfo(cata.ArgumentType)); yield return(new XAttribute("type", AssetIdentifier.FromMemberInfo(cata.ArgumentType))); if (cata.ArgumentType.IsEnum) { if ( cata.ArgumentType.GetCustomAttributesData().Any( ca => ca.Constructor.DeclaringType == typeof(FlagsAttribute))) { string flags = Enum.ToObject(cata.ArgumentType, cata.Value).ToString(); string[] parts = flags.Split(','); yield return (new XElement("literal", new XAttribute("value", cata.Value), Array.ConvertAll(parts, s => new XElement("flag", new XAttribute("value", s.Trim()))))); } else { string value = Enum.GetName(cata.ArgumentType, cata.Value); if (value != null) { yield return(new XElement("literal", new XAttribute("value", value))); } yield return(new XElement("literal", new XAttribute("value", cata.Value))); } } else if (cata.ArgumentType == typeof(Type)) { XElement tmp = new XElement("tmp"); DocGenerator.GenerateTypeRef(context.Clone(tmp), (Type)cata.Value, "value"); yield return(tmp.Attribute("value")); foreach (XElement xElement in tmp.Elements()) { yield return(xElement); } } else // TODO fix how this encodes unprintable characters { yield return(new XAttribute("value", cata.Value.ToString().Replace("\0", "\\0"))); } }
private XElement GenerateNamespaceElement(IProcessingContext context, AssetIdentifier assetId) { string ns = (string)context.AssetResolver.Resolve(assetId); var ret = new XElement("namespace", new XAttribute("name", ns), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase)); context.Element.Add(ret); foreach (IEnricher enricher in this._enrichers) enricher.EnrichNamespace(context.Clone(ret), ns); return ret; }
private XElement GenerateAssemblyElement(IProcessingContext context, AssetIdentifier assetId) { Assembly asm = (Assembly)context.AssetResolver.Resolve(assetId); var ret = new XElement("assembly", new XAttribute("name", asm.GetName().Name), new XAttribute("filename", asm.ManifestModule.Name), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase), asm.GetReferencedAssemblies().Select( an => new XElement("references", new XAttribute("assembly", AssetIdentifier. FromAssembly( Assembly . ReflectionOnlyLoad (an . FullName)))))); context.Element.Add(ret); foreach (IEnricher enricher in this._enrichers) enricher.EnrichAssembly(context.Clone(ret), asm); return ret; }
public static void GenerateTypeRef(IProcessingContext context, Type pType, string attrName = null) { if (pType.IsArray) { var arrayElem = new XElement("arrayOf", new XAttribute("rank", pType.GetArrayRank())); context.Element.Add(arrayElem); GenerateTypeRef(context.Clone(arrayElem), pType.GetElementType()); } else { if (pType.IsGenericParameter) context.Element.Add(new XAttribute("param", pType.Name)); else if (pType.IsGenericType) { AssetIdentifier aid = AssetIdentifier.FromType(pType.GetGenericTypeDefinition()); context.AddReference(aid); context.Element.Add(new XAttribute(attrName ?? "type", aid)); foreach (Type genArg in pType.GetGenericArguments()) { XElement argElem = new XElement("with"); GenerateTypeRef(context.Clone(argElem), genArg); context.Element.Add(argElem); } } else { AssetIdentifier aid = AssetIdentifier.FromMemberInfo(pType); context.AddReference(aid); context.Element.Add(new XAttribute(attrName ?? "type", aid)); } } }
private XElement GenerateConstructorElement(IProcessingContext context, AssetIdentifier assetId) { ConstructorInfo constructorInfo = (ConstructorInfo)context.AssetResolver.Resolve(assetId); XElement ret = new XElement("constructor", new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase)); if (constructorInfo.IsStatic) ret.Add(new XAttribute("isStatic", XmlConvert.ToString(constructorInfo.IsStatic))); if (constructorInfo.IsPublic) ret.Add(new XAttribute("isPublic", XmlConvert.ToString(constructorInfo.IsPublic))); if (constructorInfo.IsPrivate) ret.Add(new XAttribute("isPrivate", XmlConvert.ToString(constructorInfo.IsPrivate))); if (constructorInfo.IsFamily) ret.Add(new XAttribute("isProtected", XmlConvert.ToString(constructorInfo.IsFamily))); context.Element.Add(ret); foreach (IEnricher item in this.Enrichers) item.EnrichConstructor(context.Clone(ret), constructorInfo); ParameterInfo[] methodParams = constructorInfo.GetParameters(); this.GenerateParameterElements(context.Clone(ret), methodParams); return ret; }
private XElement GenerateEventElement(IProcessingContext context, AssetIdentifier assetId) { EventInfo eventInfo = (EventInfo)context.AssetResolver.Resolve(assetId); XElement ret = new XElement("event", new XAttribute("name", eventInfo.Name), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase)); GenerateTypeRef(context.Clone(ret), eventInfo.EventHandlerType); MethodInfo addMethod = eventInfo.GetAddMethod(true); MethodInfo removeMethod = eventInfo.GetRemoveMethod(true); if (addMethod != null) { var addElem = new XElement("add"); if (addMethod.IsPublic) addElem.Add(new XAttribute("isPublic", XmlConvert.ToString(addMethod.IsPublic))); if (addMethod.IsPrivate) addElem.Add(new XAttribute("isPrivate", XmlConvert.ToString(addMethod.IsPrivate))); if (addMethod.IsFamily) addElem.Add(new XAttribute("isProtected", XmlConvert.ToString(addMethod.IsFamily))); ret.Add(addElem); } if (removeMethod != null) { var removeElem = new XElement("remove"); if (removeMethod.IsPublic) removeElem.Add(new XAttribute("isPublic", XmlConvert.ToString(removeMethod.IsPublic))); if (removeMethod.IsPrivate) removeElem.Add(new XAttribute("isPrivate", XmlConvert.ToString(removeMethod.IsPrivate))); if (removeMethod.IsFamily) removeElem.Add(new XAttribute("isProtected", XmlConvert.ToString(removeMethod.IsFamily))); ret.Add(removeElem); } context.Element.Add(ret); this.GenerateImplementsElement(context.Clone(ret), eventInfo); foreach (IEnricher item in this.Enrichers) item.EnrichEvent(context.Clone(ret), eventInfo); return ret; }
private XElement GenerateAssemblyElement(IProcessingContext context, Asset asset) { Assembly asm = (Assembly)asset.Target; IEnumerable<XElement> references = asm.GetReferencedAssemblies() .Select(an => new XElement("references", new XAttribute("assembly", AssetIdentifier.FromAssembly(context.AssemblyLoader.Load(an.FullName))))); XElement ret = new XElement("assembly", new XAttribute("name", asm.GetName().Name), new XAttribute("filename", asm.ManifestModule.Name), new XAttribute("assetId", asset.Id), new XAttribute("phase", context.Phase), references); context.Element.Add(ret); foreach (IEnricher enricher in this._enrichers) enricher.EnrichAssembly(context.Clone(ret), asm); return ret; }
private XElement GenerateMethodElement(IProcessingContext context, AssetIdentifier assetId) { // Debug.Assert(context.Element.Name.LocalName != "type", "Cannot put Method into closed generic type"); MethodBase mBase = (MethodBase)context.AssetResolver.Resolve(assetId); if (mBase is ConstructorInfo) return this.GenerateConstructorElement(context, assetId); MethodInfo mInfo = (MethodInfo)mBase; string elemName; if (this.IsOperator(mInfo)) elemName = "operator"; else elemName = "method"; XElement ret = new XElement(elemName, new XAttribute("name", mInfo.Name), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase)); context.Element.Add(ret); Type declaringType = mInfo.DeclaringType; if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition) declaringType = declaringType.GetGenericTypeDefinition(); MethodInfo realMethodInfo = declaringType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Single( mi => mi.MetadataToken == mInfo.MetadataToken && mi.Module == mInfo.Module); AssetIdentifier declaredAs = AssetIdentifier.FromMemberInfo(realMethodInfo); if (declaringType != mInfo.ReflectedType) { ret.Add(new XAttribute("declaredAs", declaredAs)); context.AddReference(declaredAs); } else if (realMethodInfo.GetBaseDefinition() != realMethodInfo) { MethodInfo baseMethod = realMethodInfo.GetBaseDefinition(); if (baseMethod.ReflectedType.IsGenericType) { Type realTypeBase = baseMethod.ReflectedType.GetGenericTypeDefinition(); MethodInfo[] allMethods = realTypeBase.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); baseMethod = allMethods.Single( m => m.Module == baseMethod.Module && m.MetadataToken == baseMethod.MetadataToken); } declaredAs = AssetIdentifier.FromMemberInfo(baseMethod); ret.Add(new XAttribute("overrides", declaredAs)); context.AddReference(declaredAs); } this.GenerateImplementsElement(context.Clone(ret), mInfo); this.GenerateAccessModifiers(ret, mInfo); if (mInfo.ContainsGenericParameters) { Type[] typeParams = mInfo.GetGenericArguments(); foreach (Type tp in typeParams) this.GenerateTypeParamElement(context.Clone(ret), mInfo, tp); } foreach (IEnricher item in this.Enrichers) item.EnrichMethod(context.Clone(ret), mInfo); ParameterInfo[] methodParams = mInfo.GetParameters(); this.GenerateParameterElements(context.Clone(ret), methodParams); if (mInfo.ReturnType != typeof(void)) { XElement retElem = new XElement("returns"); GenerateTypeRef(context.Clone(retElem), mInfo.ReturnType); foreach (IEnricher item in this.Enrichers) item.EnrichReturnValue(context.Clone(retElem), mInfo); ret.Add(retElem); } 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); } }
public static void GenerateTypeRef(IProcessingContext context, Type pType, string attrName = null) { // TODO rethink how we generate the typerefs, probably ensure we always output a root element rather than just the attribute for param/type if (pType.IsArray) { // TODO arrayOf is the only capitalized element var arrayElem = new XElement("arrayOf", new XAttribute("rank", pType.GetArrayRank())); context.Element.Add(arrayElem); GenerateTypeRef(context.Clone(arrayElem), pType.GetElementType()); } else { if (pType.IsGenericParameter) context.Element.Add(new XAttribute("param", pType.Name)); else if (pType.IsGenericType) { Type typeDefinition = pType.GetGenericTypeDefinition(); AssetIdentifier aid = AssetIdentifier.FromType(typeDefinition); context.AddReference(new Asset(aid, typeDefinition)); context.Element.Add(new XAttribute(attrName ?? "type", aid)); foreach (Type genArg in pType.GetGenericArguments()) { XElement argElem = new XElement("with"); GenerateTypeRef(context.Clone(argElem), genArg); context.Element.Add(argElem); } } else { AssetIdentifier aid = AssetIdentifier.FromMemberInfo(pType); context.AddReference(new Asset(aid, pType)); context.Element.Add(new XAttribute(attrName ?? "type", aid)); } } }
protected virtual void GenerateArrayLiteral(IProcessingContext context, Type elementType, IEnumerable<CustomAttributeTypedArgument> arrayValues) { XElement arrayElement = new XElement("arrayOf", new XAttribute("rank", 1)); // attributes only suport one-dimensional arrays DocGenerator.GenerateTypeRef(context.Clone(arrayElement), elementType); foreach (CustomAttributeTypedArgument cta in arrayValues) { XElement elementElement = new XElement("element"); this.GenerateValueLiteral(context.Clone(elementElement), cta); arrayElement.Add(elementElement); } context.Element.Add(arrayElement); }
protected virtual void GenerateValueLiteral(IProcessingContext context, CustomAttributeTypedArgument cta) { var arrayValues = cta.Value as IEnumerable<CustomAttributeTypedArgument>; if (arrayValues != null) { Debug.Assert(cta.ArgumentType.IsArray); this.GenerateArrayLiteral(context, cta.ArgumentType.GetElementType(), arrayValues); } else if (cta.ArgumentType == typeof(Type)) { XElement typeElement = new XElement("typeRef"); if (cta.Value == null) this.GenerateNullLiteral(context.Clone(typeElement)); else DocGenerator.GenerateTypeRef(context.Clone(typeElement), (Type)cta.Value); context.Element.Add(typeElement); } else { XElement constElement = new XElement("constant"); DocGenerator.GenerateTypeRef(context.Clone(constElement), cta.ArgumentType); if (cta.Value == null) this.GenerateNullLiteral(context.Clone(constElement)); else if (cta.ArgumentType == typeof(string) && InvalidCharacters.IsMatch((string)cta.Value)) { string rawValue = (string)cta.Value; var matches = InvalidCharacters.Matches(rawValue); int startPos = 0; foreach (Match match in matches) { int invalidPos = match.Groups[0].Index; Debug.Assert(match.Groups[0].Length == 1); if (startPos < invalidPos) constElement.Add(rawValue.Substring(startPos, invalidPos - startPos)); constElement.Add(new XElement("char", new XAttribute("value", (short)match.Groups[0].Value[0]))); startPos = invalidPos + match.Groups[0].Length; } // add trailing bit constElement.Add(rawValue.Substring(startPos)); } else { constElement.Add(new XAttribute("value", cta.Value)); } context.Element.Add(constElement); } }
private static IEnumerable<XObject> GenerateAttributeArgument(IProcessingContext context, CustomAttributeTypedArgument cata) { // TODO this needs to be cleaned up, and fixed context.AddReference(AssetIdentifier.FromMemberInfo(cata.ArgumentType)); yield return new XAttribute("type", AssetIdentifier.FromMemberInfo(cata.ArgumentType)); if (cata.ArgumentType.IsEnum) { if ( cata.ArgumentType.GetCustomAttributesData().Any( ca => ca.Constructor.DeclaringType == typeof(FlagsAttribute))) { string flags = Enum.ToObject(cata.ArgumentType, cata.Value).ToString(); string[] parts = flags.Split(','); yield return new XElement("literal", new XAttribute("value", cata.Value), Array.ConvertAll(parts, s => new XElement("flag", new XAttribute("value", s.Trim())))); } else { string value = Enum.GetName(cata.ArgumentType, cata.Value); if (value != null) yield return new XElement("literal", new XAttribute("value", value)); yield return new XElement("literal", new XAttribute("value", cata.Value)); } } else if (cata.ArgumentType == typeof(Type)) { XElement tmp = new XElement("tmp"); DocGenerator.GenerateTypeRef(context.Clone(tmp), (Type)cata.Value, "value"); yield return tmp.Attribute("value"); foreach (XElement xElement in tmp.Elements()) yield return xElement; } else // TODO fix how this encodes unprintable characters yield return new XAttribute("value", cata.Value.ToString().Replace("\0", "\\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; }
private XElement GenerateNamespaceElement(IProcessingContext context, Asset asset) { NamespaceInfo nsInfo = (NamespaceInfo)asset.Target; var ret = new XElement("namespace", new XAttribute("name", nsInfo.Name), new XAttribute("assetId", asset.Id), new XAttribute("phase", context.Phase)); context.Element.Add(ret); foreach (IEnricher enricher in this._enrichers) enricher.EnrichNamespace(context.Clone(ret), nsInfo.Name); return ret; }
private void GenerateTypeParamElement(IProcessingContext context, MemberInfo mInfo, Type tp) { // AssetIdentifier assetId = AssetIdentifier.FromType(mInfo, tp); var tpElem = new XElement("typeparam", new XAttribute("name", tp.Name)); context.Element.Add(tpElem); foreach (Type constraint in tp.GetGenericParameterConstraints()) { var ctElement = new XElement("constraint"); tpElem.Add(ctElement); GenerateTypeRef(context.Clone(ctElement), constraint); } // enrich typeparam foreach (IEnricher enricher in this.Enrichers) enricher.EnrichTypeParameter(context.Clone(tpElem), tp); }
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; }
private void GenerateParameterElements(IProcessingContext context, ParameterInfo[] methodParams) { foreach (ParameterInfo item in methodParams) { XElement pElem = new XElement("param", new XAttribute("name", item.Name)); Type pType; if (item.ParameterType.IsByRef) pType = item.ParameterType.GetElementType(); else pType = item.ParameterType; if (item.ParameterType.IsByRef && item.IsOut && !item.IsIn) pElem.Add(new XAttribute("isOut", true)); else if (item.ParameterType.IsByRef) pElem.Add(new XAttribute("isRef", true)); GenerateTypeRef(context.Clone(pElem), pType); foreach (IEnricher enricher in this.Enrichers) enricher.EnrichParameter(context.Clone(pElem), item); context.Element.Add(pElem); } }
private void GenerateTypeParamElement(IProcessingContext context, MemberInfo mInfo, Type tp) { var tpElem = new XElement("typeparam", new XAttribute("name", tp.Name)); if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.Contravariant)) tpElem.Add(new XAttribute("isContravariant", XmlConvert.ToString(true))); if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.Covariant)) tpElem.Add(new XAttribute("isCovariant", XmlConvert.ToString(true))); if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) tpElem.Add(new XAttribute("isValueType", XmlConvert.ToString(true))); if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) tpElem.Add(new XAttribute("isReferenceType", XmlConvert.ToString(true))); if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) tpElem.Add(new XAttribute("hasDefaultConstructor", XmlConvert.ToString(true))); context.Element.Add(tpElem); foreach (Type constraint in tp.GetGenericParameterConstraints()) { var ctElement = new XElement("constraint"); tpElem.Add(ctElement); GenerateTypeRef(context.Clone(ctElement), constraint); } // enrich typeparam foreach (IEnricher enricher in this.Enrichers) enricher.EnrichTypeParameter(context.Clone(tpElem), tp); }
private XElement GenerateFieldElement(IProcessingContext context, AssetIdentifier assetId) { object resolve = context.AssetResolver.Resolve(assetId); FieldInfo fieldInfo = (FieldInfo)resolve; XElement ret = new XElement("field", new XAttribute("name", fieldInfo.Name), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase)); if (fieldInfo.IsStatic) ret.Add(new XAttribute("isStatic", XmlConvert.ToString(fieldInfo.IsStatic))); if (fieldInfo.IsPublic) ret.Add(new XAttribute("isPublic", XmlConvert.ToString(fieldInfo.IsPublic))); if (fieldInfo.IsPrivate) ret.Add(new XAttribute("isPrivate", XmlConvert.ToString(fieldInfo.IsPrivate))); if (fieldInfo.IsFamily) ret.Add(new XAttribute("isProtected", XmlConvert.ToString(fieldInfo.IsFamily))); if (fieldInfo.IsFamilyOrAssembly) ret.Add(new XAttribute("isProtectedOrInternal", XmlConvert.ToString(fieldInfo.IsFamilyOrAssembly))); if (fieldInfo.IsFamilyAndAssembly) ret.Add(new XAttribute("isProtectedAndInternal", XmlConvert.ToString(fieldInfo.IsFamilyAndAssembly))); if (fieldInfo.IsSpecialName) ret.Add(new XAttribute("isSpecialName", XmlConvert.ToString(fieldInfo.IsSpecialName))); GenerateTypeRef(context.Clone(ret), fieldInfo.FieldType); context.Element.Add(ret); foreach (IEnricher item in this.Enrichers) item.EnrichField(context.Clone(ret), fieldInfo); return ret; }
private static IEnumerable<XObject> GenerateAttributeArgument(IProcessingContext context, CustomAttributeTypedArgument cata) { context.AddReference(AssetIdentifier.FromMemberInfo(cata.ArgumentType)); yield return new XAttribute("type", AssetIdentifier.FromMemberInfo(cata.ArgumentType)); if (cata.ArgumentType.IsEnum) { if ( cata.ArgumentType.GetCustomAttributesData().Any( ca => ca.Constructor.DeclaringType == typeof(FlagsAttribute))) { var parts = GetEnumFlags(cata.ArgumentType, cata.Value).ToArray(); yield return new XElement("enum", Array.ConvertAll(parts, s => new XElement("flag", new XAttribute("value", s.Trim())))); } else { var val = Enum.GetName(cata.ArgumentType, cata.Value) ?? ""; yield return new XElement("enum", new XAttribute("value", val)); } } else if (cata.ArgumentType == typeof(Type)) { XElement tmp = new XElement("tmp"); DocGenerator.GenerateTypeRef(context.Clone(tmp), (Type)cata.Value, "value"); yield return tmp.Attribute("value"); foreach (XElement xElement in tmp.Elements()) yield return xElement; // yield return new XAttribute("value", AssetIdentifier.FromMemberInfo((Type)cata.Value)); } else yield return new XAttribute("value", cata.Value.ToString()); }
private XElement GeneratePropertyElement(IProcessingContext context, AssetIdentifier assetId) { PropertyInfo propInfo = (PropertyInfo)context.AssetResolver.Resolve(assetId); XElement ret = new XElement("property", new XAttribute("name", propInfo.Name), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase)); GenerateTypeRef(context.Clone(ret), propInfo.PropertyType); ParameterInfo[] pInfos = propInfo.GetIndexParameters(); this.GenerateParameterElements(context.Clone(ret), pInfos); MethodInfo setMethod = propInfo.GetSetMethod(true); MethodInfo getMethod = propInfo.GetGetMethod(true); if ((setMethod ?? getMethod).IsAbstract) ret.Add(new XAttribute("isAbstract", XmlConvert.ToString(true))); if ((setMethod ?? getMethod).IsVirtual) ret.Add(new XAttribute("isVirtual", XmlConvert.ToString(true))); const int C_PUBLIC = 10; const int C_INTERNAL_OR_PROTECTED = 8; const int C_INTERNAL = 6; const int C_PROTECTED = 4; const int C_INTERNAL_AND_PROTECTED = 2; const int C_PRIVATE = 0; int leastRestrictiveAccessModifier; if (setMethod != null && setMethod.IsPublic || getMethod != null && getMethod.IsPublic) { ret.Add(new XAttribute("isPublic", XmlConvert.ToString(true))); leastRestrictiveAccessModifier = C_PUBLIC; } else if (setMethod != null && setMethod.IsFamilyOrAssembly || getMethod != null && getMethod.IsFamilyOrAssembly) { ret.Add(new XAttribute("isInternalOrProtected", XmlConvert.ToString(true))); leastRestrictiveAccessModifier = C_INTERNAL_OR_PROTECTED; } else if (setMethod != null && setMethod.IsAssembly || getMethod != null && getMethod.IsAssembly) { ret.Add(new XAttribute("isInternal", XmlConvert.ToString(true))); leastRestrictiveAccessModifier = C_INTERNAL; } else if (setMethod != null && setMethod.IsFamily || getMethod != null && getMethod.IsFamily) { ret.Add(new XAttribute("isProtected", XmlConvert.ToString(true))); leastRestrictiveAccessModifier = C_PROTECTED; } else if (setMethod != null && setMethod.IsFamilyAndAssembly || getMethod != null && getMethod.IsFamilyAndAssembly) { ret.Add(new XAttribute("isInternalAndProtected", XmlConvert.ToString(true))); leastRestrictiveAccessModifier = C_INTERNAL_AND_PROTECTED; } else if (setMethod != null && setMethod.IsPrivate || getMethod != null && getMethod.IsPrivate) { ret.Add(new XAttribute("isPrivate", XmlConvert.ToString(true))); leastRestrictiveAccessModifier = C_PRIVATE; } else { throw new InvalidOperationException("What the hell happened here?"); } if (setMethod != null) { var setElem = new XElement("set"); if (leastRestrictiveAccessModifier > C_INTERNAL_OR_PROTECTED && setMethod.IsFamilyOrAssembly) setElem.Add(new XAttribute("isInternalOrProtected", XmlConvert.ToString(setMethod.IsFamilyOrAssembly))); if (leastRestrictiveAccessModifier > C_INTERNAL && setMethod.IsAssembly) setElem.Add(new XAttribute("isInternal", XmlConvert.ToString(setMethod.IsAssembly))); if (leastRestrictiveAccessModifier > C_PROTECTED && setMethod.IsFamily) setElem.Add(new XAttribute("isProtected", XmlConvert.ToString(setMethod.IsFamily))); if (leastRestrictiveAccessModifier > C_INTERNAL_AND_PROTECTED && setMethod.IsFamilyAndAssembly) setElem.Add(new XAttribute("isInternalAndProtected", XmlConvert.ToString(setMethod.IsFamilyAndAssembly))); if (leastRestrictiveAccessModifier > C_PRIVATE && setMethod.IsPrivate) setElem.Add(new XAttribute("isPrivate", XmlConvert.ToString(setMethod.IsPrivate))); ret.Add(setElem); } if (getMethod != null) { var getElem = new XElement("get"); if (leastRestrictiveAccessModifier > C_INTERNAL_OR_PROTECTED && getMethod.IsFamilyOrAssembly) getElem.Add(new XAttribute("isInternalOrProtected", XmlConvert.ToString(getMethod.IsFamilyOrAssembly))); if (leastRestrictiveAccessModifier > C_INTERNAL && getMethod.IsAssembly) getElem.Add(new XAttribute("isInternal", XmlConvert.ToString(getMethod.IsAssembly))); if (leastRestrictiveAccessModifier > C_PROTECTED && getMethod.IsFamily) getElem.Add(new XAttribute("isProtected", XmlConvert.ToString(getMethod.IsFamily))); if (leastRestrictiveAccessModifier > C_INTERNAL_AND_PROTECTED && getMethod.IsFamilyAndAssembly) getElem.Add(new XAttribute("isInternalAndProtected", XmlConvert.ToString(getMethod.IsFamilyAndAssembly))); if (leastRestrictiveAccessModifier > C_PRIVATE && getMethod.IsPrivate) getElem.Add(new XAttribute("isPrivate", XmlConvert.ToString(getMethod.IsPrivate))); ret.Add(getElem); } if (propInfo.IsSpecialName) ret.Add(new XAttribute("isSpecialName", XmlConvert.ToString(propInfo.IsSpecialName))); context.Element.Add(ret); this.GenerateImplementsElement(context.Clone(ret), propInfo); foreach (IEnricher item in this.Enrichers) item.EnrichProperty(context.Clone(ret), propInfo); return ret; }
private XElement GenerateAssemblyElement(IProcessingContext context, AssetIdentifier assetId) { Assembly asm = (Assembly)context.AssetResolver.Resolve(assetId); var ret = new XElement("assembly", new XAttribute("name", asm.GetName().Name), new XAttribute("filename", asm.ManifestModule.Name), new XAttribute("assetId", assetId), new XAttribute("phase", context.Phase), asm.GetReferencedAssemblies() .Select(an => { // hack - assume if we get here it is ok if we cannot load. // examples are ilmerge assemblies try { return Assembly.ReflectionOnlyLoad(an.FullName); } catch { return null; } }).Where(a => a != null) .Select(asm2 => new XElement("references", new XAttribute("assembly", AssetIdentifier. FromAssembly(asm2))))); context.Element.Add(ret); foreach (IEnricher enricher in this._enrichers) enricher.EnrichAssembly(context.Clone(ret), asm); return ret; }