1

I am currently working on a Custom MembershipProvider implementation. But I need additional methods. I would like to call those methods directly on the Membership object within my Controller like this:

Membership.DoStuff()

Is it possible to do that with an extension method? Where would I start? thanks!

ckonig
  • 1,234
  • 2
  • 17
  • 29

4 Answers4

0

learning more about extension methods is a good start. Please refer to following articles

http://technico.qnownow.com/2012/03/17/how-to-create-extension-methods-in-net/

extension methods (MSDN)

Veera
  • 1
0

Why don't you add it directly to you class (which have the custom MemebershipProvider) then cast the membership clasd to your then you will find it.

If you asking about the extension methods it should work on any class, so the answer to your question is Yes.

Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75
0

After trying a lot of examples I have found this post where they state that you cannot write extension methods to a static class.

Membership is a static class and you cannot extend it.

Community
  • 1
  • 1
ckonig
  • 1,234
  • 2
  • 17
  • 29
0

yes, Membership is extensible, but you don't extends static class Membership (because it's impossible), you must extends abstract class MembershipProvider, and calls extension methods like Membership.Provider.DoStuff().

For example:

extension class

namespace Infrastructure.Extensions
{
    public static class MembershipProviderExtensions
    {
        public static void DoStuff(this MembershipProvider provider)
        {
             // do stuff
        }
    }
}

in your code

using Infrastructure.Extensions;
...
Membership.Provider.DoStuff()
...
sh34
  • 1