c# - 如何使用 C#/.NET 的反射在程序集中实例化类?

标签 c# .net reflection invoke instantiation

我有这个库要编译成 calc.dll。

namespace MyClass
{
    public class Calculator
    {
        public int Value1 {get; set;}
        public int Value2 {get; set;}
        public Calculator()
        {
            Value1 = 100;
            Value2 = 200;
        }

        public int Add(int val1, int val2)
        {
            Value1 = val1; Value2 = val2;
            return Value1 + Value2;
        }
    }
}

我想在不链接到 calc.dll 的情况下实例化 Calculate 类。 C# 可以做到吗?我想出了这段代码,但我不知道如何实例化 Calculator 类。

using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;

namespace EX
{
    public class Code
    {
        public static void Test()
        {
            string path = Directory.GetCurrentDirectory();
            string target = Path.Combine(path, @"./myclass.dll");
            Assembly asm = Assembly.LoadFrom(target);

            Calculator h = new Calculator(); // <-- ???
            Type type = h.GetType();
            MethodInfo m = type.GetMethod("Add");

            int res = (int) m.Invoke(h, param);
            Console.WriteLine("{0}", res);
        }

        public static void Main()
        {
            Test();
        }
    }
}

已添加

我有两个解决方案,一个来自Bala R

        var param = new object[] {100, 200};
        string path = Directory.GetCurrentDirectory();
        string target = Path.Combine(path, @"./myclass.dll");            
        Assembly asm = Assembly.LoadFrom(target);            
        Type calc = asm.GetType("MyClass.Calculator");
        object h  = Activator.CreateInstance(calc);         

        MethodInfo m = calc.GetMethod("Add");            
        int res = (int) m.Invoke(h, param);            
        Console.WriteLine("{0}", res); 

而这个来自agent-j

        string path = Directory.GetCurrentDirectory();
        string target = Path.Combine(path, @"./myclass.dll");
        Assembly asm = Assembly.LoadFrom(target);
        Type type = asm.GetType("MyClass.Calculator");
        ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
        object calc = ctor.Invoke(null);
        MethodInfo m = type.GetMethod("Add");

        var param = new object[] {100, 200};

        int res = (int) m.Invoke(calc, param);
        Console.WriteLine("{0}", res);

它们都有效,但我更喜欢 Bala 的解决方案,因为它更短并且通过 CreateInstance 获取 object h 比让构造函数获取 object 更直观h(计算).

最佳答案

string path = Directory.GetCurrentDirectory();
string target = Path.Combine(path, @"./myclass.dll");
Assembly asm = Assembly.LoadFrom(target);
Type type = asm.GetType("MyClass.Calculator");
ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
object calc = ctor.Invoke (null);
MethodInfo m = type.GetMethod("Add");

int res = (int) m.Invoke(calc, param);
Console.WriteLine("{0}", res);      

关于c# - 如何使用 C#/.NET 的反射在程序集中实例化类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472980/

相关文章:

c# - Fastmember 访问非公共(public)属性

java - 检查对象是否来自某个包

reflection - F# nameof 运算符不是一流的函数

c# - 系统.InvalidOperationException : 'A method was called at an unexpected time when calling LaunchUriAsync inside ToastNotification UWP

c# - 成员(member)条件自动映射器

c# - 将 CORS 添加到索引时(以编程方式)Azure.Search.Documents 引发错误

c# - 在安装过程中选择 ClickOnce 应用程序的安装路径

c# - 基本关于 "using"构造

c# - GenericADOException 未处理无法插入

c# - 如何在 C# 中从 System.Array 转换为 object[]