Aspnetcore: how to : use Assembly load some dll file?

Created on 20 Jun 2018  路  2Comments  路  Source: dotnet/aspnetcore

I want to dynamically load dll files锛宧ow to use Assembly?
webform i can do like this:
Assembly asm = Assembly.LoadFrom("aaaaa.dll");
obj = asm.CreateInstance(ClassFullName);
Type t = asm.GetType(ClassFullName);
method = t.GetMethod("Run");

area-blazor

Most helpful comment

You can use reflection from your Blazor, but because your application runs in the browser you don't have access to the file system like you typically would in Web Forms app. So you can't just read an assembly off of the local file system, but you could, for example, retrieve the assembly from the server.

var bytes = await Http.GetByteArrayAsync("_framework/_bin/ClassLibrary1.dll");
var assembly = System.Reflection.Assembly.Load(bytes);
var t = assembly.GetType("ClassLibrary1.Class1");
var m = t.GetMethod("GetMessage");
Console.WriteLine($"ClassLibrary1.Class1.GetMessage(): {m.Invoke(null, null)}");

All 2 comments

You can use reflection from your Blazor, but because your application runs in the browser you don't have access to the file system like you typically would in Web Forms app. So you can't just read an assembly off of the local file system, but you could, for example, retrieve the assembly from the server.

var bytes = await Http.GetByteArrayAsync("_framework/_bin/ClassLibrary1.dll");
var assembly = System.Reflection.Assembly.Load(bytes);
var t = assembly.GetType("ClassLibrary1.Class1");
var m = t.GetMethod("GetMessage");
Console.WriteLine($"ClassLibrary1.Class1.GetMessage(): {m.Invoke(null, null)}");

@danroth27 I understand that, thank you!

Was this page helpful?
0 / 5 - 0 ratings