C#接口抽象类和回调 [英] C# Interfaces Abstract Classes and Callbacks

查看:371
本文介绍了C#接口抽象类和回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...寻求有关接口,抽象类和回调实现的一些帮助" ...

我们希望在C#中重新实现现有API时充分利用Microsoft的.Net 4. ...甚至可以达到机械生成API单元测试的程度...

因此,我们不仅必须编写接口和类的代码,还必须编写抽象类的代码.摘要是数据协定的去向(它们不能在接口中,当然,我们也不希望它们在类本身中).

困难发生在回调的规范中.给定一些需要回调方法的引用的方法

...asking for some "hand holding" regarding interfaces, abstract classes and the implementation of callbacks...

We''re hoping to leverage as much of Microsoft''s .Net 4 when re-implementing in C# an existing API. ...maybe even reach the point of being able to mechanically generate the unit tests for the API...

Because of that, we must code not just interfaces and classes but abstract classes, as well; abstracts are where the data contracts go (they can''t go in the interface and, of course, we don''t want them in the class, itself).

The difficulty is occurring in the specification of callbacks. Given some method that wants a called back method''s reference

void ControlMethod(CalledBackMethodType theMethod)
{
    save theMethod somewhere so that UsefulMethod (or whomever) can be invoked later;
}
void UsefulMethod(int someArgParm) { ... }
    ...
ControlMethod(UsefulMethod);


实施数据合同的食谱方法涉及以下内容的创建:
命名空间,称为NAPI ...
接口:IAPI ...
抽象类:AAPI ...
和支持类:CAPI ...


The cook book approach to implementing data contracts involves the creation of:
a namespace, call it, NAPI...
an interface: IAPI...
an abstract class: AAPI...
and the backing class: CAPI...

namespace NAPI
{
    [contract stuff referencing the abstract class AAPI]
    interface IAPI
    {
        void UsefulMethod(int someArgParm);
        void ControlMethod(CalledBackMethodType theMethod);
    }

    [contract stuff referencing the interface IAPI]
    abstract class AAPI : IAPI
    {
        void UsefulMethod(int someArgParm) { Contract.Requires(stuff); }
        void ControlMethod(CalledBackMethodType theMethod) { Contract.Requires(stuff); }
    }

    class CAPI : AAPI
    {
        No UsefulMethod, here, because that's somebody else's job.
        void ControlMethod(CalledBackMethod theMethod)
        {
            Code, knowing that theMethod parameter has been validated
        }
    }
}


...那是一场灾难.经过大量讨论,但并非所有内容都连贯,这似乎可以接受:


...and that was a disaster. After much futzing around, not all of it coherent, this seems to be accepted:

namespace NAPI
{
    [contract stuff referencing the abstract class ACB]
    interface ICB {
        void UsefulMethod(int someArgParm);
    }

    [contract stuff referencing the interface ICB]
    abstract class ACB : ICB
    {
        void UsefulMethod(int someArgParm) { Contract.Requires(stuff); }
    }

    [contract stuff referencing the abstract class AAPI]
    interface IAPI
    {
        void ControlMethod(ICB theMethod);
    }

    [contract stuff referencing the interface IAPI]
    abstract class AAPI : IAPI, ICB
    {
        void ControlMethod(ICB theMethod) { Contract.Requires(stuff); }
    }

    class CAPI : AAPI
    {
        void ControlMethod(ICB theMethod) { ... }
    }
}


...并且被接受,但是签名是接口ICB的签名,而不是该接口中声明的任何方法.这是怎么回事?

如果有人愿意为我理顺这件事,我将非常感激.我的眼睛痛苦地旋转着.


...and that is accepted, but the signature is of interface ICB rather than any method declared within that interface. What''s up with that?

If anybody would be so kind as to straighten this out for me, I''d really appreciate it. My eyeballs are spinning around painfully.

推荐答案

只是为我澄清一下...

第一个响应提到"delegate",它是C#中的特定语法元素(也不只是4.0).它允许一个人声明一个方法签名.

从OP看来,问题似乎是如何传递方法签名.

那么为什么委托"不是答案?
Just to clarify it for me...

The first response mentioned "delegate" which is a specific syntax element in C# (not just 4.0 either.) It allows one to declare a method signature.

And from the OP it seems like the question was how to pass a method signature.

So why is "delegate" not the answer?


这篇关于C#接口抽象类和回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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