ntminer 5 years ago
parent
commit
c60ac809f5

+ 0 - 3
src/NTMinerDaemon/NTMinerDaemon.csproj

@@ -106,9 +106,6 @@
     <Compile Include="..\NTMinerlib\DataExtensions.cs">
       <Link>DataExtensions.cs</Link>
     </Compile>
-    <Compile Include="..\NTMinerlib\EnumExtension.cs">
-      <Link>EnumExtension.cs</Link>
-    </Compile>
     <Compile Include="..\NTMinerlib\GZipUtil.cs">
       <Link>GZipUtil.cs</Link>
     </Compile>

+ 3 - 1
src/NTMinerlib/EnumExtension.cs → src/NTMinerDataSchemas/EnumExtension.cs

@@ -6,6 +6,7 @@ namespace NTMiner {
     using System.ComponentModel;
     using System.Reflection;
 
+    [IsNotDataSchema]
     public class EnumItem<T> where T : struct {
         public static IEnumerable<EnumItem<T>> GetEnumItems() {
             return EnumDic<T>.Instance.EnumItems;
@@ -35,7 +36,7 @@ namespace NTMiner {
             if (name != null) {
                 return EnumDic<T>.Instance.TryGetValue(name, out value);
             }
-            value = default(T);
+            value = default;
             return false;
         }
 
@@ -44,6 +45,7 @@ namespace NTMiner {
         }
     }
 
+    [IsNotDataSchema]
     internal class EnumDic<T>
         where T : struct {
         public static readonly EnumDic<T> Instance = new EnumDic<T>();

+ 7 - 0
src/NTMinerDataSchemas/IsNotDataSchemaAttribute.cs

@@ -0,0 +1,7 @@
+using System;
+
+namespace NTMiner {
+    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
+    public class IsNotDataSchemaAttribute : Attribute {
+    }
+}

+ 2 - 0
src/NTMinerDataSchemas/NTMinerDataSchemas.csproj

@@ -60,7 +60,9 @@
     <Compile Include="Core\MinerServer\MinerSign.cs" />
     <Compile Include="Core\Profile\IProfile.cs" />
     <Compile Include="DataSchemaIdAttribute.cs" />
+    <Compile Include="EnumExtension.cs" />
     <Compile Include="IConsoleOutLine.cs" />
+    <Compile Include="IsNotDataSchemaAttribute.cs" />
     <Compile Include="NTKeyword.cs" />
     <Compile Include="Report\ISpeedData.cs" />
     <Compile Include="Report\SpeedData.cs" />

+ 0 - 1
src/NTMinerlib/NTMinerlib.csproj

@@ -59,7 +59,6 @@
     <Compile Include="StringExtensions.cs" />
     <Compile Include="NTMinerRegistry.cs" />
     <Compile Include="Repositories\IJsonReadOnlyRepository.cs" />
-    <Compile Include="EnumExtension.cs" />
     <Compile Include="NTMinerException.cs" />
     <Compile Include="Messages.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 7 - 4
src/UnitTests/NTMinerDataSchemasTests.cs

@@ -1,5 +1,5 @@
 using Microsoft.VisualStudio.TestTools.UnitTesting;
-using NTMiner;
+using Newtonsoft.Json;
 using System;
 using System.Linq;
 
@@ -17,8 +17,11 @@ namespace NTMiner {
                     skipCount++;
                     continue;
                 }
+                if (type.IsDefined(typeof(IsNotDataSchemaAttribute), false)) {
+                    continue;
+                }
                 var ctors = type.GetConstructors();
-                Assert.IsTrue(ctors.Length != 0);
+                Assert.IsTrue(ctors.Length != 0, type.FullName);
                 if (ctors.All(a => a.IsStatic)) {
                     Console.WriteLine(type.FullName);
                     skipCount++;
@@ -26,8 +29,8 @@ namespace NTMiner {
                 }
                 // 1 确保有默认构造函数
                 Assert.IsTrue(ctors.Any(a => a.IsPublic && a.GetParameters().Length == 0), type.FullName);
-                // 2 所有属性都是公共的可读写的
-                Assert.IsTrue(type.GetProperties().All(a => a.CanWrite && a.CanRead && a.GetMethod.IsPublic && a.SetMethod.IsPublic), type.FullName);
+                // 2 所有属性都是公共的可读写的,除非标记了JsonIgnore
+                Assert.IsTrue(type.GetProperties().Where(a=> !a.IsDefined(typeof(JsonIgnoreAttribute), false)).All(a => a.CanWrite && a.CanRead && a.GetMethod.IsPublic && a.SetMethod.IsPublic), type.FullName);
             }
             Console.WriteLine($"共{types.Length.ToString()}个类型,以上类型被跳过,因为它们不是类型或者是抽象类型或者是静态类型或者是Attribute类型或者不是NTMiner定义的类型,共跳过{skipCount.ToString()}条");
         }