Browse Source

添加Form唯一控制工具(特殊需求)

yuzhengyang 8 years ago
parent
commit
efdd0cc860

+ 18 - 0
Fork.Net/Test/Y.Test/Commons/R.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Y.Utils.DataUtils.Collections;
+
+namespace Y.Test.Commons
+{
+    public static class R
+    {
+        public static FormDictionaryTool FD = new FormDictionaryTool();
+
+        public static void Test()
+        {
+            FD.Get();
+        }
+    }
+}

+ 1 - 0
Fork.Net/Test/Y.Test/Y.Test.csproj

@@ -84,6 +84,7 @@
     <Reference Include="System.Xml" />
     <Reference Include="System.Xml" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
+    <Compile Include="Commons\R.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Views\ChineseCalendarForm.cs">
     <Compile Include="Views\ChineseCalendarForm.cs">

+ 47 - 0
Fork.Net/Y.Utils/DataUtils/Collections/FormDictionaryTool.cs

@@ -0,0 +1,47 @@
+//************************************************************************
+//      https://github.com/yuzhengyang
+//      author:     yuzhengyang
+//      date:       2017.7.31 - 2017.7.31
+//      desc:       Form唯一字典工具
+//      Copyright (c) yuzhengyang. All rights reserved.
+//************************************************************************
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Forms;
+
+namespace Y.Utils.DataUtils.Collections
+{
+    /// <summary>
+    /// Form唯一字典工具
+    /// </summary>
+    public class FormDictionaryTool
+    {
+        protected ConcurrentDictionary<Type, Form> dictionary = new ConcurrentDictionary<Type, Form>();
+
+        public T Get<T>() where T : Form
+        {
+            if (dictionary.ContainsKey(typeof(T)))
+            {
+                Form value;
+                if (dictionary.TryGetValue(typeof(T), out value))
+                {
+                    if (value != null && !value.IsDisposed)
+                    {
+                        return (T)value;
+                    }
+                }
+            }
+
+            T form = default(T);
+            if (dictionary.TryAdd(typeof(T), form))
+            {
+                return form;
+            }
+
+            return null;
+        }
+    }
+}

+ 1 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -52,6 +52,7 @@
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="AppUtils\AppUnique.cs" />
+    <Compile Include="DataUtils\Collections\FormDictionaryTool.cs" />
     <Compile Include="DataUtils\RandomUtils\RandomTool.cs" />
     <Compile Include="DataUtils\RandomUtils\RandomTool.cs" />
     <Compile Include="DataUtils\StringUtils\NumberStringTool.cs" />
     <Compile Include="DataUtils\StringUtils\NumberStringTool.cs" />
     <Compile Include="DelegateUtils\ProgressDelegate.cs" />
     <Compile Include="DelegateUtils\ProgressDelegate.cs" />