类之间的C#代码共享 [英] C# share code between classes

查看:175
本文介绍了类之间的C#代码共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用C#的Visual Studio 2008,什么是要共享多个类和源文件代码的最佳方式?

In Visual Studio 2008 using C#, what is the best way to share code across multiple classes and source files?

继承是不如类已经有一个有意义的层次结构中的溶液。

Inheritance is not the solution as the classes already have a meaningful hierarchy.

有一些巧妙的功能,就像一个C头文件,它让你插入代码的任何地方,你想在另一个类?

Is there some neat feature that's like a C include file that let's you insert code anywhere you want in another class?

编辑:

好吧,我想我们需要一个具体的例子...

ok, i guess we need a concrete example...

有几个百类具有良好域深思熟虑类层次结构。现在,许多这些类需要打印。有一个处理打印的实用打印机类。比方说,有3个不同的打印方法依赖于正在打印的类。调用打印方法(6线)的代码是什么,我试图避免在所有不同的客户端类的网页复制和粘贴。

There are several hundred classes in the domain with a well thought out class heirarchy. Now, many of these classes need to print. There is a utility printer class that handles the printing. Let's say there are 3 different print methods that are dependent on the class that is being printed. The code that calls the print method (6 lines) is what I'm trying to avoid copying and pasting across all the different client class pages.

这将会是不错的,如果人们不会认为他们知道更多关于域运 - 尤其是当他们特别提到,不适合技术...

It'd be nice if people wouldn't assume they knew more about the domain that the op - especially when they specifically mention techniques that don't fit...

推荐答案

如果您有在表示非常不同的事情类经常使用的,以我的经验功能,应属于逼到几类:

If you have functionality that you use frequently in classes that represent very different things, in my experience that should fall into just a few categories:


  • 实用工具(如字符串格式化,解析,......)

  • 交叉横切关注点(日志,安全执法,...)

有关实用型功能,你应该考虑创建单独的类,并且引用在需要的地方在商业类的实用工具类。

For utility-type functionality you should consider creating separate classes, and referencing the utility classes where needed in the business class.

public class Validator
{
  public bool IsValidName(string name);
}

class Patient
{
  private Validator validator = new Validator();
  public string FirstName
  {
     set
     {
         if (validator.IsValidName(value)) ... else ...
     }
  }
}

有关横切关注点,如日志或安全的,我建议你调查面向方面编程

For cross-cutting concerns such as logging or security, I suggest you investigate Aspect-Oriented Programming.

关于其他意见讨论的PrintA与PrintB例如,它听起来像工厂模式的优秀案例。你定义一个接口如IPRINT,类PrintA和PrintB这两个实施的iPrint,并分配基于什么特定页面需要的iPrint的一个实例。

Regarding the PrintA vs. PrintB example discussed in other comments, it sounds like an excellent case for the Factory Pattern. You define an interface e.g. IPrint, classes PrintA and PrintB that both implement IPrint, and assign an instance of IPrint based on what the particular page needs.

// Simplified example to explain:

public interface IPrint 
{ 
   public void Print(string); 
}

public class PrintA : IPrint
{
   public void Print(string input)
   { ... format as desired for A ... }
}

public class PrintB : IPrint
{
   public void Print(string input)
   { ... format as desired for B ... }
}

class MyPage
{
   IPrint printer;

   public class MyPage(bool usePrintA)
   {
      if (usePrintA) printer = new PrintA(); else printer = new PrintB();
   }

   public PrintThePage()
   {
      printer.Print(thePageText);
   }
}

这篇关于类之间的C#代码共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆