如何使用系统命名空间创建扩展方法

时间:2021-08-29 23:59:49

I have 2 class libraries (C#), Say A and B.

我有2个类库(C#),说A和B.

Library A has some methods in it. These methods need and an extension method to cipher/decipher their values.

图书馆A中有一些方法。这些方法需要和扩展方法来加密/解密它们的值。

Library B does this job. This library has an extension method but extending it in System namespace.

图书馆B完成这项工作。此库具有扩展方法,但在System命名空间中扩展它。

namespace System
{
    public static class EncryptDecrypt  
    {
        public static string ToEncrypt(this Object toEncrypt)
        {

        }

        public static string ToDecrypt(this string toDecrypt)
        {

        }
   }
}

The reason I had it in System namespace was to use this extension throughout all application without needing to import in using section.

我在System命名空间中使用它的原因是在所有应用程序中使用此扩展而无需导入使用部分。

Please note

请注意

Library B will always be used as .dll reference because it has a private key (a secret key) for cipher/decipher. So wherever I will use this library; I would have a public key to get things encrypted.

库B将始终用作.dll参考,因为它具有用于密码/解密的私钥(密钥)。所以无论我在哪里使用这个库;我会有一个公钥来加密。

I had setup it all and when I try it; It gives me compile time error for every extension wherever I am using it.

我设置了所有这些,当我尝试它时;无论我在哪里使用它,它都会为每个扩展提供编译时错误。

So If I am using it on a string item.Member_FirstName.ToEncrypt()

所以,如果我在字符串item.Member_FirstName.ToEncrypt()上使用它

'string' does not contain a definition for 'ToEncrypt' and no extension method 'ToEncrypt' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

'string'不包含'ToEncrypt'的定义,并且没有扩展方法'ToEncrypt'可以找到接受类型'string'的第一个参数(你是否缺少using指令或汇编引用?)

But when I go to the definition of these extension methods. These are going on correct definition of theirs in Library **B.**

但是当我去定义这些扩展方法时。这些正在图书馆** B中正确定义他们的。**

I suspect there are conflicts with Library A's System namespace and B's System namespace?

我怀疑库A的System命名空间和B的System命名空间存在冲突?

Please correct me.

请指正。

1 个解决方案

#1


3  

The answer can be found here...

答案可以在这里找到......

https://msdn.microsoft.com/en-us/library/ms229026(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms229026(v=vs.110).aspx

Core namespaces include all System namespaces, excluding namespaces of the application models and the Infrastructure namespaces. Core namespaces include, among others, System, System.IO, System.Xml, and System.Net.

核心命名空间包括所有系统命名空间,不包括应用程序模型的命名空间和基础结构命名空间。核心命名空间包括System,System.IO,System.Xml和System.Net等。

X DO NOT give types names that would conflict with any type in the Core namespaces.

X不要提供与Core名称空间中的任何类型冲突的类型名称。

The caps are all there's I think they really really don't want you to do this. Why ? Because how would anyone be able to trust the .NET framework if there were 3rd party assemblies flying round, throwing in their own methods and classes in among the namespaces for the core framework ?

我认为他们真的不希望你这样做。为什么?因为如果第三方程序集飞来飞去,如何在核心框架的名称空间中引入自己的方法和类,那么任何人都能够信任.NET框架?

#1


3  

The answer can be found here...

答案可以在这里找到......

https://msdn.microsoft.com/en-us/library/ms229026(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms229026(v=vs.110).aspx

Core namespaces include all System namespaces, excluding namespaces of the application models and the Infrastructure namespaces. Core namespaces include, among others, System, System.IO, System.Xml, and System.Net.

核心命名空间包括所有系统命名空间,不包括应用程序模型的命名空间和基础结构命名空间。核心命名空间包括System,System.IO,System.Xml和System.Net等。

X DO NOT give types names that would conflict with any type in the Core namespaces.

X不要提供与Core名称空间中的任何类型冲突的类型名称。

The caps are all there's I think they really really don't want you to do this. Why ? Because how would anyone be able to trust the .NET framework if there were 3rd party assemblies flying round, throwing in their own methods and classes in among the namespaces for the core framework ?

我认为他们真的不希望你这样做。为什么?因为如果第三方程序集飞来飞去,如何在核心框架的名称空间中引入自己的方法和类,那么任何人都能够信任.NET框架?