private CodeDocType ConvertToModel(Type type, CodeDocMemberDetailLevel detailLevel, ICodeDocMemberDataProvider extraMemberDataProvider = null) { Contract.Requires(type != null); Contract.Ensures(Contract.Result<CodeDocType>() != null); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocType>().ShortName)); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocType>().FullName)); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocType>().Title)); Contract.Ensures(Contract.Result<CodeDocType>().Title == Contract.Result<CodeDocType>().ShortName); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocType>().SubTitle)); var includeExceptions = detailLevel != CodeDocMemberDetailLevel.Minimum; var appendXmlDoc = detailLevel.HasFlag(CodeDocMemberDetailLevel.Summary) || detailLevel.HasFlag(CodeDocMemberDetailLevel.AdditionalContents); var provideXmlDoc = includeExceptions || detailLevel != CodeDocMemberDetailLevel.Minimum; var includeMembers = detailLevel.HasFlag(CodeDocMemberDetailLevel.Members); var includeInheritance = detailLevel.HasFlag(CodeDocMemberDetailLevel.Inheritance); var includeParameters = detailLevel != CodeDocMemberDetailLevel.Minimum; var cRef = GetCRefIdentifier(type); var model = type.IsDelegateType() ? new CodeDocDelegate(cRef) : new CodeDocType(cRef); model.Uri = GetUri(type); var delegateModel = model as CodeDocDelegate; var memberDataProvider = new CodeDocMemberInfoProvider<Type>(type); XmlDocMember xmlDocs = null; if (provideXmlDoc) { xmlDocs = XmlDocs.GetMember(cRef.FullCRef); if (xmlDocs != null) memberDataProvider.Add(new CodeDocMemberXmlDataProvider(xmlDocs)); } if (extraMemberDataProvider != null) memberDataProvider.Add(extraMemberDataProvider); if (includeInheritance && type.BaseType != null) { if (ImplicitlyInheritDataProvider(type.BaseType) || (xmlDocs != null && xmlDocs.HasInheritDoc)) { var baseModel = GetOrConvert(type.BaseType, detailLevel); memberDataProvider.Add(new CodeDocMemberDataProvider(baseModel)); } } if (appendXmlDoc) ApplyContentXmlDocs(model, memberDataProvider); model.ExternalVisibility = memberDataProvider.ExternalVisibility ?? ExternalVisibilityKind.Public; model.ShortName = RegularTypeDisplayNameOverlay.GetDisplayName(type); model.FullName = FullTypeDisplayNameOverlay.GetDisplayName(type); model.Title = model.ShortName; model.NamespaceName = type.Namespace; if (type.IsEnum) model.SubTitle = "Enumeration"; else if (type.IsValueType) model.SubTitle = "Structure"; else if (type.IsInterface) model.SubTitle = "Interface"; else if (type.IsDelegateType()) model.SubTitle = "Delegate"; else model.SubTitle = "Class"; model.Namespace = GetOrCreateNamespaceByName(type.Namespace); model.Assembly = GetCodeDocAssembly(type.Assembly); model.IsEnum = type.IsEnum; model.IsFlagsEnum = type.IsEnum && type.HasAttribute(typeof(FlagsAttribute)); model.IsSealed = type.IsSealed; model.IsValueType = type.IsValueType; model.IsStatic = memberDataProvider.IsStatic.GetValueOrDefault(); model.IsObsolete = memberDataProvider.IsObsolete.GetValueOrDefault(); if (type.DeclaringType != null) model.DeclaringType = GetOrConvert(type.DeclaringType, CodeDocMemberDetailLevel.Minimum); if (delegateModel != null) delegateModel.IsPure = memberDataProvider.IsPure.GetValueOrDefault(); if (includeInheritance && !type.IsInterface) { var currentBase = type.BaseType; if (null != currentBase) { var baseChain = new List<CodeDocType>() { GetOrConvert(currentBase, detailLevel) }; currentBase = currentBase.BaseType; while (currentBase != null) { baseChain.Add(GetOrConvert(currentBase, CodeDocMemberDetailLevel.Minimum)); currentBase = currentBase.BaseType; } model.BaseChain = baseChain; } var implementedInterfaces = type.GetInterfaces(); if (implementedInterfaces.Length > 0) { model.Interfaces = Array.ConvertAll( implementedInterfaces, t => GetOrConvert(t, CodeDocMemberDetailLevel.Minimum)); } } if (includeParameters && type.IsGenericType) { var genericDefinition = type.IsGenericTypeDefinition ? type : type.GetGenericTypeDefinition(); if (genericDefinition != null) { var genericArguments = genericDefinition.GetGenericArguments(); if (type.IsNested && genericArguments.Length > 0) { Contract.Assume(type.DeclaringType != null); var parentGenericArguments = type.DeclaringType.GetGenericArguments(); genericArguments = genericArguments .Where(a => parentGenericArguments.All(p => p.Name != a.Name)) .ToArray(); } if (genericArguments.Length > 0) { model.GenericParameters = CreateGenericTypeParameters( genericArguments, memberDataProvider); } } } if (delegateModel != null) { if (includeParameters) { delegateModel.Parameters = Array.ConvertAll( type.GetDelegateTypeParameters(), p => CreateArgument(p, memberDataProvider)); var returnParameter = type.GetDelegateReturnParameter(); if (returnParameter != null && returnParameter.ParameterType != typeof (void)) delegateModel.Return = CreateReturn(returnParameter, memberDataProvider); } if (includeExceptions) { if (memberDataProvider.HasExceptions) delegateModel.Exceptions = CreateExceptionModels(memberDataProvider.GetExceptions()).ToArray(); if (memberDataProvider.HasEnsures) delegateModel.Ensures = memberDataProvider.GetEnsures().ToArray(); if (memberDataProvider.HasRequires) delegateModel.Requires = memberDataProvider.GetRequires().ToArray(); } } if (includeMembers) { var nestedTypeModels = new List<ICodeDocMember>(); var nestedDelegateModels = new List<ICodeDocMember>(); foreach (var nestedType in type.GetAllNestedTypes().Where(ReflectionRepository.MemberFilter)) { var nestedTypeModel = GetOrConvert(nestedType, CodeDocMemberDetailLevel.QuickSummary); if (nestedType.IsDelegateType()) nestedDelegateModels.Add(nestedTypeModel); else nestedTypeModels.Add(nestedTypeModel); } model.NestedTypes = nestedTypeModels; model.NestedDelegates = nestedDelegateModels; var methodModels = new List<ICodeDocMember>(); var operatorModels = new List<ICodeDocMember>(); foreach (var methodInfo in type.GetAllMethods().Where(ReflectionRepository.MemberFilter)) { var methodModel = GetOrConvert(methodInfo, CodeDocMemberDetailLevel.QuickSummary); if (methodInfo.IsOperatorOverload()) operatorModels.Add(methodModel); else methodModels.Add(methodModel); } model.Methods = methodModels; model.Operators = operatorModels; model.Constructors = type .GetAllConstructors() .Where(ReflectionRepository.MemberFilter) .Select(methodInfo => GetOrConvert(methodInfo, CodeDocMemberDetailLevel.QuickSummary)) .ToArray(); model.Properties = type .GetAllProperties() .Where(ReflectionRepository.MemberFilter) .Select(propertyInfo => GetOrConvert(propertyInfo, CodeDocMemberDetailLevel.QuickSummary)) .ToArray(); model.Fields = type .GetAllFields() .Where(ReflectionRepository.MemberFilter) .Select(fieldInfo => GetOrConvert(fieldInfo, CodeDocMemberDetailLevel.QuickSummary)) .ToArray(); model.Events = type .GetAllEvents() .Where(ReflectionRepository.MemberFilter) .Select(eventInfo => GetOrConvert(eventInfo, CodeDocMemberDetailLevel.QuickSummary)) .ToArray(); } return model; }
private CodeDocMethod ConvertToModel(MethodBase methodBase, CodeDocMemberDetailLevel detailLevel, ICodeDocMemberDataProvider extraMemberDataProvider = null) { Contract.Requires(methodBase != null); Contract.Ensures(Contract.Result<CodeDocMethod>() != null); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocMethod>().ShortName)); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocMethod>().FullName)); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocMethod>().Title)); Contract.Ensures(Contract.Result<CodeDocMethod>().Title == Contract.Result<CodeDocMethod>().ShortName); Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<CodeDocMethod>().SubTitle)); var includeExceptions = detailLevel != CodeDocMemberDetailLevel.Minimum; var appendXmlDoc = detailLevel.HasFlag(CodeDocMemberDetailLevel.Summary) || detailLevel.HasFlag(CodeDocMemberDetailLevel.AdditionalContents); var provideXmlDoc = includeExceptions || detailLevel != CodeDocMemberDetailLevel.Minimum; var includeInheritance = detailLevel.HasFlag(CodeDocMemberDetailLevel.Inheritance); var includeParameters = detailLevel != CodeDocMemberDetailLevel.Minimum; var methodInfo = methodBase as MethodInfo; var methodCRef = GetCRefIdentifier(methodBase); var model = new CodeDocMethod(methodCRef); model.Uri = GetUri(methodBase); var memberDataProvider = new CodeDocMemberInfoProvider<MethodBase>(methodBase); if (provideXmlDoc) { var xmlDocs = XmlDocs.GetMember(methodCRef.FullCRef); if (xmlDocs != null) memberDataProvider.Add(new CodeDocMemberXmlDataProvider(xmlDocs)); } if (extraMemberDataProvider != null) memberDataProvider.Add(extraMemberDataProvider); if (includeInheritance && methodInfo != null) { var baseDefinition = methodInfo.FindNextAncestor(); if (baseDefinition != null) { var baseDefinitionModel = GetOnly(GetCRefIdentifier(baseDefinition), detailLevel); if (baseDefinitionModel != null) memberDataProvider.Add(new CodeDocMemberDataProvider(baseDefinitionModel)); } } if (appendXmlDoc) ApplyContentXmlDocs(model, memberDataProvider); model.ExternalVisibility = memberDataProvider.ExternalVisibility ?? ExternalVisibilityKind.Public; model.ShortName = RegularTypeDisplayNameOverlay.GetDisplayName(methodBase); model.FullName = FullTypeDisplayNameOverlay.GetDisplayName(methodBase); model.Title = model.ShortName; Contract.Assume(methodBase.DeclaringType != null); model.NamespaceName = methodBase.DeclaringType.Namespace; if (includeParameters) { model.Parameters = Array.ConvertAll( methodBase.GetParameters(), p => CreateArgument(p, memberDataProvider)); if (methodInfo != null && methodInfo.ReturnParameter != null && methodInfo.ReturnType != typeof (void)) model.Return = CreateReturn(methodInfo.ReturnParameter, memberDataProvider); } if (methodBase.IsConstructor) model.SubTitle = "Constructor"; else if (model.Parameters != null && model.Parameters.Count == 1 && model.HasReturn && CSharpOperatorNameSymbolMap.IsConversionOperatorMethodName(methodBase.Name)) { model.SubTitle = "Conversion"; string conversionOperationName; if (methodBase.Name.EndsWith("Explicit", StringComparison.OrdinalIgnoreCase)) conversionOperationName = "Explicit"; else if (methodBase.Name.EndsWith("Implicit", StringComparison.OrdinalIgnoreCase)) conversionOperationName = "Implicit"; else conversionOperationName = String.Empty; var conversionParameterPart = String.Concat( model.Parameters[0].ParameterType.ShortName, " to ", model.Return.ParameterType.ShortName); model.ShortName = String.IsNullOrEmpty(conversionOperationName) ? conversionParameterPart : String.Concat(conversionOperationName, ' ', conversionParameterPart); model.Title = model.ShortName; } else if (methodBase.IsOperatorOverload()) model.SubTitle = "Operator"; else model.SubTitle = "Method"; Contract.Assume(methodBase.DeclaringType != null); model.Namespace = GetOrCreateNamespaceByName(methodBase.DeclaringType.Namespace); model.Assembly = GetCodeDocAssembly(methodBase.DeclaringType.Assembly); model.DeclaringType = GetOrConvert(methodBase.DeclaringType, CodeDocMemberDetailLevel.Minimum); model.IsOperatorOverload = methodBase.IsOperatorOverload(); model.IsExtensionMethod = methodBase.IsExtensionMethod(); model.IsSealed = methodBase.IsSealed(); model.IsStatic = memberDataProvider.IsStatic.GetValueOrDefault(); model.IsObsolete = memberDataProvider.IsObsolete.GetValueOrDefault(); model.IsPure = memberDataProvider.IsPure.GetValueOrDefault(); if (methodBase.DeclaringType != null && !methodBase.DeclaringType.IsInterface) { if (methodBase.IsAbstract) model.IsAbstract = true; else if (methodBase.IsVirtual && !methodBase.IsFinal && methodBase.Attributes.HasFlag(MethodAttributes.NewSlot)) model.IsVirtual = true; } if (includeExceptions){ if (memberDataProvider.HasExceptions) model.Exceptions = CreateExceptionModels(memberDataProvider.GetExceptions()).ToArray(); if (memberDataProvider.HasEnsures) model.Ensures = memberDataProvider.GetEnsures().ToArray(); if (memberDataProvider.HasRequires) model.Requires = memberDataProvider.GetRequires().ToArray(); } if (includeParameters && methodBase.IsGenericMethod) { var genericDefinition = methodBase.IsGenericMethodDefinition ? methodBase : methodInfo == null ? null : methodInfo.GetGenericMethodDefinition(); if (genericDefinition != null) { var genericArguments = genericDefinition.GetGenericArguments(); if (genericArguments.Length > 0) { model.GenericParameters = CreateGenericTypeParameters( genericArguments, memberDataProvider); } } } return model; }