Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
XML
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)
        {
            ....
        }
    }
}

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!
Posted
Comments
[no name] 19-Jun-12 15:58pm    
public override void Create<T>(T item) { base.Create<T>(item); }
thomas_wingfield 19-Jun-12 16:04pm    
Wes Aday. If I want that derived Create methd should seems on Create in base class, but with one different instruction in derived method.If I would call base
I can't add this instruction.I need Create method with own logic in derived class without logic of base class method
Karthik. A 19-Jun-12 16:06pm    
Exactly, Wes Aday and my answer's are just stubs. You just have to remove the call base.Create<t>(item) and add your own implementation.
[no name] 19-Jun-12 16:09pm    
That make absolutely no sense at all. Are you overriding Create or not?
thomas_wingfield 19-Jun-12 16:16pm    
Yes I'm overriding but it should seems to Create of base class but a little bit another

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:

C#
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:
C#
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:
C#
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


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

—SA
 
Share this answer
 
v4
Comments
thomas_wingfield 20-Jun-12 12:05pm    
Sergey Alexandrovich Kryukov thanks a lot for your help.Actually I decided how do it, but I will try your solution
Sergey Alexandrovich Kryukov 21-Jun-12 11:22am    
You are very welcome.
This is just one of the possible ideas.
As you seem to see some reason in it, please also accept it formally (green button) -- thanks.
--SA
_Amy 30-Jun-12 0:52am    
My +5. Good answer as always..
Sergey Alexandrovich Kryukov 30-Jun-12 1:54am    
Thank you, A K.
--SA
That's because the signature of the Create method is wrong. Change it to the following

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


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!
 
Share this answer
 
Comments
thomas_wingfield 19-Jun-12 15:57pm    
Sorry but it doesn't help.But thanks for post
Karthik. A 19-Jun-12 16:03pm    
Sorry about that. But are you saying you get an error if you do this? You cannot just override a generic method with a concrete type, if you are trying to achieve something in the effect of what you have posted. Can you pls. elaborate your issue?
[no name] 19-Jun-12 16:03pm    
What do you mean "it doesn't help"?
thomas_wingfield 19-Jun-12 16:05pm    
check my comment for my question
Karthik. A 19-Jun-12 16:06pm    
Pls. check my reply for your comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900