Manoz
Manoz

Reputation: 6617

how to create an extension method with system namespace

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

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

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

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.

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.

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()

'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?)

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

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

Please correct me.

Upvotes: 0

Views: 1080

Answers (1)

Mick
Mick

Reputation: 6864

The answer can be found here...

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.

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

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 ?

Upvotes: 3

Related Questions