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

推荐订阅源

V
Visual Studio Blog
IT之家
IT之家
F
Fortinet All Blogs
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
GbyAI
GbyAI
Recent Announcements
Recent Announcements
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 司徒正美
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
博客园_首页
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
爱范儿
爱范儿
F
Full Disclosure
博客园 - 【当耐特】
V
V2EX
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Cisco Talos Blog
Cisco Talos Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
S
Schneier on Security
Latest news
Latest news
S
Secure Thoughts
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cyberwarzone
Cyberwarzone
Spread Privacy
Spread Privacy
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Palo Alto Networks Blog
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 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" });
            ;
        }
    }

请食客慢慢享用