C# 在控制器内部使用指令

C# 在控制器内部使用指令,c#,asp.net,asp.net-mvc,controller,using-directives,C#,Asp.net,Asp.net Mvc,Controller,Using Directives,如何避免在所有控制器中重复使用10+指令 每个控制器中大约有10多个使用指令,因为它们引用了我们公司使用的核心框架功能。我知道你会说逻辑应该分开,所以我不再需要它们了,但这不是一个选项 所以说清楚,我说的是: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data.Entity; using System.Linq; using System.Threading.

如何避免在所有控制器中重复使用10+指令

每个控制器中大约有10多个使用指令,因为它们引用了我们公司使用的核心框架功能。我知道你会说逻辑应该分开,所以我不再需要它们了,但这不是一个选项

所以说清楚,我说的是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using AutoMapper.QueryableExtensions;
using Kendo.Mvc.UI;

using语句确保即使在调用对象上的方法时发生异常,也会调用Dispose。 通过将对象放在try块中,然后在finally块中调用Dispose,可以获得相同的结果;事实上,编译器就是这样翻译using语句的

using (Font font1 = new Font("Arial", 10.0f)) 
{
    byte charset = font1.GdiCharSet;
}
编译程序翻译

{
  Font font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet; 
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}
因此,在本例中,您可以在单个块中添加添加对象初始化,并在finally块中处理所有对象,请参见下面的示例

{
  Font font1 = new Font("Arial", 10.0f);
 Font font2 = new Font("Arial", 10.0f);
 Font font3 = new Font("Arial", 10.0f);
 Font font4 = new Font("Arial", 10.0f);
 Font font5 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet; 
  }
  finally
  {

      ((IDisposable)font1).Dispose();

      ((IDisposable)font2).Dispose();

      ((IDisposable)font3).Dispose();

      ((IDisposable)font4).Dispose();

      ((IDisposable)font5).Dispose();
  }
}

你忘了解释你指的是哪10+个using语句,以及为什么在所有控制器中重复它们。也许你的意思是代替?夜猫子888,这确实是我的意思。很抱歉,我的意思是使用指令,而不是使用语句。我已经编辑了我的帖子。谢谢你的努力。