> This page is for SDK, version Current (default).
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# 添加自定义代码

> 使用 Fern 为你的 .NET SDK 添加自定义逻辑和方法。扩展 BaseClient，创建自定义辅助函数，并有效使用 .fernignore。

本页面介绍如何为你的 .NET SDK 添加自定义逻辑和方法。

> **Note**
>
> These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use [Replay](/learn/sdks/overview/custom-code#replay) instead.

## 添加自定义逻辑

开始添加自定义代码：

#### 创建一个新文件并添加你的自定义逻辑

**`src/YourNamespace/Helper.cs`**

```csharp title="src/YourNamespace/Helper.cs"
namespace YourNamespace;

public static class Helper
{
    public static void MyHelper()
    {
        Console.WriteLine("Hello World!");
    }
}
```

#### 将你的文件添加到 \`.fernignore\`

**`.fernignore`**

```yaml {3} title=".fernignore"
# Specify files that shouldn't be modified by Fern

src/YourNamespace/Helper.cs
```

#### 使用辅助函数

Now your users can consume the helper function by importing it from the SDK.

```csharp
using YourNamespace;

Helper.MyHelper();
```

## 添加自定义 SDK 方法

Fern also allows you to add custom methods to the SDK itself (e.g.
`client.my_method()` ) by inheriting the Fern generated client and then
extending it.

#### 更新 \`generators.yml\` 配置

将你的 Fern 生成的客户端命名为类似 `BaseClient` 的名称，以反映该客户端将被扩展。

**`generators.yml`**

```yaml {4} title="generators.yml"
- name: fern-csharp-sdk
  version: 2.92.8
  config:
    client-class-name: BaseClient
```

#### 导入并扩展生成的客户端

首先，导入 Fern 生成的基础客户端并扩展它。然后，添加你需要的任何方法。

**`src/YourNamespace/MyClient.cs`**

```csharp title="src/YourNamespace/MyClient.cs"
namespace YourNamespace;

public class MyClient : BaseClient
{
    public MyClient(ClientOptions? clientOptions = null) : base(clientOptions)
    {
    }

    public void MyHelper()
    {
        Console.WriteLine("Hello World!");
    }
}
```

#### 更新 \`.fernignore\`

将 `MyClient.cs` 添加到 `.fernignore`。

**`.fernignore`**

```diff title=".fernignore"
+ src/YourNamespace/MyClient.cs
```

#### 使用方法

用户将构造扩展的客户端，而不是生成的客户端。

```csharp
using YourNamespace;

var client = new MyClient();
```

Now your users can consume the helper function by importing it from the SDK.

```csharp
client.MyHelper();
```