如何实例化类中使用反射在C#/。NET程序集? [英] How to instantiate the class in an assembly using Reflection with C#/.NET?

查看:147
本文介绍了如何实例化类中使用反射在C#/。NET程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个库编译为calc.dll。

 命名空间MyClass的
{
    公共类计算器
    {
        公众诠释值1 {获得;组;}
        公众诠释值2 {获得;组;}
        公共计算器()
        {
            值1 = 100;
            值2 = 200;
        }

        公众诠释添加(INT VAL1,INT将val2)
        {
            值1 = VAL1;值2 = val2的;
            返回值1 +值2;
        }
    }
}
 

我要实例化的计算课,而不会链接到calc.dll。可C#这样做吗?我想出了这个code,但我不知道如何实例化计算器类。

 使用系统;
使用System.IO;
使用的System.Reflection;
使用System.Diagnostics程序;
使用System.Collections.Generic;

命名空间EX
{
    公共类code
    {
        公共静态无效测试()
        {
            字符串路径= Directory.GetCurrentDirectory();
            目标字符串= Path.Combine(路径,@./ myclass.dll);
            大会ASM = Assembly.LoadFrom(目标);

            计算器H =新计算器(); //<  -  ???
            类型类型= h.GetType();
            MethodInfo的M = type.GetMethod(添加);

            INT RES =(int)的m.Invoke(H,参数);
            Console.WriteLine({0},RES);
        }

        公共静态无效的主要()
        {
            测试();
        }
    }
}
 

ADDED

我有两个方案,一个是从巴拉 - [R

  VAR参数=新的对​​象[] {100,200};
        字符串路径= Directory.GetCurrentDirectory();
        目标字符串= Path.Combine(路径,@./ myclass.dll);
        大会ASM = Assembly.LoadFrom(目标);
        键入计算值= asm.GetType(MyClass.Calculator);
        对象H = Activator.CreateInstance(计算值);

        MethodInfo的M = calc.GetMethod(添加);
        INT RES =(int)的m.Invoke(H,参数);
        Console.WriteLine({0},RES);
 

和这一个是从代理-J

 字符串路径= Directory.GetCurrentDirectory();
        目标字符串= Path.Combine(路径,@./ myclass.dll);
        大会ASM = Assembly.LoadFrom(目标);
        类型类型= asm.GetType(MyClass.Calculator);
        ConstructorInfo男星= type.GetConstructor(Type.EmptyTypes);
        对象计算值= ctor.Invoke(空);
        MethodInfo的M = type.GetMethod(添加);

        VAR参数=新的对​​象[] {100,200};

        INT RES =(int)的m.Invoke(计算,参数);
        Console.WriteLine({0},RES);
 

他们两人的工作,但我还挺preFER巴拉的解决方案,因为它更短和获取物体H 的CreateInstance 是不是让构造函数来获得更多的intutive 物体H(计算值)

解决方案

 对象H = Activator.CreateInstance(asm.FullName,MyClass.Calculator);
 

编辑:

看看这个工程

 键入计算值= asm.GetType(MyClass.Calculator);
对象H = Activator.CreateInstance(计算值);
 

I have this library to compiled to 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;
        }
    }
}

I want to instantiate the Calculate class without linking to the calc.dll. Can C# do that? I came up with this code, but I don't know how to instantiate the Calculator class.

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

ADDED

I have two solutions, one is from 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); 

And this one is from 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);

Both of them are working, but I kinda prefer Bala's solution as it's shorter and getting object h through CreateInstance is more intutive than getting constructor to get object h(calc).

解决方案

object h = Activator.CreateInstance(asm.FullName, "MyClass.Calculator");

EDIT:

See if this works

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

这篇关于如何实例化类中使用反射在C#/。NET程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆