X3

RedSky

导航

C#访问或修改私有类、函数、变量、属性

.NET:

public static class TypeUtil
    {
        public static Type? GetType(string assemblyName, string typePath)
        {
            var assembly = Assembly.Load(assemblyName);
            if (assembly == null) return null;
            return assembly.GetType(typePath);
        }

        public static ConstructorInfo? Constructor(string assemblyName, string typePath, Type[] parametersType)
        {
            return Constructor(GetType(assemblyName, typePath), parametersType);
        }

        public static ConstructorInfo? Constructor(Type type, Type[] parametersType)
        {
            if(type == null) return null;
            return type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, parametersType);
        }

        public static object? CreateInstance(string assemblyName, string typePath, Type[] parametersType, object[] parameters)
        {
            return Constructor(GetType(assemblyName, typePath), parametersType)?.Invoke(parameters);
        }

        public static object? CreateInstance(Type type, Type[] parametersType, object[] parameters)
        {
            return Constructor(type, parametersType)?.Invoke(parameters);
        }

        public static MethodInfo? GetMethod(Type type, string methodName, Type?[] parametersType)
        {
            return parametersType == null ?
                type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                : type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, parametersType);
        }
        public static object? InvokeMethod(object instance, Type type, string methodName, Type[] parametersType, object[] parameters)
        {
            var method = GetMethod(type, methodName, parametersType);
            if(method == null) return null;
            return method.Invoke(instance, parameters);
        }

        public static PropertyInfo? GetProperty(Type type, string propertyName)
        {
            if(type == null) return null;
            return type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        }

        public static object? GetPropertyValue(object instance, Type type, string propertyName)
        {
            if(instance == null) return null;
            return GetProperty(type, propertyName)?.GetValue(instance);
        }

        public static void SetProperty(object instance, Type type, string propertyName, object value)
        {
            GetProperty(type, propertyName)?.SetValue(instance, value);
        }

        public static FieldInfo? GetField(Type type, string fieldName)
        {
            if (type == null) return null;
            return type.GetField(fieldName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        }

        public static object? GetFieldValue(object instance, Type? type, string fieldName)
        {
            return GetField(type, fieldName)?.GetValue(instance);
        }
        public static void SetField(object instance, Type? type, string fieldName, object? value)
        {
            GetField(type, fieldName)?.SetValue(instance, value);
        }

.Net Framework:

public static class TypeUtil
{
    public static Type GetType(string assemblyName, string typePath)
    {
        var assembly = Assembly.Load(assemblyName);
        if (assembly == null) return null;
        return assembly.GetType(typePath);
    }

    public static ConstructorInfo Constructor(string assemblyName, string typePath, Type[] parametersType)
    {
        return Constructor(GetType(assemblyName, typePath), parametersType);
    }

    public static ConstructorInfo Constructor(Type type, Type[] parametersType)
    {
        if (type == null) return null;
        return type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parametersType, null);
    }

    public static object CreateInstance(string assemblyName, string typePath, Type[] parametersType, object[] parameters)
    {
        return Constructor(GetType(assemblyName, typePath), parametersType)?.Invoke(parameters);
    }

    public static object CreateInstance(Type type, Type[] parametersType, object[] parameters)
    {
        return Constructor(type, parametersType)?.Invoke(parameters);
    }

    public static MethodInfo GetMethod(Type type, string methodName, Type[] parametersType)
    {
        return parametersType == null ?
            type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
            : type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parametersType, null);
    }
    public static object InvokeMethod(object instance, Type type, string methodName, Type[] parametersType, object[] parameters)
    {
        var method = GetMethod(type, methodName, parametersType);
        if (method == null) return null;
        return method.Invoke(instance, parameters);
    }

    public static PropertyInfo GetProperty(Type type, string propertyName)
    {
        if (type == null) return null;
        return type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    }

    public static object GetPropertyValue(object instance, Type type, string propertyName)
    {
        if (instance == null) return null;
        return GetProperty(type, propertyName)?.GetValue(instance);
    }

    public static void SetProperty(object instance, Type type, string propertyName, object value)
    {
        GetProperty(type, propertyName)?.SetValue(instance, value);
    }

    public static FieldInfo GetField(Type type, string fieldName)
    {
        if (type == null) return null;
        return type.GetField(fieldName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    }

    public static object GetFieldValue(object instance, Type type, string fieldName)
    {
        return GetField(type, fieldName).GetValue(instance);
    }
    public static void SetField(object instance, Type type, string fieldName, object value)
    {
        GetField(type, fieldName).SetValue(instance, value);
    }
}

使用参考代码:


    public class ClassA
    {
        public int Id { get; set; } = 10;
        public string Name { get; private set; }
        string Description { get; set; }
        string _name;
        static int MaxId = 10;

        public ClassA(string name, string description)
        {
            this._name = name;
            this.Name = name;
            this.Description = description;
        }

        public string GetDescription()
        {
            return this.Name;
        }
        public void SetDescription(string description)
        {
            this.Description = description;
        }
        public static ClassA CreatInstance(string name, string des)
        {
            return new ClassA(name, des);
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            var obj = TypeUtil.CreateInstance(typeof(ClassA), new Type[] { typeof(string), typeof(string) }, new object[] { "张三", "学生" });
            var name = TypeUtil.GetFieldValue(obj, obj.GetType(), "_name");
            var id = TypeUtil.GetPropertyValue(obj, obj.GetType(), "Id");
            TypeUtil.SetField(obj, obj.GetType(), "_name", "reset name");
            TypeUtil.SetProperty(obj, obj.GetType(), "Id", 11);
            TypeUtil.SetField(obj, obj.GetType(), "MaxId", 11);
            var name1 = TypeUtil.InvokeMethod(obj, obj.GetType(), "GetDescription", null, null);
            TypeUtil.InvokeMethod(obj, obj.GetType(), "SetDescription", new Type[] { typeof(string) }, new object[] { "Des" });
            ;
        }
    }

 

请食客慢慢享用

posted on 2024-05-27 10:57  HotSky  阅读(116)  评论(1编辑  收藏  举报