C#:结合的DllImport继承? [英] C#: combine DllImport with inheritance?

查看:216
本文介绍了C#:结合的DllImport继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些麻烦来了,我试图端口从Java中的一些code到C#。

Some trouble comes up for me while trying to port some code from java to c#.

起初,后面的java的code中的关键概念的一些解释: 现有code中的关键概念是一类进口/使用外部库的方法。这类实现一个接口,其中宣布大部分从外部库的方法。这样做的好处是创建像

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

接口1实例=新classImplementingInterface1();

Interface1 instance = new classImplementingInterface1();

我想端口code,它实现从外部库的界面和进口的方法。其实我不得不说的接口转换为一个抽象类,因为Java接口,利用不支持在实际上可以.NET接口包含pre定义的值,字段。

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.

这可能是我盯着点:

public abstract class abstractClassA
{
    public abstract int abstractMethodA(int parameter);
}

public class usualClass : abstractClassA
{
    [DllImort("ExternalLib.dll")]
    public static extern abstractMethodA(int parameter);
}

这是抽象类用于能够从类实现抽象类中创建实例,只需键入

An abstract class is used to be able to create instances from classes implementing that abstract class just by typing

abstractClassA instance = new usualClass();

好了,这就是我想要做的,但我想通了,这是行不通的,而我继承形成一个抽象类,我将不得不使用override语句我想要实现像

public class usualClass : abstractClassA
{
    public extern override abstractMethodA(int parameter);
}

这是行不通的结合的DllImport语句,因为它告诉我,使用的语句办法,同时声明:extern和static。添加override关键字来实现抽象类是不可能的,因为静态成员不能被声明为覆盖。所以我想我traped一些如何:/

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 :/

但实际上我想创建一个类从外部库命名的入口点。但我想这个类只需键入实现一个接口/抽象类必须创建的类实现此接口/抽象类实例的能力

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

abstractClassA instance = new usualClass();

我也尝试这个东西使用接口(但没有恼人的静电pre定义的字段),我发现该接口实现不工作结合的DllImport声明过,编译器说,命名的方法是静态的,因此无法实现接口的方法。这实际上是有意义的,但没有suiteable解决我的问题。

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.

你有经验的或furhter想法?

Do you have experiences with that or furhter ideas?

推荐答案

由于C#编译器说,该方法的必须的是静态外部。幸运的是的DllImport 入口点属性,它可以让你在C#中使用不同的名称(从而避免命名冲突)。例如:

As the C# compiler says the method MUST be static extern. Fortunately DllImport has the EntryPoint property which lets you use a different name in C# (thus avoiding naming conflicts). For example:

public abstract class AbstractClassA
{
    public abstract int AbstractMethodA(int parameter);
}

public class UsualClass : AbstractClassA
{
    [DllImport("ExternalLib.dll", EntryPoint = "abstractMethodA")]
    static extern int AbstractMethodAImport(int parameter);
    public override int AbstractMethodA(int parameter)
    {
        return AbstractMethodAImport(parameter);
    }
}

不过,您的code不遵循最佳实践(额外忌讳,是的,这就是你的名字的东西在Java中 - 但是当在罗马是罗马;请阅读了C#的命名约定)。你真的应该执行它,如下所示:

However, your code does not follow best practices (additional pet peeve, yes, that's how you name things in Java - but when in Rome be a Roman; please read up on the C# naming conventions). You should really implement it as follows:

public abstract class AbstractClassA
{
    public abstract int AbstractMethodA(int parameter);
}

public class UsualClass : AbstractClassA
{
    public override int AbstractMethodA(int parameter)
    {
        return NativeMethods.AbstractMethodA(parameter);
    }
}

[SuppressUnmanagedCodeSecurity]
internal class NativeMethods
{
    [DllImport("ExternalLib.dll", EntryPoint = "abstractMethodA")]
    public static extern int AbstractMethodA(int parameter);
}

始终保持在一个类中的实习医生,你的应该的通话 NativeMethods

这篇关于C#:结合的DllImport继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆