Get programmatic xml doc comments

Visual Studio does this; The reflector does this; and now i want also :)

I want to get XML documentation for some members in some frame assemblies (i.e. mscorlib.dll, System.dlletc.). I assume this will include:

  • search XML file for assembly,
  • Jump to the corresponding named child and
  • extraction of the necessary elements ( <summary>, <remarks>etc.)


Where are the XML files for building frames stored? Any questions about decoding the XMLDOC naming scheme? Are there libraries that will facilitate this process?

+3
source share
6 answers

Find xml

assembly.dll assembly.xml, Framework SDK, Runtime XML . API.

. dll, (, C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\en) - . , .

, "xml-doc ID" . AFAIR # MSDN. . XML.

+5

:

xml

.NET ( GAC )

static FileInfo GetXmlDocFile( Assembly assembly ) {
  string assemblyDirPath = Path.GetDirectoryName( assembly.Location );
  string fileName = Path.GetFileNameWithoutExtension( assembly.Location ) +".xml";

  return GetFallbackDirectories( CultureInfo.CurrentCulture )
    .Select( dirName => CombinePath( assemblyDirPath, dirName, fileName ) )
    .Select( filePath => new FileInfo( filePath ) )
    .Where( file => file.Exists )
    .First( );
}

static IEnumerable<string> GetFallbackDirectories( CultureInfo culture ) {
  return culture
    .Enumerate( c => c.Parent.Name != c.Name ? c.Parent : null )
    .Select( c => c.Name );
}

static IEnumerable<T> Enumerate<T>( this T start, Func<T, T> next ) {
  for( T item = start; !object.Equals( item, default(T) ); item = next( item ) )
    yield return item;
}

static string CombinePath( params string[] args ) {
  return args.Aggregate( Path.Combine );
}

XML

static XElement GetDocMember( XElement docMembers, MemberInfo member ) {
  string memberId = GetMemberId( member );
  return docMembers.Elements( "member" )
    .Where( e => e.Attribute( "name" ).Value == memberId )
    .First( );
}

static string GetMemberId( MemberInfo member ) {
  char memberKindPrefix = GetMemberPrefix( member );
  string memberName = GetMemberFullName( member );
  return memberKindPrefix + ":" + memberName;
}

static char GetMemberPrefix( MemberInfo member ) {
  return member.GetType( ).Name
    .Replace( "Runtime", "" )[0];
}

static string GetMemberFullName( MemberInfo member ) {
  string memberScope = "";
  if( member.DeclaringType != null )
    memberScope = GetMemberFullName( member.DeclaringType );
  else if( member is Type )
    memberScope = ((Type)member).Namespace;

  return memberScope + "." + member.Name;
}

Type type = typeof( string );

var file = GetXmlDocFile( type.Assembly );
var docXml = XDocument.Load( file.FullName );
var docMembers = docXml.Root.Element( "members" );

var member = type.GetProperty( "Length" );
var docMember = GetDocMember( docMembers, member );
+3

Jolt.NET CodePlex , . Jolt.

, doc XML , System.Reflection (.. MethodInfo, PropertyInfo ..).

+2

DocsByReflection.

// From method.
var methodInfo = typeof(Stub).GetMethod("MethodWithGenericParameter");
var methodDoc = DocsService.GetXmlFromMember(methodInfo);
+2

, : NuDoc http://kzu.to/nudoc

mscorlib, , , :

var members = Reader.Read(typeof(string).Assembly);

, .

I use this library to create an API site using markdowns for hosting on GitHub, ideally automatically with every build, and possibly even for a wiki, so that it can be updated and cycle through the code :).

+1
source

The XML file is called exactly the same as the assembly file, with the exception of the other extension "xml" and must be in the same directory as the assembly itself.

Can't help you with two other questions. AFAIK, you yourself ...

0
source

Source: https://habr.com/ru/post/1698653/


All Articles