Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: combine DllImport with inheritance?
    text
    copied!<p>Some trouble comes up for me while trying to port some code from java to c#.</p> <p>At first, some explanation of the key concept behind the java code: The key concept of the existing code is a class importing/using methods in an external library. This class implements an interface, which declares most of the methods from the external library. The benefit is the ability to create instances like</p> <p>Interface1 instance = new classImplementingInterface1();</p> <p>I'm trying to port code which implements an interface and imports methods from a external library. Actually I had to translate that interface to an abstract class because the java interface makes use of fields containing pre defined values, which is not supported in .NET interfaces acutally.</p> <p>That may be my staring point:</p> <pre><code>public abstract class abstractClassA { public abstract int abstractMethodA(int parameter); } public class usualClass : abstractClassA { [DllImort("ExternalLib.dll")] public static extern abstractMethodA(int parameter); } </code></pre> <p>An abstract class is used to be able to create instances from classes implementing that abstract class just by typing</p> <pre><code>abstractClassA instance = new usualClass(); </code></pre> <p>Ok, thats what I want to do, but I figured out that this won't work, while I inherit form an abstract class, i'll have to use the override statement for methods I want to implement like</p> <pre><code>public class usualClass : abstractClassA { public extern override abstractMethodA(int parameter); } </code></pre> <p>This will not work combined with the DllImport statement because it's telling me that methods using that statement shall declare both: extern and static. Adding the override keyword to implement the abstract class is not possible because a static member can't be declared as override. So I guess I'm traped some how :/</p> <p>But actually I want to create a class naming the entry point from an external library. But I want this class to implement an interface / abstract class to have the ability to create instances of classes implementing this interface / abstract class just by typing</p> <pre><code>abstractClassA instance = new usualClass(); </code></pre> <p>I also tried this stuff using an interface (but without annoying static pre defined fields) and I found out that interface implementation does not work combined with the DllImport statement too, the compiler says that the named method is static and therefore can not implement an interface method. That actually makes sense, but is no suiteable solution to my problem.</p> <p>Do you have experiences with that or furhter ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload