csharp .NET Standard如何与.NET平台相关

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp .NET Standard如何与.NET平台相关相关的知识,希望对你有一定的参考价值。

namespace Analogy
{
    /// <summary>
    /// This example shows that a library that needs access to target .NET Standard 1.3
    /// can only access APIs available in that .NET Standard. Even though similar the APIs exist on .NET 
    /// Framework 4.5, it implements a version of .NET Standard that isn't compatible with the library.
    /// </summary>INetCoreApp10
    class Example1
    {
        public void Net45Application(INetFramework45 platform)
        {
            // .NET Framework 4.5 has access to all .NET Framework APIs
            platform.FileSystem();

            platform.Console();


            // This fails because .NET Framework 4.5 does not implement .NET Standard 1.3
            // Argument 1: cannot convert from 'Analogy.INetFramework45' to 'Analogy.INetStandard13'
            NetStandardLibrary13(platform);
        }

        public void NetStandardLibrary13(INetStandard13 platform)
        {
            platform.FileSystem();

            platform.Console();
        }
    }


    /// <summary>
    /// This example shows a library targeting multiple frameworks and 2 different applications
    /// using that library. MultipleTargetsLibrary needs access to the FileSystem, that API was only available
    /// in .NET Standard 1.3. .NET Standard 1.3 only works with .NET Framework 4.6. Because of this
    /// MultipleTargetsLibrary needs to add support for .NET Framework 4.5 explicitly.
    /// </summary>
    class Example2
    {
        public void Net45Application(INetFramework451 platform)
        {
            // On the .NET 4.5.1 application, the INetFramework45 implementation is choson
            MultipleTargetsLibrary(platform);
        }

        public void NetCoreApplication(INetCoreApp10 platform)
        {
            // On the .NET Core 1.0 application, the INetStandard13 implementation is choson
            MultipleTargetsLibrary(platform);
        }

        public void MultipleTargetsLibrary(INetFramework45 platform)
        {
            platform.FileSystem();
        }

        public void MultipleTargetsLibrary(INetStandard13 platform)
        {
            platform.FileSystem();
        }
    }

    /// <summary>
    /// This example shows how future platforms can be added without the need to change libraries that
    /// target the .NET Standard. JSON.NET targets .NET Standard 1.0 and can run on *ANY* platform that implements
    /// the standard.
    /// </summary>
    class Example3
    {
        /// <summary>
        /// This future platform implements .NET Standard 1.3
        /// </summary>
        public void FuturePlatformApplication(ISomeFuturePlatform platform)
        {
            // You are able to use JSON.NET with the future platform without recompiling JSON.NET
            JsonNet(platform);
        }

        /// <summary>
        /// This method represents the implementation of JSON.NET. JSON.NET supports .NET Standard 1.0.
        /// </summary>
        public void JsonNet(INetStandard10 platform)
        {
            platform.Linq();

            platform.Reflection();

            platform.Collections();
        }
    }
}
using System;

namespace Analogy
{
    // Each interface represents a target framework and methods represents groups of APIs available on that target framework. 
    // The goal is to show the relationship between .NET Standard API surface and other .NET platforms


    // .NET Standard

    interface INetStandard10
    {
        void Primitives();
        void Reflection();
        void Tasks();
        void Collections();
        void Linq();
    }

    interface INetStandard11 : INetStandard10
    {
        void ConcurrentCollections();
        void InteropServices();
    }

    interface INetStandard12 : INetStandard11
    {
        void ThreadingTimer();
    }

    interface INetStandard13 : INetStandard12
    {
        void FileSystem();
        void Console();
        void ThreadPool();
        void Process();
        void Sockets();

        void AsyncLocal();
    }

    interface INetStandard14 : INetStandard13
    {
        void IsolatedStorage();
    }

    interface INetStandard15 : INetStandard14
    {
        void AssemblyLoadContext();
    }

    // .NET Framework 

    interface INetFramework45 : INetStandard11
    {
        void FileSystem();
        void Console();
        void ThreadPool();
        void Crypto();
        void WebSockets();
        void Process();
        void Sockets();
        
        void AppDomain();
        void Xml();
        void Drawing();
        void SystemWeb();
        void WPF();
        void WindowsForms();
        void WCF();
    }

    interface INetFramework451 : INetFramework45, INetStandard12
    {
        // TODO: .NET Framework 4.5.1 specific APIs
    }

    interface INetFramework452 : INetFramework451, INetStandard12
    {
        // TODO: .NET Framework 4.5.2 specific APIs
    }

    interface INetFramework46 : INetFramework452, INetStandard13
    {
        // TODO: .NET Framework 4.6 specific APIs
    }

    interface INetFramework461 : INetFramework46, INetStandard14
    {
        // TODO: .NET Framework 4.6.1 specific APIs
    }

    interface INetFramework462 : INetFramework461, INetStandard15
    {
        // TODO: .NET Framework 4.6 specific APIs
    }
    
    // Mono
    
    interface IMono43 : INetFramework46
    {
        void MonoSpecificApi();
    }

    // Windows Universal Platform

    interface IWindowsUniversalPlatform : INetStandard14
    {
        void GPS();
        void Xaml();
    }

    // Xamarin 

    interface IXamarinIOS : INetStandard15
    {
        void AppleAPIs();
    }

    interface IXamarinAndroid : INetStandard15
    {
        void GoogleAPIs();
    }

    // .NET Core

    interface INetCoreApp10 : INetStandard15
    {

    }

    // Future platform

    interface ISomeFuturePlatform : INetStandard13
    {
        // A future platform chooses to implement a specific .NET Standard version.
        // All libraries that target that version are instantly compatible with this new
        // platform
    }
}

以上是关于csharp .NET Standard如何与.NET平台相关的主要内容,如果未能解决你的问题,请参考以下文章

csharp .NET Standard如何与.NET平台相关

将依赖注入与 .NET Core 类库 (.NET Standard) 结合使用

如何在新创建的 .NET Core 2.0 Web 应用程序中定位 .NET Standard 2.0?

针对 .NET Core 与 .NET Standard

.NET Standard 类库的编译与 .NET Framework 有何不同?

.NET Standard:程序集的清单定义与程序集引用不匹配