-1

I have a partial Class "ClassA" in an assembly MyAssembly.

Can I extend this partial class in a separate assembly "MyExtendedAssembly" with the same name "ClassA"

When partial classes are not possible in a separate assembly, is there any other way where we can extend the properties without inheritance?

Thanks

Satyajit
  • 1,971
  • 5
  • 28
  • 51
  • 1
    Have you tried it? This seems to be a question that you could very easily find out the answer to yourself. – Jashaszun Jul 28 '15 at 19:57
  • 2
    I googled your exact question header and came up with this: http://stackoverflow.com/questions/3858649/partial-classes-in-separate-dlls – Daniel Persson Jul 28 '15 at 19:58
  • 1
    Why would you want to do this? What problem are you trying to solve? – crush Jul 28 '15 at 19:58
  • How was this upvoted? :/ – Jashaszun Jul 28 '15 at 19:58
  • @Jashaszun Tried, it didn't work. Trying if there are any ways to achieve this. – Satyajit Jul 28 '15 at 19:59
  • @crush Trying to allow extending a model with the same name, without using inheritance. – Satyajit Jul 28 '15 at 20:02
  • You should build an interface or base (potentially abstract) class, then let the other assembly implement the interface or extend the base class. You could also uses extension methods on a static class to "extend" a class with new methods, but that won't add new fields/properties. Any particular reason you don't want to use inheritance? – crush Jul 28 '15 at 20:03
  • @Jashaszun, I upvoted it. It's a clear and concise question with a definite answer. – Samuel Neff Jul 28 '15 at 20:40

2 Answers2

3

No, partial classes must be in the same assembly. They are only partial in source code. When the code is compiled the compiler finds all the partial parts and combines them at compile time into a single class.

The runtime has no concept of a partial class, only the compiler.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • Thanks. Are there any ways similar thing can be achieved? – Satyajit Jul 28 '15 at 20:00
  • @user1731788, yes, you need some kind of plugin system. Declare an interface that has the methods you want to call, then use reflection to find any implementations and call them. Use properties or whatever on the interface to pair up calling and callee classes. The code we use for finding assignable classes is posted here: http://stackoverflow.com/questions/2159879/find-class-to-instantiate-by-name-without-namespace-or-assembly-net – Samuel Neff Jul 28 '15 at 20:37
1

use extension and interface if you dont like inhertance. Partial class are mean to be together in one assembly like a partner in a family. ^^,

CyberNinja
  • 872
  • 9
  • 25