Implementing Interface using Reflection c# - c#

I have a requirement where I need to implement an interface in my project which is present in a third part DLL. I'm loading this DLL using reflection. Is it Possible?
class MyClass : I3rdPartyInterface
{
//implementing interface
}
Here, the I3rdPartyInterface is the interface present in my 3rd Party DLL.
This DLL could, or could not, be present on Client machine my software product is installed. If it is present, then i should implement the interface methods.

Your best option is to write a new project that references that dll and implements the interface - and load that in runtime.
The new dll "translates" between the external dll types and your application's types - that way there is no direct dependency between your app and the external dll.
You can generate and compile code at runtime but for this scenario it's just simpler to create a wrapper/translator dll

Related

Embed native dll or .NET assembly into COM

I have a .NET library and I want to wrap it by COM to invoke its functions from C++. Fortunately, this library is open source and if I add COM-visible class right inside this project - it works:
[Guid("38F752CC-20F1-4729-B1E3-EE0AAD145052")]
public interface IQRCodeUI
{
string GetDecodedString(string encodedString);
}
[Guid("D4CFCDFA-6718-494D-A23F-EBC0F9550377")]
public class QRCodeUI : IQRCodeUI
{
public string GetDecodedString(string encodedString)
{
return decoder.decode(encodedString);
}
}
decoder is a class from this very library.
But what to do in case if I would have compiled assembly? I tried to create class library (COM) and add .NET library as embedded resource to it. Without results! During compile it said something like cannot register assembly "path\name". Cannot load file or assembly "nameOfAssembly" or its dependency. Cannot find the file. Apologise, I can't provide original text of the error, because I have MSVS which language differs from English. Is it possible to resolve this issue?
If your task is to call .NET library from C++ via COM, why you're adding .NET library as an embedded resource into some new COM class library?
Just make existing third party assembly COM-visible and call it from C++, VB6, etc.
There are a number of docs all over the internet of how to make .NET assembly COM-visible. None of them, as I recall, contain recommendations to embed .NET assembly into COM class assembly.
Start with:
Best Practice in Writing a COM-Visible Assembly (C#)

Access .net dll through JNA in java

I have a .net 4.0 dll it has a namespace and in that namespace there is a class, I wants to access procedures inside that class using jna.
I have included jna.jar as well as platform.jar(in case) using maven,
My java code looks like this
MyConfiguration interface
import com.sun.jna.Library;
public interface MyConfiguration extends Library{
public void callInterface();
}
Accessing dll code
MyConfiguration myAPI = (MyConfiguration) Native
.loadLibrary("dll/MyAPI.dll", MyConfiguration.class);
System.out.println("Interface Created");
System.out.println("Calling Interface");
myAPI.callInterface();
but i am getting the exception--->
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'myInterface': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:208)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
at com.sun.jna.Library$Handler.invoke(Library.java:199)
at com.sun.proxy.$Proxy0.myInterface(Unknown Source)
at foo.App.main(App.java:83)
I have checked the dll using dll decompiler tool, and it has the called function, can somebody help out.
dll using dll decompiler tool
You need to use a PE (portable executable) viewer to look for entries in the export table. (Depends is one.)
Most .NET DLLs don't export functions that way. When they do, it's through a mechanism called Reverse P/Invoke, which isn't supported by most Microsoft .NET language compilers. The C++/CLI language was designed for this purpose.
You might find a shorter path to success with a Java-.NET bridge product. Or, a Java-COM bridge product if the .NET DLL exposes classes as COM objects. (Use OLE/COM Object Viewer to inspect a COM DLL.)
Also, be sure the DLL has the same bitness as your JVM process (e.g., java.exe or javaw.exe), unless you are using as an out-of-process COM object.

Do all assemblies in my project need to be recompiled and re-deployed if a class changes?

If I have a class library with a certain number of public classes (5 for this example) and I have 3 projects that depend on this library (for this example) and I decide to change one of the classes, but only 1 of the 3 projects depend on that class.
Do I need to compile/link/deploy all 3 projects?
Basically does .NET depend on the names of the classes or does it have some type of addressing dependency? (I have a c++ background, so I know in c++ I would have to redeploy all 3 projects).
If the class inherits from an interface, and you do not change the interface (but only the class implementation), then you don't have to change the dependent assemblies if they rely solely on the interface for communication with the class.
If you change a class's API (like a method signature or name) on which a dependent assembly relies, then you have to fix the dependent assembly to use the new API. This includes changes to the name of the class itself.
So the key here is to provide a stable API. If you change the API, then you have to change the code that depends on the API. If an assembly is not dependent (i.e. it doesn't rely on any API dependencies that you are changing), then you don't have to re-deploy it.
If you recompile the class library project, creating a new DLL, you do not have to recompile the projects that do not depend on the changes to the DLL as long as:
the public interface that each project depends on does not change in a breaking manner.
The projects are still compatible with the new version number of the DLL.
If your dependent projects do not require a specific version number or version range of the DLL, the version consideration does not apply.

Is there a way to use reflection to examine an assembly in isolation?

I have a plugin assembly, that references 3rd party assemblies for the application that it "plugs into". I then have a utility that runs this 3rd party application, passing it the full name of the class, the assembly name and the method in my plugin that it is to run.
I could hardcode these details as string constants in the utility. Instead though, I'd like to obtain them at runtime using reflection. The plugin class implements a specific interface and the method is annotated with a custom attribute. So it ought to be possible I'd have thought. I added a specific reflection class to the plugin and call that to get the details. But it falls over at the (representative) line:
Assembly.GetExecutingAssembly().GetExportedTypes() with a FileNotFoundException due to trying to load one of the 3rd party assemblies. Due to licensing rules, I do not want to link the utility directly to these 3rd party assemblies.
So my question is, how do I get a list of classes in my plugin assembly without triggering the attempted loading of other referenced assemblies? Is it possible, or must I resort to my string constants?
Mono.Cecil

How to deploy a COM

I just finished building my new COM project (C#, .NET 3.5). This project will be called by a VFP application. It's working great on my development machine, but now I need to know how to deploy it on the user's machine. Click Once isn't available for this kind of project, so I guess I'm stuck with manually distributing the DLL.
So, where should I put the DLL and how do I register it?
BTW, the 3.5 framework is already installed on the user's machine.
TIA
I've really never used RegSvr32 with .Net assemblies, rather I use the regasm with the /codebase option:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\regasm.exe /codebase mydll.dll
You can also use the /tlb option to export the type library and register it.
Of course the easiest way, just create an installer with vstudio and it will do this for you.
Creating a Description of the COM class and Interfaces
.Net assemblies don't include information in Type Library compatible format. So it is necessary for the programmer to run one of two .Net-supplied utilities to extract the assembly description of a class into a Type Library file.
One utility is TLBEXP.EXE, the .Net Type Library Exporter. This command line utility takes as input the name of an assembly DLL file to be converted to a Type Library. The programmer can also specify the name of a Type Library file to be created.
tlbexp ComServer.dll /out:ComServer.tlb
Assembly exported to C:\Magellan\Source\Output\Debug\ComServer.tlb
Once a Type Library has been created, it can be referenced by a COM client to obtain the information necessary for the COM client to bind to the interfaces of the COM class, and activate the COM class at runtime.
Registration of the COM Class and Interfaces
For a COM class to be accessible by the client at runtime, the COM infrastructure must know how to locate the code that implements the COM class. The following command accomplishes this:
regasm ComServer.dll
Your DLL can be put anywhere you wish, but a good choice is C:\Program Files\MyApplication.
http://www.csharphelp.com/archives/archive190.html

Categories

Resources