部分类VS扩展方法 [英] Partial Class vs Extension Method

查看:384
本文介绍了部分类VS扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用这2种方式来扩展类或创建针对类扩展方法很多经验。通过看别人打工,我这里有一个问题。

I dont have much experience of using these 2 ways to extend a class or create extension methods against a class. By looking others work, I have a question here.

我看到人们使用parital类项目中扩展实体类。同时,在同一个项目中,有含有大量扩展方法实体类另一个文件夹。

I saw people using a parital class to extend an entity class in a project. Meanwhile, in the same project, there is another folder containing a lot extension methods to the entity class.

时,它有权这样做?我的意思是这2种方法都工作得很好。你能给我一些真正的实践中如何挑选一个来自他们,当我想要扩展一个类?

Is it right to do so? I mean these 2 ways both work well. Could you give me some real practice how to pick one from them when I want extend a class?

推荐答案

有些差异即会决定是否要使用部分类或扩展方法是

Some of differences that will determine whether you want to use a Partial Class or an Extension Method are

部分类


  • 仅适用反类在同一个项目/组件

  • 目标类有标记为部分

  • 曾先后获得目标类田野和保护成员

  • 目标必须是一个类实现

  • Only works against classes in the same project/assembly
  • Target class has to be marked as partial
  • Has access to the Target class' fields and protected members
  • Target must be a class implementation

扩展方法


  • 可以对其他组装类

  • 申请必须是静态的,只能访问的目标类公共成员

  • 扩展目标可以是一个具体类型或抽象类型或接口

这篇关于部分类VS扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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