2

In c# if I have this

private void Run(Web site) {
    site.BreakRoleInheritance(false, false);
}

private void Run(ListItem folder) {
    folder.BreakRoleInheritance(false, false);
}

How can I instead make 1 function that can accept either Site or Folder?

2
  • Use an interface it's the best way for these types of questions. Commented Aug 11, 2020 at 1:35
  • 1
    You seem to be adding a lot of comments to answers that should have been in the question. Add all your constraints and why you cant use common approaches Commented Aug 11, 2020 at 1:48

3 Answers 3

5

If Site and Folder are classes that you've created, then you can create a common interface which those classes inherit from. For example:

public interface IBreakable
{
    void break();
}

public class Folder : IBreakable 
{
    public void break() { /* implementation here*/ }
}

public class Site : IBreakable 
{
    public void break() { /* implementation here*/ }
}

Usage

private void Run(IBreakable breakable)
{
    breakable.break();
}

Edit

Here's a solution based on reflection, although this is not ideal.

void Run(object obj)
{
    MethodInfo method = obj.GetType().GetMethod("break");

    if (!(method is null))
    {
        method.Invoke(obj, new object[] {});
    }
}

Given

public class Foo
{
    public void break() {Console.WriteLine("Foo");}
}

public class Bar
{
    public void break() {Console.WriteLine("Bar");}
}

public class Bad
{
    public void NotBreak() {Console.WriteLine("Bad");}
}

Usage

Foo foo = new Foo();
Bar bar = new Bar();
Bad bad = new Bad();

Run(foo);
Run(bar);
Run(bad);

Output

Foo

Bar

Sign up to request clarification or add additional context in comments.

5 Comments

The 2 classes come from an SDK though so I can't make it implement it.
@omega Are you able to provide the SDK details. It might be the case that they do have a common interface/base class. I'll provide another solution using reflection.
The SDK is Microsoft.SharePoint.Client
@omega See my edit. Also worth noting that the docs for Folder and Site have a common inheritance on ClientObject. Maybe ClientObject has the method you're wanting to use.
I figured it out using SecurableObject instead, it supported most of the common methods, and then had to use reflection for the last one.
2

As a later answer from @Pedro pointed out, those specific classes derive from a common ancestor, and that would be the preferred option. Assuming you did not have that option:

You can use the C# dynamic type (sorry if that is not the latest doc, I couldn't find a newer one): https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic

Example:

using System;
                
public class Program
{
    public static void Main()
    {
        var foo = new Foo();
        var bar = new Bar();
        DoSomething(foo);
        DoSomething(bar);
    }

    private static void DoSomething<T>(T someObjectWithDoMethod)
    {
        ((dynamic)someObjectWithDoMethod).Do();
    }
}

public class Foo
{
  public void Do() { Console.WriteLine("Foo is doing something"); }
}

public class Bar
{
  public void Do() { Console.WriteLine("Bar is doing something"); }
}

.Net Fiddle: https://dotnetfiddle.net/MShLK5

Comments

1

Based on your comments to other answers, Web and ListItem are types defined in the Microsoft.SharePoint.Client.

Checking the SDK docs, both types derive from SecurableObject where the BreakRoleInheritance method is defined.

That being the case, all you need to do is define one method that takes a SecurableObject object as an input:

public void Run(SecurableObject item)
{
    item.BreakRoleInheritance();
}

And you should be able to pass a Web and a ListItem to this same method.

2 Comments

It didn't have .Update() so I had to use reflection there
@omega I'm glad you were able to figure it out. Just an advice, make sure you include as much detail as possible to the question itself, for instance in the question there is no mention of an .Update() method, so I had no idea you needed that when I wrote my answer.

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.