c# - 在 SandcaSTLes 的单独文件中包含代码示例

标签 c# .net chm sandcastle

我正在尝试在使用 SandcaSTLes 生成的 CHM 文件中包含代码示例。这是我目前所拥有的:

/// <summary>
/// Lorem ipsum.
/// </summary>
/// <example>
/// <code>
/// public int Test
/// {
/// private int test { get; set }
/// private int test2 { get; set; }
/// public int findE()
/// {
/// if(test == test2){
/// return Console.WriteLine("Variables are Equal")
/// }
/// else{
/// return Console.WriteLine("Variables are not equal")
/// }
/// }
/// }
/// </code>
/// </example>

这太乱了,我不想在我的 .cs 文件中包含这段代码。有没有办法将此示例代码放在单独的 .cs 文件中,并以某种方式在评论中引用它?所以相反,我会有这样的东西:

/// <summary>
/// Lorem ipsum.
/// </summary>
/// <reference src="mycode.cs">
/// </example>

最佳答案

当然。假设您创建了一个外部代码文件 Example.cs。在那个文件中,你放置了你的例子,周围是一个命名的 region

#region example1
Console.WriteLine("Hello, World!");
#endregion

在您为其创建示例的元素的 XML 注释中,您指定外部代码示例的来源:

/// <summary>blah blah my class</summary>
/// <example><code source="Example.cs" region="example1" lang="C#" 
/// title="Some Title to Display" /></example>
public class Hurr
{
 // ...

C# 不需要指定语言,因为这是默认语言,如果您不指定区域,它将包含示例文件中的所有代码。

另请注意,源文件路径是相对于您的 SHFB 项目文件的,不一定是 Visual Studio 项目文件。如果您配置代码块组件(在 SHFB -> 项目属性 -> 组件中),您可以指定不同的基本路径。

关于c# - 在 SandcaSTLes 的单独文件中包含代码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021136/

相关文章:

c# - MVVM - 模型或 ViewModel 中的 PropertyChanged?

c# - 列表中的枚举值

c# - 不同类中的相同变量名,但一个可以为空

c# - 远程调试托管应用程序

c# - 如何设置 Canvas.ZIndex 在控件之间绘制黑色面板?

chm - 无法打开 .chm 文件,出现 "page cannot be displayed"错误

c# - 有什么方法可以在 SHFB 中导入外部项目或主题文件?

c# - 静态方法线程安全吗

c# - 在 C# 中向 UI 动态添加元素

windows - 在一次 HH session 中打开联机帮助