找到不适合的方法来覆盖泛型 [英] not suitable method found to override generic

查看:100
本文介绍了找到不适合的方法来覆盖泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class SomeRepository<TContext> : IDisposable
            where TContext : DbContext, new()
        {
            protected TContext context;
            protected SomeRepository()
            { }

        public virtual void Create<T>(T item) where T : class, new()
        {
            ...
        }
        }

    internal class SomeCrud : SomeRepository<SomeContext>
    {
        public override void Create(ConcreteType item)
        {
            ....
        }
    }
}


你好,我有一个像SomeRepository这样的基类,它有四个标准方法,每种方法都可以使用不同的类型,效果很好.
但是我遇到一种情况,我需要向派生类中的这些方法之一添加一些指令.这就是为什么我像基类中的virtual一样签名并尝试使用具体类型覆盖其中一个方法,并且出现错误


Hello I have base class like SomeRepository with four standart methods.Each one this method can work with different types.That works good.
But I got a situation that I need to add some instruction to one of those methods in derived class.That''s why I signed like virtual in base class and tried to override one of this method with concrete type and I got a error
not suitable method found to override on deriveed overriden method please help me!

推荐答案

类似于C ++模板专业化,这可以称为通用专业化. />
在使用通用方法之前,您的工作似乎无用.通过覆盖具体类型不可能专门实现该实现.我想给你一个替代的主意:将通用参数从方法移动到类型.想一想:

This is something which could called generic specialization, by the analogy with C++ template specialization.

It looks like your efforts are useless until you work with generic methods; it is not possible to specialize the implementation via overriding for a concrete type. I want to give you an alternative idea: move a generic parameter from a method to a type. Just think about it:

class Base<T> {
    internal protected virtual void Create(T item) { /*...*/ } // maybe, pseudo-abstract
} //class Base<T>

struct ConcreteType {/*...*/}

//this is something you really can do
class Derived : Base<ConcreteType> {
    internal protected override void Create(ConcreteType item) { /*...*/ }
} //class Derived



与您想要的东西非常相似,但是通过这种方式它可以工作.

通常,第一种方法是抽象的:



Pretty similar to what you wanted, but in this way it can work.

More typically, the first method would be abstract:

abstract class Base<T> {
    internal protected abstract void Create(T item)
} //class Base<T>

class Derived : Base<ConcreteType> {
    internal protected override void Create(ConcreteType item) { /*...*/ }
} //class Derived



请注意,您不必专用于派生类型中的所有通用参数,但仍可以使用专用通用参数覆盖该方法.这是怎么做的:



Note that you don''t have to specialize all the generic parameters in the derived type, but still can override the method with a specialized generic parameter; this is how to do it:

abstract class Base<SomeType, ParameterType> {
    internal protected abstract void Create(ParameterType item);
    // use SomeType in some other way
} //class Base<T>

class Derived<SomeType> : Base<SomeType, ConcreteType> { //second generic parameter "specialized out"
    internal protected override void Create(ConcreteType item) { /*...*/ }
    // use SomeType in some other way
} //class Derived



你明白了吗?这样,您可以获得足够的灵活性,可以满足您的目标.再次考虑一下.

—SA



Are you getting it? This way you can get yourself enough flexibility which could suit your goals. Again, think about it.

—SA


这是因为Create方法的签名错误.将其更改为以下

That''s because the signature of the Create method is wrong. Change it to the following

internal class SomeCrud : SomeRepository<SomeContext>
{
    public override void Create<T>(T item)
    {
        base.Create<T>(item);
    }
}



要做的快捷方式是,在SomeCrud类中,首先输入override,然后提供一个空格.现在,您将获得可以覆盖的项目列表.只需选择一个,您将获得带有对基类的方法的调用的存根.

希望这会有所帮助!



Shortcut to do this is, in the SomeCrud class, enter override first, then give a space. Now you will get a list of items that can be overridden. Just choose one, you will get the stub w/ a call to the base class''s method.

Hope this helps!


这篇关于找到不适合的方法来覆盖泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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