Browse Source

添加反射工具

yuzhengyang 9 years ago
parent
commit
55125a1741

+ 10 - 7
Fork.Net/Y.Utils/ComputerUtils/ComputerTool.cs

@@ -55,23 +55,26 @@ namespace Y.Utils.ComputerUtils
         #region 获取网卡信息
         /// <summary>
         /// 获取网卡信息
-        /// Item1:描述,Item2:物理地址(Mac),Item3:Ip地址
+        /// Item1:描述,Item2:物理地址(Mac),Item3:Ip地址,Item4:网关地址
         /// </summary>
         /// <returns></returns>
-        public static List<Tuple<string, string, string>> GetNetworkCardInfo()
+        public static List<Tuple<string, string, string, string>> GetNetworkCardInfo()
         {
             try
             {
-                List<Tuple<string, string, string>> result = new List<Tuple<string, string, string>>();
+                List<Tuple<string, string, string, string>> result = new List<Tuple<string, string, string, string>>();
                 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
                 foreach (var item in adapters)
                 {
                     if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                     {
-                        result.Add(new Tuple<string, string, string>(
-                            item.Description,
-                            item.GetPhysicalAddress().ToString(),
-                            item.GetIPProperties().UnicastAddresses[1].Address.ToString()));
+                        string _desc = item.Description;
+                        string _mac = item.GetPhysicalAddress().ToString();
+                        string _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
+                            item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
+                        string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
+                            item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
+                        result.Add(new Tuple<string, string, string, string>(_desc, _mac, _ip, _gateway));
                     }
                 }
                 return result;

+ 61 - 0
Fork.Net/Y.Utils/ReflectionUtils/SimpleReflection.cs

@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using Y.Utils.FileUtils;
+using Y.Utils.LogUtils;
+
+namespace Y.Utils.ReflectionUtils
+{
+    public class SimpleReflection : MarshalByRefObject
+    {
+        //public string AssemblyPath { get; set; }
+        //public SimpleReflection(string assemblyPath)
+        //{
+        //    AssemblyPath = assemblyPath;
+        //}
+        //public Assembly AssemblyResolve(object sender, ResolveEventArgs args)
+        //{
+        //    return SearchAssembly(AssemblyPath, args.Name);
+        //}
+        private Assembly SearchAssembly(string path, string name)
+        {
+            try
+            {
+                List<string> dlls = FileTool.GetFile(path, "*.dll");
+                foreach (var dll in dlls)
+                {
+                    Assembly ass = Assembly.LoadFile(dll);
+                    if (ass.FullName == name)
+                        return ass;
+                }
+            }
+            catch (Exception e)
+            {
+                Log.e(e.Message);
+            }
+            return null;
+        }
+
+        public T Do<T>(string file, string className, string methodName, object[] args, object[] values)
+        {
+            //获取dll中所有类
+            Type[] types = Assembly.LoadFile(file).GetTypes();
+            //从列表中获取指定类
+            Type cls = types.FirstOrDefault(x => x.FullName.Contains(className));
+            if (cls != null)
+            {
+                //创建实例
+                object instance = Activator.CreateInstance(cls, args);
+                //根据名称获取方法
+                MethodInfo method = cls.GetMethod(methodName);
+                //执行方法
+                object result = method.Invoke(instance, values);
+                return (T)result;
+            }
+            return default(T);
+        }
+    }
+}

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

@@ -55,7 +55,7 @@
   <ItemGroup>
     <Compile Include="AppUtils\UniqueTool.cs" />
     <Compile Include="LogUtils\Log.cs" />
-    <Compile Include="ComputerUtils\ComputerTool.cs" />
+    <Compile Include="ComputerUtils\ComputerInfoTool.cs" />
     <Compile Include="ComputerUtils\ComputerPermissionTool.cs" />
     <Compile Include="ComputerUtils\RegisterTool.cs" />
     <Compile Include="ImageUtils\BarCodeToHTML.cs" />
@@ -80,6 +80,7 @@
     <Compile Include="FileUtils\FileTool.cs" />
     <Compile Include="BaseUtils\ListTool.cs" />
     <Compile Include="JsonUtils\JsonTool.cs" />
+    <Compile Include="ReflectionUtils\SimpleReflection.cs" />
     <Compile Include="TxtUtils\IniTool.cs" />
     <Compile Include="TxtUtils\LogTool.cs" />
     <Compile Include="TxtUtils\TxtTool.cs" />