카테고리 없음

[C#] Load Assembly

딸기우유중독 2025. 3. 18. 17:01

 

dll 정적 참조

 

 


 

 

Assembly.LoadFrom

 

Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo[] Params = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System.String
//   Position = 0
//   Optional=False
foreach (ParameterInfo Param in Params)
{
    Console.WriteLine("Param=" + Param.Name.ToString());
    Console.WriteLine("  Type=" + Param.ParameterType.ToString());
    Console.WriteLine("  Position=" + Param.Position.ToString());
    Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
}

 

 

 


# 호출하는 TargetFramework 와 dll의 TargetFramework  가 일치해야 함.

 

ex) 

호출하는 프로젝트 (.exe or other .dll)

 

호출되는 프로젝트 (.dll)

 

 


 

 

현재 실행파일 경로에 있는  WPFLibrary.dll 어셈블리 로드
var library = Assembly.LoadFrom("./WPFLibrary.dll");

 

어셈블리 의 타입들

Type[] types = library.GetTypes();

 

중에 타입이림으 DialogClass 인 타입

var type = library.GetTypes().Where(x=>x.Name == "DialogClass").FirstOrDefault();

 

해당 타입을 인스턴스화

IDialogClass instance = Activator.CreateInstance(type) as IDialogClass;

 

인스터스화 한 타입의 ShowDialog 메서드 실행.

(instance).ShowDialog();

 

IDialogClass 타입으로 불러와서 메서드 호출까지 작동되게 하기 위해서는 

호출하는 (.exe or other .dll) 쪽 과 호출되는(.dll) 어셈블리간의 서로 공통된 타입(인터페이스)를 참조하고이 있어야 가능.

 

똑같은 IDialogClass 를 양쪽에 만들어서 하면 컴파일 에러는 피하지만 런타입 에러 일으킴.

 

하나의 독립된 IDialogClass 를 구성하는 프로젝트를 만들고

그 프로젝트를 (.exe or other .dll) 쪽 프로젝트랑 .dll 프로젝트에서 참조해서

 

.dll 프로젝트에서 IDialogClass 를 상속받아 Class를 구현하고

(.exe or other .dll)  프로젝트에서 IDialogClass 로 받아서 인스턴스 생성해야 정상작동.

 

IDialogClass 

 

.dll

 

 

(.exe or other .dll)

 

 


 

같은 인터페이스를 참조하지 않고 메서드를 호출하고 싶다면

 

dynamic 타입으로 받아서

메서드 호출 ( 흰색으로 컴파일 에러 없이 그냥 무지성 호출 함.)

 

 


 

Or

 

.dll 의 호출되는 메서드를 static으로 선언하고

 

 

호출하는곳에서 해당 메서드를 추출해서 Invoke

 

 



***  DevExpress 등 .dll 로드 관련 ***

 

Other Module (.dll) 에서  DevExpress 컴포넌트를 쓸 때 

Main (.exe) 에도  

<PackageReference Include="devexpress.wpf.ribbon" Version="23.1.5" />

추가해야 정상 작동하는데 

 

이유는 

 AppDomain.CurrentDomain 에 Assembly가 로드 되어야 .dll 을 부를 수 있음

 

Main (.exe) 프로젝트에 레퍼런스 안걸고 하려면

 

Main에서 특정 폴더(Other Module Dependant Framework )의 .dll 들을 

App() 에서 수동으로 Load 해 주어야 함.

 

ex)

.exe 실행폴더의  .dll 파일들 전부 load

 

 

Or

 

 

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> + Costura.fody 로 묶으면 가능.

 


개념설명

https://highfence.tistory.com/11

 

CLR via C# 23장 어셈블리 로딩과 리플렉션

Notion에서 보기 이번 장에서는 컴파일 타임에 전혀 알지 못했던 타입에 대해 타입의 세부 정보를 알아내고 인스턴스를 생성하는 등의 방법(리플렉션)에 대한 것을 배운다. 이는 주로 동적으로 확

highfence.tistory.com

 

https://bigexecution.tistory.com/238

 

C#] type, System.Type, System.Reflection

type : string, int, float, double 등등... 변수와 상수가 갖는 type이다. 함수는 type 형식의 parameters를 전달받고 values를 반환할 수 있다. type에는 여러 카테고리가 있는데, built-in value types, built-in reference typ

bigexecution.tistory.com

 

 


 

 

https://learn.microsoft.com/ko-kr/dotnet/api/system.reflection.assembly.loadfrom?view=net-8.0

 

Assembly.LoadFrom 메서드 (System.Reflection)

어셈블리를 로드합니다.

learn.microsoft.com

 

 

 

https://dlsenfl.tistory.com/entry/DXWPF-DevExpress-MVVM-for-WPF-New-Module-Injection-Framework

 

[DX_WPF] Dynamic Load Modules In DevExpress MVVM For WPF

방식 1. Libaray Nuget Package https://github.com/dlsenfl3/DXDynamicModule Nuget 추가. 사용. https://github.com/dlsenfl3/DXModularWithDynamic App.xaml.cs Project Module ViewModelSource 나 ViewModelBase 둘중 하나만 사용. (같은 거) ViewModelS

dlsenfl.tistory.com

 

 

728x90