Part1-3:.Net .Standard

Part1-3:.Net .Standard

一.
先建立类库Standard为2.0的项目:TestLib1
再建立个.net core项目(.net core 3.0):CoreConsole1,并引用类库Standard为2.1的项目:TestLib1
再建立个.net framework项目(.net framework 4.7.2):FrameworkConsole1,并引用类库Standard为2.1的项目:TestLib1

二.
1…net core(在VS解决方案资源管理器中,右键“CoreConsole1”设为“启动项目”,然后运行的结果)
typeof(FileStream).Assembly.Location:
C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.0.3\System.Private.CoreLib.dll
typeof(Class1).Assembly.Location:
C:\Users\hao.yu\Desktop\C#\1\TestLib1\CoreConsole1\bin\Debug\netcoreapp3.0\TestLib1.dll

2…net framework(在VS解决方案资源管理器中,右键“FrameworkConsole1”设为“启动项目”,然后运行的结果)
typeof(FileStream).Assembly.Location:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
typeof(Class1).Assembly.Location:
C:\Users\hao.yu\Desktop\C#\1\TestLib1\FrameworkConsole1\bin\Debug\TestLib1.dll

3.standard(在VS解决方案资源管理器中,打开“TestLib1”中的“Class1.cs”,ctrl+鼠标左键点击“FileStream”,弹出“FileStream”代码后,双击第一行即可得到“FileStream”在standard的位置)
typeof(FileStream).Assembly.Location:
C:\Users\hao.yu.nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll
typeof(Class1).Assembly.Location:
C:\Users\hao.yu\Desktop\C#\1\TestLib1\TestLib1\bin\Debug\netstandard2.0\TestLib1.dll

三.下载反编译器:ILSpy(下载地址:https://github.com/icsharpcode/ILSpy/releases/tag/v8.0)
下载好之后,把下载好的压缩包解压,打开ILSpy.exe
反编译.NET Standard下的System.IO下的FileStream、.Net Framework和.Net Core下的System.IO下的FileStream下的BeginRead,会发现.Net Framework和.Net Core根据标准实现了各自的实现(具体见最下面)
所以得出结论:
.NET Standard只有定义,没有实现
.Net Core和.Net Framework根据标准实现了各自的实现。

1…NET Standard下的System.IO下的FileStream(反编译:C:\Users\hao.yu.nuget\packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll),会发现.NET Standard只定义未实现

// netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// System.IO.FileStream
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;

public class FileStream : Stream
{
	public override bool CanRead
	{
		get
		{
			throw null;
		}
	}

	public override bool CanSeek
	{
		get
		{
			throw null;
		}
	}

	public override bool CanWrite
	{
		get
		{
			throw null;
		}
	}

	[Obsolete("This property has been deprecated.  Please use FileStream's SafeFileHandle property instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
	public virtual IntPtr Handle
	{
		get
		{
			throw null;
		}
	}

	public virtual bool IsAsync
	{
		get
		{
			throw null;
		}
	}

	public override long Length
	{
		get
		{
			throw null;
		}
	}

	public string Name
	{
		get
		{
			throw null;
		}
	}

	public override long Position
	{
		get
		{
			throw null;
		}
		set
		{
		}
	}

	public virtual SafeFileHandle SafeFileHandle
	{
		get
		{
			throw null;
		}
	}

	public FileStream(SafeFileHandle handle, FileAccess access)
	{
	}

	public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize)
	{
	}

	public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
	{
	}

	[Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
	public FileStream(IntPtr handle, FileAccess access)
	{
	}

	[Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")]
	public FileStream(IntPtr handle, FileAccess access, bool ownsHandle)
	{
	}

	[Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")]
	public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
	{
	}

	[Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")]
	public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
	{
	}

	public FileStream(string path, FileMode mode)
	{
	}

	public FileStream(string path, FileMode mode, FileAccess access)
	{
	}

	public FileStream(string path, FileMode mode, FileAccess access, FileShare share)
	{
	}

	public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
	{
	}

	public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
	{
	}

	public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
	{
	}

	public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
	{
		throw null;
	}

	public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
	{
		throw null;
	}

	protected override void Dispose(bool disposing)
	{
	}

	public override int EndRead(IAsyncResult asyncResult)
	{
		throw null;
	}

	public override void EndWrite(IAsyncResult asyncResult)
	{
	}

	~FileStream()
	{
	}

	public override void Flush()
	{
	}

	public virtual void Flush(bool flushToDisk)
	{
	}

	public override Task FlushAsync(CancellationToken cancellationToken)
	{
		throw null;
	}

	public virtual void Lock(long position, long length)
	{
	}

	public override int Read(byte[] array, int offset, int count)
	{
		array = null;
		throw null;
	}

	public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
	{
		throw null;
	}

	public override int ReadByte()
	{
		throw null;
	}

	public override long Seek(long offset, SeekOrigin origin)
	{
		throw null;
	}

	public override void SetLength(long value)
	{
	}

	public virtual void Unlock(long position, long length)
	{
	}

	public override void Write(byte[] array, int offset, int count)
	{
	}

	public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
	{
		throw null;
	}

	public override void WriteByte(byte value)
	{
	}
}

2…Net Framework下的System.IO下的FileStream下的BeginRead:(反编译C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll),实现了方法

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// System.IO.FileStream
using System.Security;
using System.Security.Permissions;

[SecuritySafeCritical]
[HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
{
	if (array == null)
	{
		throw new ArgumentNullException("array");
	}
	if (offset < 0)
	{
		throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
	}
	if (numBytes < 0)
	{
		throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
	}
	if (array.Length - offset < numBytes)
	{
		throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
	}
	if (_handle.IsClosed)
	{
		__Error.FileNotOpen();
	}
	if (!_isAsync)
	{
		return base.BeginRead(array, offset, numBytes, userCallback, stateObject);
	}
	return BeginReadAsync(array, offset, numBytes, userCallback, stateObject);
}

3…Net Core下的System.IO下的FileStream下的BeginRead:(反编译C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.0.3\System.Private.CoreLib.dll),实现了方法

// System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// System.IO.FileStream
using System.Threading;
using System.Threading.Tasks;

public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback callback, object? state)
{
	if (array == null)
	{
		throw new ArgumentNullException("array");
	}
	if (offset < 0)
	{
		throw new ArgumentOutOfRangeException("offset", SR.ArgumentOutOfRange_NeedNonNegNum);
	}
	if (numBytes < 0)
	{
		throw new ArgumentOutOfRangeException("numBytes", SR.ArgumentOutOfRange_NeedNonNegNum);
	}
	if (array.Length - offset < numBytes)
	{
		throw new ArgumentException(SR.Argument_InvalidOffLen);
	}
	if (IsClosed)
	{
		throw new ObjectDisposedException(SR.ObjectDisposed_FileClosed);
	}
	if (!CanRead)
	{
		throw new NotSupportedException(SR.NotSupported_UnreadableStream);
	}
	if (!IsAsync)
	{
		return base.BeginRead(array, offset, numBytes, callback, state);
	}
	return TaskToApm.Begin(ReadAsyncTask(array, offset, numBytes, CancellationToken.None), callback, state);
}

从上面可以看出,.NET Standard只定义未实现,.Net Framework和.Net Core实现了各自的实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值