简体   繁体   English

C# 添加到另一个 DLL 中的 class

[英]C# add to class in another DLL

...is it possible? ...可能吗?

For example, can I add this simple function...例如,我可以添加这个简单的 function...

public static double Deg2Rad(double degrees) {
    return Math.PI / 180 * degrees;
}

...to the Convert class? ... Convert为 class? So (using the default..."usings") you can call所以(使用默认的...“usings”)你可以调用

double radians = Convert.Deg2Rad(123);

Can this be done?这可以做到吗? If so, how?如果是这样,怎么做?

No, it's not possible.不,这是不可能的。 You could use extensions if Convert weren't static , but no.如果Convert不是static ,您可以使用扩展,但不是。

You could add an extension to double though, using the this keyword:不过,您可以使用this关键字为double添加扩展名:

public static double Deg2Rad(this double degrees) {
    return Math.PI / 180 * degrees;
}

And use it like so:并像这样使用它:

double radians = (123D).Deg2Rad();

You'll have to put your method in a static class for it to work, though.不过,您必须将您的方法放入static class 才能正常工作。

You can sort of get what you want.你可以得到你想要的。 C# has "Extension Methods". C# 有“扩展方法”。 This allow you to add methods to another class, even if that class is in another assembly that you do not have the source code for.这允许您将方法添加到另一个 class,即使该 class 位于另一个您没有源代码的程序集中。 However, you cannot add static functions to another class.但是,您不能将 static 函数添加到另一个 class。 You can only add instance methods.您只能添加实例方法。

For more information, see MSDN Extensions Methods .有关详细信息,请参阅MSDN 扩展方法

No you can't, but you can add an Extension method to double and call it like不,你不能,但你可以添加一个扩展方法来double并像这样调用它

double x = 180.0;
double y = Math.Cos( x.Deg2Rad() );
double z = Math.Sin( 0.5.Pi() );

in an static class add the following extension method:在 static class 添加以下扩展方法:

public static class ExtensionMethods
{
    public static double Deg2Rad(this double angle)
    {
        return Math.PI*angle/180.0;
    }

    public static double Pi(this double x)
    {
        return Math.PI*x;
    }
}

No you can't, but you don't really need to - you can just declare your own static class instead, for example:不,你不能,但你真的不需要 - 你可以声明你自己的 static class 代替,例如:

public static class MyConvert
{
    public static double Deg2Rad(double degrees) 
    {
        return Math.PI / 180 * degrees;
    }
}

Usage:用法:

double radians = MyConvert.Deg2Rad(123);

(Obviously MyConvert is a rubbish name, but you get the idea). (显然MyConvert是一个垃圾名称,但你明白了)。

The only difference between the above and having the method on the system Convert class is that if its on the Convert class it looks like a built-in function, but you are going to struggle to convince me thats actually a good thing (I like to know when I'm calling framework code vs code maintained internally).上述方法与在系统Convert class 上使用该方法之间的唯一区别是,如果它在Convert class 上,它看起来像一个内置的 function,但实际上你会努力说服我知道我何时调用框架代码与内部维护的代码)。

Apart from what has been said here, there is also another option which is Partial classes (this won't apply to Convert).除了这里所说的之外,还有另一个选项是部分类(这不适用于转换)。

If the class is declared partial, then you can add menthods via another partial class, even if it's in another assembly (DLL).如果 class 被声明为部分,那么您可以通过另一个部分 class 添加方法,即使它位于另一个程序集 (DLL) 中。

But, again, the class has to be originally declared partial for that to work.但是,同样,class 必须最初声明为部分才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM