コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeExampleInfo"/> class.
 /// </summary>
 /// <param name="type">The type of the example.</param>
 /// <param name="sourceCodePath">The source code path where
 /// the example type is defined.</param>
 /// <param name="language">The programming language in which the example
 /// has been coded.</param>
 public CodeExampleInfo(
     Type type,
     string sourceCodePath,
     ProgrammingLanguage language)
 {
     this.Type           = type;
     this.SourceCodePath = sourceCodePath;
     this.Language       = language;
 }
コード例 #2
0
        /// <summary>
        /// Gets the info about the source code file where the
        /// specified type is defined.
        /// </summary>
        /// <param name="exampleType">The example type.</param>
        /// <param name="path">The source code file, if it can be found;
        /// otherwise <b>null</b>.</param>
        /// <param name="language">The language in which the source code is
        /// written, if the source code file can be found;
        /// otherwise <b>null</b>.</param>
        /// <remarks>
        /// <para>
        /// This method assumes the each type is defined in
        /// a C# or VB file named from the type name, and that source code
        /// files are stored in
        /// a directory tree, under the folder returned by
        /// <see cref="CodeBase"/>,
        /// reflecting the project namespaces.
        /// </para>
        /// <para>
        /// It is also assumed that the default namespace
        /// matches the value returned by <see cref="DefaultNamespace"/>.
        /// </para>
        /// </remarks>
        private void GetSourceCodeInfo(
            Type exampleType,
            out string path,
            out ProgrammingLanguage language)
        {
            string codeBase         = this.CodeBase;
            string defaultNamespace = this.DefaultNamespace;

            // fullName contains the namespace in which the type is defined.
            var fullName         = exampleType.FullName;
            int length           = defaultNamespace.Length;
            var nestedNamespaces = fullName.Substring(length + 1);
            var tokens           = nestedNamespaces.Split('.');

            string basePath = codeBase;

            // Here if the type is not defined in the default namespace
            for (int i = 0; i < tokens.Length; i++)
            {
                basePath = Path.Combine(basePath, tokens[i]);
            }

            foreach (var supportedLanguage in SupportedLanguages)
            {
                string sourceCodePath = basePath + supportedLanguage.FileExtension;
                if (File.Exists(sourceCodePath))
                {
                    path     = sourceCodePath;
                    language = supportedLanguage;
                    return;
                }
            }

            path     = null;
            language = null;
        }