Nhận các phương thức giao diện từ một lớp được tải động trong .NET (Getting interface methods from a dynamically loaded class in .NET)


問題描述

Nhận các phương thức giao diện từ một lớp được tải động trong .NET (Getting interface methods from a dynamically loaded class in .NET)

I've got a .dll library I'm writing that interfaces with a proprietary COM assembly.  My goal is to publish my work online once it's built, however I need to remove the COM assembly as a project reference to avoid distribution of this proprietary dll.  So what I'm trying to be able to do is dynamically load the assembly at runtime, and invoke methods where I need them.  Traditionally I've used object reflection for unknown types, however this can get slow at times, and involves several ugly instanciations.  In .NET 4.0 is there a way to cleanly get all the methods and toss them into a dynamic class/interface at runtime? 

I'm currently getting the object like this:

Type myClassType = Type.GetTypeFromProgID("MyClass.Myclass.1");
object classObj = Activator.CreateInstance(myClassType);

I thought I'd be able to use Type.GetMethods(), however it only returns the generic ones (Equals, ToString, GetLifetime..., etc.).  I know the class uses an interface, so I tried looking into dynamically loading the interface also.  That led me to Reflection.Emit, and the Microsoft.Xrm.Sdk, which I am failing to understand so far.

If there's a way for me to invoke methods without needing to throw a bunch of BindingFlags every few lines, I'd greatly appreciate a nudge in the right direction

I do have the GUID for both the Class and the Interface if that helps at all.


參考解法

方法 1:

If I were at your situation(and if i have understand the problem correctly): I would separate the Interface,s library (DLL) from implementation library(DLL) Then the implementation library would be loaded dynamically and my main source code that is referenced to Interface library would be complied

Type myClassType = Type.GetTypeFromProgID("MyClass.Myclass.1");
myClassIntrface classObj = 
   Activator.CreateInstance(myClassType) as myClassIntrface;
classObj.CallSomeInterfaceMethod();

hope this will be useful pal.

(by Parrish HusbandMohsen Heydari)

參考文件

  1. Getting interface methods from a dynamically loaded class in .NET (CC BY‑SA 3.0/4.0)

#assemblies #dllimport #reflection.emit #reflection #C#






相關問題

Nhận các phương thức giao diện từ một lớp được tải động trong .NET (Getting interface methods from a dynamically loaded class in .NET)

支持超過 2 個版本的項目 (Supporting more then 2 versions of a project)

無法加載混淆程序集 (Cannot load obfuscated assembly)

如何禁止引用 .NET DLL 類庫 (How to forbid a .NET DLL class library to be referenced)

我可以從命令行運行 .NET 程序集的代碼嗎? (Can I run code from a .NET assembly from a command line?)

如何將一個應用程序組裝到另一個應用程序中? (How to use assembly of one application in to another application?)

從程序集中復制 DLL 以進行部署 (Copy DLL From Assembly For Deployment)

是否可以查看程序集的源代碼?如果是,那麼如何? (Is it possible to view source code of assembly? If yes then How?)

組裝成字節 (Assembly to Bytes)

DLL 與程序集 (DLL versus Assembly)

pdb 是否提供有關源文件中類型定義的信息? (Does pdb provide information about type definitions in source files?)

如何在 .NET 程序集中重命名 [空格] 類型 (How can I rename[space] a type in a .NET assembly)







留言討論