C #: extending static class for projects

I have a static class in a collaborative project that I want to extend using other methods in another project. Since the method that I want to extend the static class is applicable only to the second project, and also depends on other classes in this other project, I cannot just move it to the general project.

Basically I have an X class in MySolution.SharedProject. I want to create an X.Get () method in MySolution.PrimaryProject that references a SharedProject.

It seems that I cannot execute partial on projects, nor can I distribute static classes using extension methods.

How to do it?!

For all later visitors: The selected answer does what I requested, but the BEST way is that John Skeet set out - to select different class names and get it using.

+3
source share
3 answers

You cannot use partial classes. You can use inheritance, but then you are not allowed to create a class static, so just make all its methods static. Then prevent the instance from being created by making it a protected constructor:

namespace SharedProject
{
    public class X
    {
        protected X() { }
        public static void SharedMethod() { }
    }
}

namespace PrimaryProject
{
    public class X : SharedProject.X
    {
        protected X() { }
        public static void ProjectMethod() { }
    }
}

You will be able to see both methods when using PrimaryProject.X. Naturally, these classes would actually be in different files, and indeed, in different projects.

, . .

, , , . , using.

+3

. . ( . CLR - .)

? "" ?

EDIT: , , , , . CommonValidation ProjectValidation. , ... , .

+15

but the configuration object is not available in the general project, only basically

Thus, these methods should only be visible in the main project, but I still want to reuse the rest as part of my shared library.

Looks like you just want some of the methods to be internal?

public static class Foo
{
    public static void AvailableAnywhere() {...}

    internal static void AvailableForThisAssemblyOnly() {...}
}

If I misunderstood, then other options:

  • using the interface to describe configuration parameters (so there is no concrete type in secondary projects)
  • using [InternalsVisibleTo]
+2
source

Source: https://habr.com/ru/post/1714703/


All Articles