src/01_Utils/Utils/Extensions/TypeExtensions.cs

(开头部分) 6KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Linq;
using System.Reflection;

// ReSharper disable once CheckNamespace
namespace Mkh;

/// <summary>
/// 类型扩展
/// </summary>
public static class TypeExtensions
{
    /// <summary>
    /// 判断属性是否是静态的
    /// </summary>
    /// <param name="property"></param>
    /// <returns></returns>
    public static bool IsStatic(this PropertyInfo property) => (property.GetMethod ?? property.SetMethod).IsStatic;

    /// <summary>
    /// 判断指定类型是否实现于该类型
    /// </summary>
    /// <param name="serviceType"></param>
    /// <param name="implementType"></param>
    /// <returns></returns>
    public static bool IsImplementType(this Type serviceType, Type implementType)
    {
        //泛型
        if (serviceType.IsGenericType)
        {
            if (serviceType.IsInterface)
            {
                var interfaces = implementType.GetInterfaces();
                if (interfaces.Any(m => m.IsGenericType && m.GetGenericTypeDefinition() == serviceType))
                {
                    return true;
                }
            }
            else
            {
                if (implementType.BaseType != null && implementType.BaseType.IsGenericType && implementType.BaseType.GetGenericTypeDefinition() == serviceType)
                {
                    return true;
                }
            }
        }
        else
        {
            if (serviceType.IsInterface)
            {
                var interfaces = implementType.GetInterfaces();
                if (interfaces.Any(m => m == serviceType))
                    return true;
            }
            else
            {
                if (implementType.BaseType != null && implementType.BaseType == serviceType)
                    return true;
            }
        }
后面还有 219 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。