Adding a new additional method to existing NuGet package.

huangapple go评论64阅读模式
英文:

Adding a new additional method to existing nuget package

问题

Sure, here's the translation:

我想问一下,是否可以为现有的NuGet包(在我这里是Trimble Connect NuGet与Api Connection)编写扩展方法?我想要添加筛选选项,但是这个NuGet包没有提供类似筛选的方法...

在网上寻找答案,但没有找到。

英文:

I'd like to ask whether is it possible to write a extension method for existing nuget package (In my case it is Trimble Connect nuget with Api Connection) ? I'd like to do filetring option, but this nugetdoesnt offer a method like filtering...

Was looking for answer in web but not found.

答案1

得分: 1

您可以为nuget中的类型编写扩展方法,如果这是您要实现的目标-是的,基本上这就是扩展方法的主要目标:

> 扩展方法允许您在不创建新派生类型、重新编译或以其他方式修改原始类型的情况下“添加”方法到现有类型中。

因此,类型的“所有权”并不重要-它可以是您自己库/项目中的类型,也可以是来自第三方nuget的类型,甚至可以是内置类型,如string(来自之前链接文档的示例):

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this string str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}
using ExtensionMethods;

string s = "Hello Extension Methods";
int i = s.WordCount();

您可以为nuget中适用的类型定义扩展方法。请注意,扩展方法可以做的事情受到它与之交互的类的API的限制。

如果问题是-"我能创建能理解我想让它执行任意操作的库吗",那么答案是:这取决于库的可扩展性设计以及它具有哪些可扩展性点。相当不错的示例(但可能还不是理想的)是诸如ASP.NET Core和EF Core之类的框架,它们允许做很多事情。

英文:

You can write an extension method to a type in nuget, if that is what you are trying to achieve - then yes, basically that is the main goal of extension methods:

> Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

So the "ownership" of the type does not matter - it can be a type from your own library/project or from 3rd party nuget, or even one of the build-in types like string (example from the linked earlier docs):

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this string str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}
using ExtensionMethods;

string s = "Hello Extension Methods";
int i = s.WordCount();

And you can define an extension method for applicable types from the nuget. Note that what the extension method can do is limited by the
API of the class it interacts with.

If the question is - "can I make library to understand the arbitrary thing I want it to do", then answer would be: it depends on how extensible the library was designed and what extensibility points it has. Quite good examples (but maybe still not ideal) of extensibility would be the frameworks like ASP.NET Core and EF Core, which allows to do quite a lot.

huangapple
  • 本文由 发表于 2023年7月6日 16:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定