Skip to content

Commit

Permalink
Merge pull request #10 from TimChen44/main
Browse files Browse the repository at this point in the history
增加扩展函数
  • Loading branch information
TimChen44 authored Sep 8, 2023
2 parents d3366b2 + 5bcc82e commit 838e9e1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
22 changes: 22 additions & 0 deletions EasyTool.Core/IOCategory/FileTypeExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace EasyTool.Extension
{
public static class FileTypeExtension
{
/// <summary>
/// 文件流头部信息获得文件类型
///
/// 说明:
/// 1、无法识别类型默认按照扩展名识别
/// 2、xls、doc、msi、ppt、vsd头信息无法区分,按照扩展名区分
/// 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分
/// </summary>
/// <param name="file">文件</param>
/// <returns>类型,文件的扩展名,未找到为null</returns>
public static string GetType(this FileInfo file) => FileTypeUtil.GetType(file);
}
}
2 changes: 1 addition & 1 deletion EasyTool.Core/ImageCategory/ImgExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.IO;
using System.Text;

namespace EasyTool.ImageCategory
namespace EasyTool.Extension
{
public static class ImgExtension
{
Expand Down
90 changes: 90 additions & 0 deletions EasyTool.Core/ToolCategory/ClassExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace EasyTool.Extension
{
/// <summary>
/// ClassUtil 工具类提供了许多有用的方法,可以帮助您轻松处理和操作C#类
/// </summary>
public static class ClassExtension
{
/// <summary>
/// 获取类的继承层次结构
/// </summary>
/// <param name="type">要获取继承层次结构的类</param>
/// <returns>类的继承层次结构</returns>
public static Type[] GetClassHierarchy(this Type type) => ClassUtil.GetClassHierarchy(type);




/// <summary>
/// 获取类的静态属性的值
/// </summary>
/// <param name="type">要获取静态属性的类</param>
/// <param name="propertyName">要获取的静态属性的名称</param>
/// <returns>静态属性的值</returns>
public static object GetStaticPropertyValue(this Type type, string propertyName) => ClassUtil.GetStaticPropertyValue(type, propertyName);


/// <summary>
/// 设置类的静态属性的值
/// </summary>
/// <param name="type">要设置静态属性的类</param>
/// <param name="propertyName">要设置的静态属性的名称</param>
/// <param name="value">要设置的静态属性的值</param>
public static void SetStaticPropertyValue(this Type type, string propertyName, object value) => ClassUtil.SetStaticPropertyValue(type, propertyName, value);


/// <summary>
/// 获取类的静态字段的值
/// </summary>
/// <param name="type">要获取静态字段的类</param>
/// <param name="fieldName">要获取的静态字段的名称</param>
/// <returns>静态字段的值</returns>
public static object GetStaticFieldValue(this Type type, string fieldName) => ClassUtil.GetStaticFieldValue(type, fieldName);

/// <summary>
/// 设置类的静态字段的值
/// </summary>
/// <param name="type">要设置静态字段的类</param>
/// <param name="fieldName">要设置的静态字段的名称</param>
/// <param name="value">要设置的静态字段的值</param>
public static void SetStaticFieldValue(this Type type, string fieldName, object value) => ClassUtil.SetStaticFieldValue(type, fieldName, value);


/// <summary>
/// 动态调用类的静态方法
/// </summary>
/// <param name="type">要调用静态方法的类</param>
/// <param name="methodName">要调用的静态方法的名称</param>
/// <param name="arguments">要传递给静态方法的参数</param>
/// <returns>静态方法的返回值</returns>
public static object InvokeStaticMethod(this Type type, string methodName, object[] arguments) => ClassUtil.InvokeStaticMethod(type, methodName, arguments);

///// <summary>
///// 动态调用类的实例方法
///// </summary>
///// <param name="instance">要调用实例方法的类实例</param>
///// <param name="methodName">要调用的实例方法的名称</param>
///// <param name="arguments">要传递给实例方法的参数</param>
///// <returns>实例方法的返回值</returns>
//public static object InvokeMethod(object instance, string methodName, object[] arguments)
//{
// Type type = instance.GetType();
// MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
// return method.Invoke(instance, arguments);
//}

/// <summary>
/// 动态创建类的实例
/// </summary>
/// <param name="type">要创建实例的类</param>
/// <param name="constructorArguments">要传递给构造函数的参数</param>
/// <returns>类的新实例</returns>
public static object CreateInstance(this Type type, object[] constructorArguments) => ClassUtil.CreateInstance(type, constructorArguments);
}
}

0 comments on commit 838e9e1

Please sign in to comment.