扩展方法必须在非通用静态类中定义错误public static

时间:2019-12-25 17:57:31

标签: c#

我的项目有问题。

我有此代码:

public static TitleInfoPacket GenerateTitInfo(this ICharacterDAO visualEntity)
{
    var visibleTitle = visualEntity.Titles.FirstOrDefault(s => s.Visible)?.TitleType;
    var effectiveTitle = visualEntity.Titles.FirstOrDefault(s => s.Active)?.TitleType;

    return new TitleInfoPacket
        {
            VisualId = visualEntity.VisualId,
            EffectiveTitle = effectiveTitle ?? 0,
            VisualType = visualEntity.VisualType,
            VisibleTitle = visibleTitle ?? 0,
        };
}

public TitlePacket GenerateTitle(this ICharacterDAO visualEntity)
{
    var data = visualEntity.Titles.Select(s => new TitleSubPacket
        {
            TitleId = (short)(s.TitleType - 9300),
            TitleStatus = (byte)((s.Visible ? 2 : 0) + (s.Active ? 4 : 0) + 1)
        }).ToList();

    return new TitlePacket
           {
                Data = data.Any() ? data : null
           };
}

我得到一个错误:

  

扩展方法必须在非通用静态类中定义

我尝试了一切,但仍然遇到这个问题,有人可以向我解释我做的不好还是告诉我一些我想念的东西,谢谢大家的答复

1 个答案:

答案 0 :(得分:1)

观察到我放置了静态关键字

public static class MyExtensionsClass {

   // extension methods here
    public static TitleInfoPacket GenerateTitInfo(this ICharacterDAO 

 visualEntity)
    {
      var visibleTitle = visualEntity.Titles.FirstOrDefault(s => s.Visible)?.TitleType;
      var effectiveTitle = visualEntity.Titles.FirstOrDefault(s => s.Active)?.TitleType;
return new TitleInfoPacket
    {
        VisualId = visualEntity.VisualId,
        EffectiveTitle = effectiveTitle ?? 0,
        VisualType = visualEntity.VisualType,
        VisibleTitle = visibleTitle ?? 0,
    };
  }
}

希望这会有所帮助!