Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Extend Static Class across Projects

Tags:

c#

.net

I have a static class in a shared project, which I want to extend with further methods in another project. Because the method I want to extend the static class with is only applicable to the 2nd project and also dependent on other classes in that different project, I can't just move it to the shared project.

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

It seems that I can't do partial across projects, and I also can't extend static classes using extension methods.

How do I do this??!

For all later visitors: The chosen answer does what I asked, but the BETTER way is what Jon Skeet outlined - choose different class names and get it over with.

like image 829
Alex Avatar asked Nov 30 '22 06:11

Alex


1 Answers

You can't. A type can only exist in a single assembly. (That's why partial types can't be split between projects. The CLR has no concept of partial types - they're just compiler magic to compile a single type from multiple files.)

Why not just create a new type in the second project? Where would the benefit be in "extending" the class?

EDIT: It certainly sounds like you should have two classes, e.g. CommonValidation and ProjectValidation. It should be obvious which methods belong in which class... I really can't see why it would create a problem.

like image 169
Jon Skeet Avatar answered Dec 05 '22 11:12

Jon Skeet