简体   繁体   English

如何为 Microsoft.AppCenter.Crashes 创建扩展方法

[英]How can I create extension method for Microsoft.AppCenter.Crashes

I have this code to track errors but only for live我有这个代码来跟踪错误,但仅限于实时

Crashes.TrackError(ex);

I don't want to be sending crashes on development so I have this我不想在开发过程中发送崩溃,所以我有这个

if (!Debugger.IsAttached) Crashes.TrackError(ex);

but the if condition is redundant so I wanted to create an extension method like但是 if 条件是多余的,所以我想创建一个扩展方法,如

Crashes.TrackErrorLive(ex);

I tried the following code but it's not working... I've also read that extension method requires an instance我尝试了以下代码但它不起作用......我也读过扩展方法需要一个实例

public static void TrackErrorLive(this Crashes crashes, Exception ex)
        {
            if (!Debugger.IsAttached) Crashes.TrackError(ex);
        }

You can not create an extension method for static class.您不能为静态类创建扩展方法。 But you can create your own static class with method needed:但是您可以使用所需的方法创建自己的静态类:

public static class CrashesExtensions
{
    public static void TrackErrorLive(Exception ex)
    {
        if (!Debugger.IsAttached) Crashes.TrackError(ex);
    }
}

and then use it:然后使用它:

CrashesExtensions.TrackErrorLive(ex);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM