I have class foo
public abstract class Foo
{
protected int[,] bar;
public abstract Point test(int a, int b);
}
And in program, I have another 4 classes inherited from foo
.
class inheritedClass : Foo
{
// ...
}
And I want to load another class from DLL and add it to List<Foo>
, where I store all classes that are inherited from Foo
. How can I do it? Now, I have this:
try
{
DLL = Assembly.LoadFile(name);
Type type = DLL.GetType("DLL.customObject");
var obj = Activator.CreateInstance(type);
// Even when [obj] is not null, [newObj] is always null
newObj = obj as Foo;
}
catch
{
return false;
}
Here's my DLL
namespace DLL
{
public class customObject : Foo
{
// ...
}
public abstract class Foo
{
protected int[,] bar;
public abstract Point test(int a, int b);
}
}
答案 0 :(得分:1)
据我所知,你想从DLL中创建一个自定义类的sublcass。在C#中是不可能的。如果您无法访问类的代码,则无法更改其继承层次结构。请考虑使用List<object>
来存储您的对象。
答案 1 :(得分:0)
var assembly= Assembly.LoadFile(@"C:\myDll.dll");
// Assembly executingAssembly = assembly.GetExecutingAssembly();
Type type = assembly.GetType("DLL.customObject");
object obj = Activator.CreateInstance(type);
or
Type t = AppDomain.CurrentDomain.GetAssemblies();
Hover on t you might get current assemblies that are loaded.