0

I have a generated partial class

public partial interface IPartialInterface
{
   Task<object> Method(string param)
}
public partial class PartialClass : IPartialInterface
{
    public Task<object> Method(string param)
    {
        // does stuff
    }
}

I want to extend the Method so I can plug some logic and then depending on my logic let the PartialClass.Method logic take over or stop it.

5
  • Have you heard of partial methods? That's the best you'll be able to get. Commented Nov 12, 2021 at 13:05
  • short answer. you can't. you can't even "extend" a partial method; that's just a declaration meaning "implemented elsewhere". and think about it: in what order would the two parts be executed? what would variable scopes be like? Commented Nov 12, 2021 at 13:05
  • Thanks, @FranzGleichmann. I searched for a bit and couldn't find anything. Thought it was just me, but guess not ;( Commented Nov 12, 2021 at 13:07
  • The Manual is quite informative on what partial methods can and can't do. but in short it's limited to: one part (the generated) of the class defines what the method signature looks like, one part (the user-written) optionally defines what it does. and in the end, you're simply limited to what the generator generated. Commented Nov 12, 2021 at 13:11
  • For me, it sounds like inheritance is the way to go here. Commented Nov 12, 2021 at 13:39

1 Answer 1

0

For people with the same problem, this worked for me:

    public partial interface IGeneratedPartialInterface
    {
        Task<object> Method(string param)
    }
    public partial class GeneratedPartialClass : IGeneratedPartialInterface
    {
        public Task<object> Method(string param)
        {
             // does stuff
        }
    }
        
    public interface MyInterface
    {
        Task<object> MethodExtended(string param)
    }
        
    // added by me, but must be named as the generated partial class
    public partial class GeneratedPartialClass : MyInterface
    {        
        public Task<object> MethodExtended(string param)
        {
            // my logic here example
            if (isInCache(param))
            {
                // do stuff
            }
            else 
            {
                return this.Method(param)
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.