1

I have an existing partial class that has extension methods applied to it, in one project.

I want to add an attribute to that class within a different project, but when I create a second partial class the extension methods disappear.

Initially I created the class with the new attribute as a child of the original class, but I want to avoid the tedious up-cast of an instance of the original class to the new child class (though this may be the "best" way in the end).

Is there anyway to add the attribute without losing the extension methods, without using inheritance?

1
  • I'm not really an expert but, as you pointed out, I'm not sure if it's a great idea to use partial classes this way. Commented Sep 22, 2011 at 20:45

1 Answer 1

3

You can't declare a partial class across projects - it's got to be in a single project.

Basically if you need an extra attribute on the class, you'll have to put that in the original project.

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

3 Comments

@BalaR: Nope, there's no such restriction. So long as it's still a static top-level non-generic class, that's fine. In particular, in Edulinq I had one huge partial class spread across loads of files :)
Ah ok, I see. You are correct. It's not that the extension methods disappeared, it's that the class was redefined. Ok, so inheritance it is.
@BalaR In fact you can add an extension method to any type indicator you can use in a method header, including interfaces.

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.