继承一个实现从.NET Framework到.NET Standard的IValidatableObject的类

问题描述 投票:1回答:1

我有两个库-第一个在.NET Framework 4.6.1中,而第二个在.NET Standard 2.0中。 (第二个取决于第一个)

。NET Framework 4.6.1包含以下类

    public class Class_Framework : IValidatableObject
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            Console.WriteLine();
            return null;
        }
    }

然后,我尝试在第二个.NET Standard 2.0库中使用该类:

    interface IStandardInterface
    {
        string AProperty { get; set; }
    }

    class Class_Standard : Class_Framework, IStandardInterface
    {
        public string AProperty { get; set; }
    }

    public class MainStandardClass
    {
        public void AMethod()
        {
            IStandardInterface anInstance = new Class_Standard();
        }
    }

由于以下错误,我无法编译此代码:

The type 'IValidatableObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

我知道.NET标准中没有DataAnnotations库,因此我在.NET Standard和.NET Framework库中都使用了NuGet System.ComponentModel.Annotations。最奇怪的是,如果我更改

IStandardInterface anInstance = new Class_Standard();

to

var anInstance = new Class_Standard();

然后一切正常。您是否有任何建议如何/是否可以从.NET Framework库继承实现IValidatableObject的类?谢谢

c# .net data-annotations .net-standard
1个回答
0
投票

不可行,期限。 netstandard 2.0库只能从netstandard 2.0库继承,不能从完整的.NET库继承。

这是每个定义。

您将不得不拆分类层次结构或将其他库移到其他位置。我们在这里尚不足以推荐一种体面的方法。

© www.soinside.com 2019 - 2024. All rights reserved.