I have a partial class that is split over two namespaces. The problem is that if I have an interface implemented on one of the partials, it is not recognized on the counterpart partial class. For example, I would expect the below to return true for being recognized as ILastModified
(C# fiddle at http://ideone.com/heLDn0):
using System;
using MyNamespace.One;
public class Test
{
public static void Main()
{
var item = new Product();
Console.WriteLine(item is ILastModified); //RETURNS FALSE??!
}
}
interface ILastModified
{
DateTime LastModified { get; set; }
}
namespace MyNamespace.One
{
public partial class Product
{
public int ID { get; set; }
}
}
namespace MyNamespace.Two
{
public partial class Product : ILastModified
{
public DateTime LastModified { get; set; }
}
}
All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.
The source file name for each part of the partial class can be different, but each partial class's name must be the same. The name of all parts of a partial class should be the same. All parts of a partial class should be in the same assembly.
In your case it will raise an error mentioning you already have defined a method with the same name. One modern caveat - that code would be totally fine if the partial classes were split into 2 separate files meant for 2 separate platform targets in a multi-targeted .
Partial classes allow the generator to simply emit the code they need to emit and they do not have to deal with user edits to the file. Users are likewise free to annotate the class with new members by having a second partial class. This provides a very clean framework for separation of concerns.
You cannot have a partial class in two different namespaces. The compiler treats those as two different classes.
I have a partial that is split over two namespaces.
You can't. By being in different namespaces, they are different classes.
Consider that this is the reason namespaces exist - so you can have the same class name for different classes.
From the C# Language Specification (C# 4.0), §10.2, Partial types:
Each part of a partial type declaration must include a partial modifier. It must have the same name and be declared in the same namespace or type declaration as the other parts.
(emphasis mine)
So, by definition, what you are doing is not a partial type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With