C#訪問或修改私有類、函式、變數、屬性

HotSky發表於2024-05-27
    public static class TypeUtl
    {
        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);
        }

使用參考程式碼:

public partial class App : Application
    {
        public App()
        {
            var obj = TypeUtl.CreateInstance(typeof(ClassA), new Type[] { typeof(string), typeof(string) }, new object[] { "張三", "學生" });
            var name = TypeUtl.GetFieldValue(obj, obj.GetType(), "_name");
            var id = TypeUtl.GetPropertyValue(obj, obj.GetType(), "Id");
            TypeUtl.SetField(obj, obj.GetType(), "_name", "reset name");
            TypeUtl.SetProperty(obj, obj.GetType(), "Id", 11);
            TypeUtl.SetField(obj, obj.GetType(), "MaxId", 11);
            var name1 = TypeUtl.InvokeMethod(obj, obj.GetType(), "GetDescription", Array.Empty<Type>(), null);
            TypeUtl.InvokeMethod(obj, obj.GetType(), "SetDescription", new Type[] { typeof(string) }, new object[] { "Des" });
            ;
        }
    }

    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);
        }
    }

請食客慢慢享用

相關文章