Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use extension methods across many projects .

i am learning extension method, a very handy feature, which can save number of hours of coding, provides reusability. what i'm doing now a days, daily i'm creating 10 extension methods which useful in day to day scenario. but i'm not getting how to use these extension methods, everytime we need to add dll and reference it. or is there any smart way where we can use .

suppose

 public static bool isValidMail(this string str)
        {
            Regex reg = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return reg.IsMatch(str);
        }

if i created 100 extension methods like this , then for every project should i need to reference this static class dll. as i work with multplie projects. is there any way that we can put these extension methods in centralized location or some assembly cache, where we can easily add using statement and get access to all static methods.

can we do like this ?

  1. whenever we do create new project, can VS automatically add the extensionmethods which we created, so that in evey project we can access it. rather than adding dll everytime

  2. i want to know how you people do. i hope no one down votes it, just curious abt implementation of extension methods

like image 844
Ravi Gadag Avatar asked Oct 31 '11 14:10

Ravi Gadag


People also ask

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

Should you use extension methods?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.

Can you add extension methods to an existing static class?

No. Extension methods require an instance of an object.


1 Answers

It sounds like you should have a single project containing related extension methods - and then yes, you'll need to add a reference to that project from every project which needs it. That's a one-time cost. You could put it in the GAC, but personally I wouldn't - just treat it as another class library you need to depend on, like any other.

like image 126
Jon Skeet Avatar answered Oct 01 '22 08:10

Jon Skeet