david-coder
2/10/2020 - 8:08 AM

Reflect

反射处理类

public static class Reflect
{

    public static object CreateObject(Type sourceType, params object[] args)
    {
        return sourceType.Assembly.CreateInstance(
                    sourceType.FullName, false, BindingFlags.CreateInstance,
                    null, args, null, null);
    }
    public static object CreateObject(string TypeName, params object[] Params)
    {
        var type = Type.GetType(TypeName);
        return type.Assembly.CreateInstance(
                    TypeName, false, BindingFlags.CreateInstance,
                    null, Params, null, null);
    }

    public static object CreateList(Type itemType, ref Type listType)
    {

        listType = typeof(List<>).MakeGenericType(itemType);

        var result = listType.Assembly.CreateInstance(listType.FullName, false, BindingFlags.CreateInstance,
                                                      null, null, null, null);

        return result;

    }
    public static object AddToList(object listSource, Type listType, Type itemType, Dictionary<string, object> propertyFields = null)
    {

        var item = itemType.Assembly.CreateInstance(itemType.FullName, false, BindingFlags.CreateInstance,
                                                    null, null, null, null);

        if (propertyFields != null)
        {
            foreach (var field in propertyFields)
            {
                SetPropertyValue(item, field.Key, field.Value);
            }
        }

        listType.InvokeMember("Add", BindingFlags.InvokeMethod, null, listSource, new object[] { item });

        return item;

    }

    public static object FindInList(object listSource, Type listType, Predicate<object> formula)
    {
        return listType.InvokeMember("Find", BindingFlags.InvokeMethod, null, listSource, new object[] { formula });
    }
    public static object FindInList(object listSource, Type listType, string propertyName, object propertyValue)
    {
        var fma = new Predicate<object>(o => GetPropertyValue(o, propertyName) == propertyValue);
        return listType.InvokeMember("Find", BindingFlags.InvokeMethod, null, listSource, new object[] { fma });
    }

    public static void SetPropertyValue(object target, string propertyName, object value)
    {

        object result = target;
        string[] paths = propertyName.Split('.');

        PropertyInfo ppt = null;

        for (int i = 0; i < paths.Length; i++)
        {

            Type type = result.GetType();

            var pInfo = type.GetProperty(paths[i]);
            if (pInfo == null) return;

            if (i == (paths.Length - 1)) ppt = pInfo;

        }

        if (ppt != null)
        {
            if (ppt.PropertyType.BaseType == typeof(Enum))
            {
                try
                {
                    var enumValue = System.Enum.Parse(ppt.PropertyType, value.ToString(), true);
                    ppt.SetValue(target, enumValue, null);
                }
                catch (Exception) { }
            }
            else
            {
                ppt.SetValue(target, value, null);
            }
        }

    }
    public static object GetPropertyValue(object target, string propertyName)
    {

        object result = target;
        string[] paths = propertyName.Split('.');

        for (int i = 0; i < paths.Length; i++)
        {

            Type type = result.GetType();

            var pInfo = type.GetProperty(paths[i]);
            if (pInfo == null) return null;

            result = pInfo.GetValue(result, null);
            if (result == null) return null;

        }

        return result;

    }

    public static string GetDescription(object target)
    {

        Type source = target is Type ? target as Type : target.GetType();
        var field = target is Enum ? target : null;

        string fieldName = field != null ? field.ToString().Trim() : string.Empty;
        Attribute attr = null;

        if (!string.IsNullOrEmpty(fieldName))
        {
            var f = source.GetField(fieldName);
            attr = Attribute.GetCustomAttribute(f, typeof(DescriptionAttribute));
        }
        else
        {
            attr = Attribute.GetCustomAttribute(source, typeof(DescriptionAttribute));
        }

        return attr != null ? (attr as DescriptionAttribute).Description : string.Empty;

    }

    public static List<string> DescriptEnum<T>() where T : System.Enum
    {
        return DescriptEnum(typeof(T));
    }
    public static List<string> DescriptEnum(Type type)
    {

        var results = new List<string>();

        var values = System.Enum.GetValues(type);

        foreach (var v in values)
        {
            results.Add(GetDescription(v));
        }

        return results;

    }

}