实现扩展方法的最佳方式

发布于 2024-10-04 02:48:33 字数 214 浏览 4 评论 0原文

我对这一切真的真的很陌生,而且其中大部分都是未经探索的领域。今天我需要创建一个匿名类并将其放入列表中。我试图找到如何创建匿名类型列表,并发现我应该创建一个扩展方法。我还已经发现扩展方法应该位于静态类中。但我还没有弄清楚是否应该使用某种模式?现在,我在 App_Code 文件夹中创建了一个名为 ExtensionMethods 的静态类,但不知道是否应该将各种类型的扩展方法放入此类中,或者是否应该创建单独的类等。

I am really really really new to all of this, and most of it is unexplored territory. Today I needed to create an anonymous class and put it to a list. I was trying to find how I can make a list of anonymous types, and found that I should make an extension method. I also already figured out an extension method should be in a static class. But what I haven't figured out yet is if there is some pattern that I should use? For now, I have made a static class in my App_Code folder named ExtensionMethods, but have no idea if I should put extension methods of all kinds of types in this class, or if I should make separate classes etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陪你搞怪i 2024-10-11 02:48:33

据我所知,您无法实现匿名类的扩展方法。如果该类具有一些语义,那么它应该成为一个命名类,这确实是有意义的。
要创建匿名类的列表,请使用此方法:

public static List<T> CreateListFromType<T>(T anonType){
   return new List<T>();
}

要使用此方法创建列表,请执行以下操作:

var list = CreateListFromType(new {A = default(int), B = default(int)});

To my knowledge you can not implement extension methods for anonymous classes. And this makes sense as really and truly if the class has some semantics it should be made a named class.
To create a list of anonymous classes use this method:

public static List<T> CreateListFromType<T>(T anonType){
   return new List<T>();
}

To use this method to create a list, do something like:

var list = CreateListFromType(new {A = default(int), B = default(int)});
英雄似剑 2024-10-11 02:48:33

扩展方法只不过是更容易阅读静态帮助器/实用方法。组织它们的方式与组织普通课程的原则相同。如果您认为这些应该放在一起,那么尝试给它们一个有意义的、足够通用的名称作为类名。一旦你发现你的类名不能包含该方法所做的事情,那么你就知道该方法应该属于其他地方。

Extension method is nothing more than easier-to-read static helper/utility methods. The way you organize them is the same principal as how you organize your normal classes. If you think those should stays together, then try to give them a meaningful, general enough name as the class name. Once you found your class name cannot include what that method doing, then you know that method should belongs to other places.

像你 2024-10-11 02:48:33

首先,扩展方法是像这样声明的普通静态方法 -

void MyExtension(this MyTarget target, int myparam){ ..

之后,C# 编译器添加一些语法糖,让您调用此方法,因为

target.MyExtension(myparam);

编译器将替换所有这些调用。

MyStaticClass.MyExtension(target, myparam);

重要的是,扩展方法是普通静态方法。创建它们时应遵循以下准则 -

  1. 尝试将每个目标类的扩展方法分组到单独的静态类中,并将其适当命名为扩展。

  2. 在应用程序中为扩展方法(例如 MyAppExtensions)创建一个新的命名空间,并将静态类保留在该命名空间内。这将使编码人员不会将它们误解为扩展方法并意外地将它们用作实例方法。

  3. 将命名空间和扩展方法的静态类的这些命名约定作为团队中的标准。

  4. 在您的扩展方法中检查第一个参数是否为空并采取适当的操作。如果不这样做,则可能会调用目标为 null 的方法,并可能导致意外行为。如果允许第一个参数为 null,则清楚地记录下来。

  5. 有意识地判断创建是否正确,扩展方法或实例方法会更好。

  6. 请注意,扩展方法名称不要与此类的实例方法名称或其他现有扩展方法名称冲突。以下第 1、2 和 3 点将帮助您实现这一目标。

我从 Jon Skeet 的《C# 深度》 中学到了这些内容。你也应该读一下。

Firstly extension methods are normal static methods declared like this -

void MyExtension(this MyTarget target, int myparam){ ..

After this the C# compiler adds some syntactic sugar letting you call this method as

target.MyExtension(myparam);

The compiler will replace all these calls with

MyStaticClass.MyExtension(target, myparam);

The important thing is that an extension method is a normal static method. You should follow following guidelines while creating them -

  1. Try to group extension methods for each target class in a separate static class and name it appropriately as Extensions.

  2. Create a new namespace in your application for Extension methods such as MyAppExtensions and keep your static classes inside this namespace. This will keep the coder from misunderstanding them as Extension method and accidentally using them as instance methods.

  3. Make these naming conventions for namespaces and static classes of Extension methods as a standard in the team.

  4. In your extension method check if the first parameter is null and take appropriate action. If you do not then it will be possible to call the method with target being null and may result in unexpected behavior. If the first parameter is allowed to be null then document it clearly.

  5. Consciously decide if it is correct to create and extension method or instance method will be better.

  6. Be careful that the extension method names do not clash with instance method names or other existing extension method names for this class. Following point 1, 2 and 3 will help you achieve this.

I learnt these from Jon Skeet's C# In Depth. You should read it too.

郁金香雨 2024-10-11 02:48:33

这就是创建匿名类列表的方法。

var anonList = new[]{
    new {Foo = "foo", Bar = 2},
    new { Foo = "bar", Bar = 3},
}.ToList();

它与编写扩展方法无关。

编辑:
事实上,您将很难使用扩展方法来创建匿名类型,因为匿名类型不能用作方法参数或返回类型。

编辑2:
如果要将匿名类型列表转换为特定类型列表,可以使用 Select:

class MyClass
{
    public string Prop1 {get;set;}
    public int Prop2 {get;set;}
}

List<MyClass> myList = anonList.Select(x => new MyClass(){Prop1 = Foo, Prop2 = Bar}).ToList();

This is how you create a list of an anonymous class.

var anonList = new[]{
    new {Foo = "foo", Bar = 2},
    new { Foo = "bar", Bar = 3},
}.ToList();

It has nothing to do with writing extension methods.

Edit:
In fact you will have a hard time to use extension methods to create anonymous types since anonymous types can not be used as method parameters or return type.

Edit2:
If you want to convert a list of anonymous types to a list of specific types you can use Select:

class MyClass
{
    public string Prop1 {get;set;}
    public int Prop2 {get;set;}
}

List<MyClass> myList = anonList.Select(x => new MyClass(){Prop1 = Foo, Prop2 = Bar}).ToList();
素年丶 2024-10-11 02:48:33

我习惯将匿名放入数据表中。由于其局限性,DataTable 提供了更多功能,例如序列化。

I use to put anonymous to DataTable. Due to it's limitation, DataTable provide more functionality such as serialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文