在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大
- 对于private方法,那只有做一个副本然后改成internal或public来测试
- 对于internal的类和方法可以直接利用[assembly]标签来指定该类可以被哪个dll/方案读取
/*******************************************************************************
* File Name: SoftwareInfoHelper.cs
* Namespace:
*
* CreateTime: 2018/9/6 14:50
* Author: linkanyway
* Description: SoftwareInfoHelper
* Class Name: SoftwareInfoHelper
*
* Ver ChangeDate Author Description
* ───────────────────────────────────
* V0.01 2018/9/6 14:50 linkanyway draft
*
* Copyright (c) 2018 linkanyway
* Description: Framework
*
*********************************************************************************/ using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("TestProject.Nirvana.Infrastructure")
] namespace Nirvana.Infrastructure.Runtime
{
/// <summary>
/// SoftwareInfoHelper
/// </summary>
// ReSharper disable once UnusedMember.Global
public static class SoftwareInfoHelper
{
/// <summary>
/// Return correct .NET Core product name like ".NET Core 2.1.0" instead of ".NET Core 4.6.26515.07" returning by RuntimeInformation.FrameworkDescription
/// </summary>
/// <returns></returns>
// ReSharper disable once UnusedMember.Global
internal static string GetFrameworkDescription()
{
// ".NET Core 4.6.26515.07" => ".NET Core 2.1.0"
// var parts = RuntimeInformation.FrameworkDescription.Split([' '], StringSplitOptions.RemoveEmptyEntries);
var charSeparators = new[] {','};
var parts = RuntimeInformation.FrameworkDescription.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
var i = ;
for (; i < parts.Length; i++)
{
if (char.IsDigit(parts[i][]))
{
break;
}
} var productName = string.Join("", parts, , i);
return string.Join("", productName, " ", GetNetCoreVersion());
} /// <summary>
///
/// </summary>
/// <returns></returns>
internal static string GetNetCoreVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] {'/', '\\'}, StringSplitOptions.RemoveEmptyEntries);
var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > && netCoreAppIndex < assemblyPath.Length - )
return assemblyPath[netCoreAppIndex + ];
return null;
} /// <summary>
///
/// </summary>
/// <returns></returns>
public static PlatformInformation GetPlatformInformation()
{
OSPlatform platform; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
platform = OSPlatform.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
platform = OSPlatform.Linux;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
platform = OSPlatform.OSX;
return new PlatformInformation()
{
Architecture = RuntimeInformation.OSArchitecture,
DotnetVersion = GetNetCoreVersion(),
Os = platform,
OsVersion = RuntimeInformation.OSDescription };
}
}
}