惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
罗磊的独立博客
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
博客园 - 叶小钗
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
博客园 - 【当耐特】
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
量子位
宝玉的分享
宝玉的分享
V
Visual Studio Blog
博客园_首页
IT之家
IT之家
V
V2EX
腾讯CDC
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
有赞技术团队
有赞技术团队
J
Java Code Geeks
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security

博客园 - HotSky

Chrome启用CDP(chrome-devtools-protocol)进行远程操控 WPF 让ScrollViewer支持按住鼠标中键拖拽滚动内容 WPF 让ScrollViewer支持鼠标中键滚动缩放内容 Nodejs使用 nodejs中写sql需要用in时的写法 C#城市最短路径 C#Animation Sqlite PDF附录A: 内容流操作码 WPF FPS类 生命模拟 C# Sql帮助类,可扩展 WPF WriteableBitmap通过GDI+绘制帮助类 Alpha混合 Bmp读写二值图 WPF支持任意快捷键+鼠标组合的绑定类 WPF阻止窗体被系统缩放,使用显示器DPI WPF DataGrid自动增长序号列 WPF一个简单的属性编辑控件
C#访问或修改私有类、函数、变量、属性
HotSky · 2024-05-27 · via 博客园 - HotSky

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

请食客慢慢享用