5

I have a class named NIFERepository. It contains a SaveObject method, which returns nothing and performs an operation on a database.

public class NIFERepository<E>
{
    protected void SaveObject(object saveObject)
    {
        if (saveObject is E)
            this.RepositorySession.Merge(saveObject);
    }
}

1 . I wish to create a public extension method who calls this SaveObject() method, like this:

    public static class NIFERepositoryExtensions
    {
        public static T Save<T, E>(this T self, E saveObject) where T : NIFERepository<E>
        {
            self.SaveObject(saveObject);
            return self;
        }
    } 

But the protected scope doesn't allow my extensions method to recognize it.

Is there a way for me to get a method of this form to work?

2 . I created my extension method with 2 types: T and E. T is the instance who called it, and E is the defined type into my ProductRepository<Product>, for example. When I call this method, the defined type is not shown. enter image description here

Is there a way for me to get this to work?

Servy
  • 202,030
  • 26
  • 332
  • 449
Kiwanax
  • 1,265
  • 1
  • 22
  • 41
  • `SaveObject()` should be generic, or at the very least should probably throw an exception if called with the wrong type. – SLaks Apr 17 '13 at 18:44
  • I don't typed here, but my SaveObject() is throwing an exception when the object parameters is of the wrong type. But I would want my extension method beauty, showing the right type into the call of it. But it works fine. Thanks! – Kiwanax Apr 17 '13 at 18:54
  • Do not edit answers into the question. If you have an answer to your own question than post the answer as *an answer*. – Servy Apr 19 '13 at 13:41
  • Is there a reason you can't add implement this extension method as an actual public member of the class itself? – DCShannon Jan 13 '15 at 05:15

2 Answers2

9
  1. You cannot use extension methods to violate encapsulation.
    If the class does not expose the method, you can't call it (except with Reflection).
    The method is (hopefully) protected for a reason; you're probably doing something wrong.
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    I thought Extensions methods recognized all types of scope from extended classes. I'll try another way here, thanks! – Kiwanax Apr 17 '13 at 18:52
  • @Kiwanax Extension methods are not different than calling a static method. Just a syntatic sugar. – I4V Apr 17 '13 at 18:55
  • Really haha. Do you see another way to do what I want? the SaveObject() method could be recognized into extension methods but not in normal instances, like the example above? – Kiwanax Apr 17 '13 at 19:57
  • 1
    @Kiwanax: No; that is not possible. – SLaks Apr 18 '13 at 14:16
  • @Kiwanax: It sounds like you actually want `public void SaveObject(E obj)` in the class itself. – SLaks Apr 18 '13 at 14:32
  • Actually, it appears to be more of an implementation detail of the extension method internals. I'd say since an extension method is no less related to the extended class than a subclass, why not provide protected access? Again, this looks like more of a limitation of the C# implementation of extension methods than a conscious decision to prohibit protected access. – Scott Apr 29 '15 at 16:55
  • 1
    @ScottMcKinney: No. Derived classes can only access protected members _through a qualifier of their own type_. Extension methods do not create their own type, so it does not make sense to allow protected members. – SLaks Apr 29 '15 at 16:57
  • Right, which is why I said, " it appears to be more of an implementation detail of the extension method internals." – Scott Feb 04 '16 at 20:35
2

Protected method are visible inside inherited classes. So do inheritance and create a public method that calls the base protected method like

public class BaseClass
{
  protected void SomeMethod()
   {

   }
}

public class ChildClass:BaseClass
{
  public void SomeMethodPublic()
        {
          base.SomeMethod()
        }
}
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • 1
    It's a little confusing to use the `base` keyword in a situation like this. The `SomeMethod` is not overridden og hidden in `ChildClass`, so personally I would use `this.SomeMethod()` or (more often) just `SomeMethod()`. – Jeppe Stig Nielsen Apr 17 '13 at 18:52
  • 3
    This is an old thread but I had to comment. Using base.SomeMethod() is the acceptable call because the method is in the base class. using this.SomeMethod(), although does work, is the confusing way because it implies that SomeMethod is in the current class. – Daniel Bardi Oct 10 '15 at 21:36