无法正确识别不同名称空间中的局部类 [英] Partial classes in different namespace are not being recognized correctly

查看:96
本文介绍了无法正确识别不同名称空间中的局部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个局部类,分为两个名称空间。问题是,如果我在部分之一上实现了接口,则在对应的部分类上无法识别该接口。例如,我希望下面的代码因被识别为 ILastModified 而返回true(C#在 http://ideone.com/heLDn0 ):

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; }
    }
}


推荐答案

您不能在两个不同的名称空间中使用局部类。编译器将它们视为两个不同的类。

You cannot have a partial class in two different namespaces. The compiler treats those as two different classes.

这篇关于无法正确识别不同名称空间中的局部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆