【发布时间】:2019-05-15 17:42:49
【问题描述】:
atm 我正在“玩”一些 C# 可以做的事情——只是想知道它是如何工作的。 现在我有了在运行时从 dll 中加载一个类的想法,该类从我的原始程序中扩展了一个类。示例:
// My class in the dll.
namespace MyDll
{
public class MyClassInDll : MyClass
{
public MyClassInDll(int num) : base(num)
{
// May contain more code :)
}
}
}
// My abstract class which is inherited in the dll.
namespace MyProgram
{
public class MyClass
{
private int num;
public MyClass(int numToUse)
{
num = numToUse;
}
public void WriteTheNumber()
{
Console.WriteLine(num);
}
}
}
// My mainfile.
namespace MyProgram
{
public class Program
{
public void Main(String[] args)
{
// Load the DLL
Assembly dll = Assembly.LoadFile("myDll.dll");
// Create an instance of MyClassInDll stored as MyClass.
MyClass myclass = ? // I dont know what to enter here :(
// Call the function my program knows from MyClass
myclass.WriteTheNumber(); // this should write the in the constructor passed integer to the console.
}
}
}
所以这是我的问题:
- 最重要的是:创建实例需要做什么? (构造函数需要传参)
- 我如何检查它是否成功(或者更好的是哪些异常意味着什么?)
- 什么(其他)可能出错(找不到 dll,dll 不包含类,dll 中的类不扩展 MyClass,MyClassInDll 的构造函数与假设不同,MyClass 的方法和属性与使用的 MyClass 版本不同在 dll 中),有什么我可以做的吗?
- 方法调用是否在 dll 中使用最终被覆盖的方法? (应该,但我不确定)
我真的希望你能帮我解决这个问题,我在 Google 上找到的只是在外部 dll 中的类中运行单个方法(或者我太愚蠢而看不到答案),但对继承的类一无所知。
谢谢!
【问题讨论】:
标签: c# dll runtime .net-assembly