抽象类继承的最派生类型 [英] Abstract class inheriting the most derived type

查看:131
本文介绍了抽象类继承的最派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我无法找到原来的项目,使我这个问题。 。这将有可能给这个问题更多的上下文

Unfortunatly, i can't find the original project which led me to this question. That would have perhaps given this question a bit more context.

编辑:我发现原来的项目我在看到了这一点:<一HREF =http://mews.codeplex.com/SourceControl/changeset/view/63120#1054567相对=nofollow> http://mews.codeplex.com/SourceControl/changeset/view/63120#1054567 与一个具体的实现: http://mews.codeplex.com/SourceControl /变更/查看/ 63120#1054606

I found the original project i've seen this in: http://mews.codeplex.com/SourceControl/changeset/view/63120#1054567 with a concrete implementation at: http://mews.codeplex.com/SourceControl/changeset/view/63120#1054606

可以说我有一个具体的实现,它有用的东西像一个抽象类:

Lets say i have an abstract class with a concrete implementation that does something usefull like:

abstract class AbstractClass
{
    public virtual int ConvertNumber(string number)
    {
        string preparedNumber = Prepare(number);
        int result = StringToInt32(number);
        return result;
    }

    protected abstract string Prepare(string number);

    protected virtual int StringToInt32(string number)
    {
        return Convert.ToInt32(number);
    }
}

sealed class ConcreteClass : AbstractClass
{
    protected override string Prepare(string number)
    {
        return number.Trim();
    }

    public override int ConvertNumber(string number)
    {
        return base.ConvertNumber(number);
    }
}

这是基本,因为它得到。现在,在代码中我已经看到了网络笔者从最派生类型继承抽象类实现的传承上,例如:

This is as basic as it gets. Now in the code i've seen on the web the author implemented inheritence by inheriting the Abstract class from the most derived type, e.g:

abstract class AbstractGenericClass<TGenericClass>
    where TGenericClass : AbstractGenericClass<TGenericClass>
{
    public virtual int ConvertNumber(string number)
    {
        string preparedNumber = Prepare(number);
        int result = StringToInt32(number);
        return result;
    }

    protected abstract string Prepare(string number);

    protected int StringToInt32(string number)
    {
        return Convert.ToInt32(number);
    }
}

sealed class ConcreteGenericClass : AbstractGenericClass<ConcreteGenericClass>
{
    protected override string Prepare(string number)
    {
        return number.Trim();
    }

    public override int ConvertNumber(string number)
    {
        return base.ConvertNumber(number);
    }
}



现在,人们为什么会做这样的事情?我很依稀记得这是在ATL中大量用于性能原因的技术(调用具体成员实现不使用虚函数表工作的一些方法?)我不是这部分非常肯定。

Now why would one do such a thing? I very vaguely remember this was a technique heavily used in ATL for performance reasons (some way of calling concrete member implementations without working with a vtable?) I'm not very sure on this part.

我检查生成的IL这两种情况下,他们是完全一样的。

I've checked the generated IL for both cases and they are exactly the same.

谁能给我讲解一下?

推荐答案

这就是所谓在C奇怪的循环模板模式++中的C#版本。这是一个有点怪异,我个人来看,尽量避免它。它是有用的,但我觉得比用更加混乱。

That is a C# version of what is called the Curiously Recurring Template Pattern in C++. It is a bit weird, and personally, I try to avoid it. It is useful, but I find it more confusing than useful.

请参阅我的文章的详细信息:

See my article for details:

http://blogs.msdn.com/b/ericlippert/archive/2011/02/03/curiouser -and-curiouser.aspx

这篇关于抽象类继承的最派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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