anita
anita

Reputation: 193

Accessing a private method in a overridden public method

I am currently having some issues with accessing a private method from a overriden public method.

My situation is like this: I have a compiled .dll file which consist basically of this

public class OriginalHandler
{
    public virtual void Request()
    {
        RedirectIfConditionIsFulfilled()
        this.PeformRequest()    
    } 


    protected virtual bool PeformRequest()
    {

    }

    private static void RedirectIfConditionIsFulfilled()
    {

    }

}

I need to alter the method PeformRequest(), So i make a public class, which inherit OriginalHandler and override the method as such:

public class ModifiedOriginalHandler : OriginalHandler 
{

    protected override bool PeformRequest()
    {

    }

}

To ensure that this method doesn't violate an "impact" scope, I have to ensure that it only get evaluated on certain sites, We use this to ensure that HttpRequestProcess ONLY impact the desired site using this

namespace Sitecore.Sharedsource.Pipelines.HttpRequest
{
    using System.Collections.Generic;

    using Assert = Sitecore.Diagnostics.Assert;
    using S = Sitecore;

    public abstract class SiteSpecificHttpRequestProcessor: S.Pipelines.HttpRequest.HttpRequestProcessor
    {
            public abstract List<string> _sites;


            public sealed override void Process(S.Pipelines.HttpRequest.HttpRequestArgs args)
            {
                Assert.ArgumentNotNull(args, "args");

                if (S.Context.Site == null || !this._sites.FirstOrDefault(S.Context.Site.Name))
                {
                    return;
                }

                this.DoProcess(args, this._sites.FirstOrDefault(S.Context.Site.Name));
            }

        protected abstract void DoProcess(S.Pipelines.HttpRequest.HttpRequestArgs args, string);
    }
}

So include my ModifiedOriginalHandler to include this

 public class SiteSpecificModifiedOriginalHandler: SiteSpecificHttpRequestProcessor
 {
Public override List<String> _sites => new[]
{
    "www.only.com"  , "www.boat.com"
};


public virtual HttpContext GetHttpContext()
    {
        return HttpContext.Current;
    }

    public override void DoProcess(HttpRequestArgs args, string)
    {
        var mediaRequest = new ModifiedOriginalHandler ();
        var context = GetHttpContext();
    var site = Sitecore.Context.Site;

        if (site == null)
        {
            return;
        }

        if (string != null)
        {
            mediaRequest.Request(context);
        }
        else
        {
            OriginalHandler baseClass = mediaRequest;
            baseClass.Request(context);
        }
    }
 }

This Is where I am having a problem, I can from the SiteSpecificModifiedOriginalHandler not call the protected method PeformRequest,
but can call the public method Request which internally calls the desired function, so I make an override function, to ensure that the original is not being called but my modified version

public class ModifiedOriginalHandler : OriginalHandler 
{

    protected override bool PeformRequest()
    {

    }

    public override void Request()
    {
        RedirectIfConditionIsFulfilled()
        this.PeformRequest()    
    }        
}

Which is where I am having my problem, RedirectIfConditionIsFulfilled is a private method, and I can in no way make this method call as such. I could in my overridden function remove this call, but that would require RedirectIfConditionIsFulfilled to be removed, which would alter the original functionality, which i don't want to do.

So how do i overcome this?

How do i access a private method in a overriden public method?

Upvotes: 0

Views: 505

Answers (2)

InBetween
InBetween

Reputation: 32790

If you have access to OriginalHandler implementation then make the derived class a nested one:

class A {
    private void Foo() { }
    protected virtual void Bar() { }

    public class B: A {
        protected override void Bar() {
            Foo(); \\ legal } } }

If you don’t then barring reflection there is no way to access from an external type a private member of another type.

Upvotes: 3

Scott Hannen
Scott Hannen

Reputation: 29312

You can't access a private method from an inherited class. (But you know that.) But your question didn't give any reason why your method shouldn't be protected, which would expose it to inherited classes.

What you're describing is exactly why protected exists.

A nested class will work, but I don't recommend it unless you want every single inherited class that needs the private method to be nested inside the base class. What if some of those inherited classes have their own private methods and even more inherited classes need to access those? You'd have to nest classes inside your nested classes.

Upvotes: 2

Related Questions