Reputation: 467
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);
}
}
Upvotes: 0
Views: 324
Reputation: 841
As I understand you want to make a class from DLL a sublcass of your custom class. It is not possible in C#. If you don't have access to the code of a class you can't change its inheritance hierarchy. Consider using List<object>
to store your objects.
Upvotes: 1
Reputation: 4275
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.
Upvotes: 0