【问题标题】:How to instantiate the class in an assembly using Reflection with C#/.NET?如何使用反射与 C#/.NET 在程序集中实例化类?
【发布时间】:2011-06-24 19:44:20
【问题描述】:

我有这个库要编译成 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;
        }
    }
}

我想实例化Calculate 类而不链接到calc.dll。 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(calc) 更直观。

【问题讨论】:

    标签: c# .net reflection invoke instantiation


    【解决方案1】:
    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);      
    

    【讨论】:

    • 如何在m.Invoke(h, param)中获取h
    • @prosseek, ` object calc = ctor.Invoke (null); MethodInfo m = type.GetMethod("Add"); int res = (int) m.Invoke(calc, param); `
    【解决方案2】:
    object h = Activator.CreateInstance(asm.FullName, "MyClass.Calculator");
    

    编辑:

    看看这是否有效

    Type calc = asm.GetType("MyClass.Calculator)";
    object h  = Activator.CreateInstance(calc);
    

    【讨论】:

    • 它编译,但我得到Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at EX.Code.Test() at EX.Code.Main() 错误。
    • @prosseek 查看我的编辑。我从未使用过CreateInstance() 的第一个重载。我对asm.FullName 有点怀疑,但我使用了第二种方法,它应该可以工作。
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多