Is there any other way to implement Overriding (no

2019-09-21 05:57发布

Could anyone tell me is there any other way a method can be overridden without using virtual/abstract/override in C#/.NET, Please provide me with an example.Please provide with an example... (what i am thinking is Extension methods am i correct.....)

标签: c# .net oop
3条回答
甜甜的少女心
2楼-- · 2019-09-21 06:30

You can use the "new" keyword on your method to "hide" a method that was not declared as abstract/virtual, however if the method is called from a variable type-casted as the base class, it won't call your new method. This is the similar to overriding.

Example:

public class A
{
    public string GetName() { return "A"; }
}

public class B : A
{
    // this method overrides the original
    public new string GetName() { return "B"; }
}

Extension methods allow you to add new methods to any class even if you don't have their source code or they're sealed. This is not the same as overriding

public sealed class A // this could even be from a dll that you don't have source code to
{
}
public static class AExtensionMethods
{
    // when AdditionalMethod gets called, it's as if it's from inside the class, and it            
    // has a reference to the object it was called from. However, you can't access
    // private/protected fields.
    public static string AdditionalMethod(this A instance)
    {
        return "asdf";
    }
}

Another option is to use interfaces so that you can have two completely different objects that both have the same method, but when called from a variable type-casted as the interface, it looks similar to overriding.

You could also use a form of Proxy Mocking framework like Microsoft Moles, or CastleWindsor -> They have a way of instantiating a "proxy" object that has the same interface as the real object, but can provide a different implementation for each method.

查看更多
放我归山
3楼-- · 2019-09-21 06:31

No, there is no other way. You can hide an existing method with new if the base method is not marked as virtual, however it does not have the same effect (there is no polymorphism - the call will be dispatched based on the variable type, not on the actual object type).

查看更多
乱世女痞
4楼-- · 2019-09-21 06:43

Extension will not work in this case. Objects own properties and methods take precedence. However you can overload a method using extensions.

You can override methods defined in the extensions, by keeping your extensions closer in the namespace to the place where you are going to use it.

Example:

namespace ConsoleApplication2

{

    using System;

    using ConsoleApplication3;

    internal class Program
    {
        private static void Main(string[] args)
        {
            ThirdPartyClass t = new ThirdPartyClass();
            Console.WriteLine(t.Fun("hh"));
            Console.WriteLine(t.Fun(1));
        }
    }


    public static class LocalExtension
    {
        public static string Fun(this ThirdPartyClass test, int val)
        {
            return "Local" + val;
        }

        public static string Fun(this ThirdPartyClass test, string val)
        {
            return "Local" + val;
        }
    }
}


namespace ConsoleApplication3

{

    public class ThirdPartyClass
    {
        public virtual string Fun(string val)
        {
            return "ThirdParty" + val.ToUpper();
        }
    }


    public static class ThripartyExtension
    {
        public static string Fun(this ThirdPartyClass test, int val)
        {
            return "ThirdParty" + val;
        }
    }
}
查看更多
登录 后发表回答