Would like to have a generic Wrapper class

For inheritance purposes, I would like to have the following class, which I cannot write myself:

class Wrapper<G> : G
{
    public Wrapper(G base);
    protected G GetBase();
}

He would inherit all members of G as common, and all applications of these members would be redirected to G, delivered during construction.

Would it be technically possible to add this functionality to C #?

The main way that I would like to use is:

    class Wrapper<G> : G
    {
        public Wrapper(G g);
    }

    class IGraphNode<G> where G : IGraphNode<G>
    {
        IEnumerable<G> ForwardNodes();
        IEnumerable<G> BackwardNodes();
    }

    //Reverses the direction of the graph.
    class Reverse<G> : Wrapper<G> where G : IGraphNode<G>
    {
        public Reverse(G g)
            : base(g)
        { }

        IEnumerable<G> ForwardNodes()
        {
            return base.BackwardNodes();
        }

        IEnumerable<G> BackwardNodes()
        {
            return base.ForwardNodes();
        }
    }
+3
source share
4 answers

Not sure if this is what you want, but you can use DynamicProxy to create wrapper objects at runtime and intercept method and call properties as needed.

+1
source

, Ruby, , . #. #? , .Net # ... , .

0

, ?

public static class MyStringExtensions
{
    public static string Add1ToString(this string myString)
    {
        myString += "1";
        return myString;
    }
}

:

using YourNamespaceOfTheExtensionMethods;

public class OtherClass
{
    public string DoSomething()
    {
        string someString = "Hello";
        return someString.Add1ToString();
    }
}

string , Object, .

0

, . ? , ? , , , , , , . , - , , .

, (), , .

class Wrapper<G> where G: SomeType
{
    public G Base;
    public Wrapper(G g) {
        Base = g;
    }

    // methods to do stuff with object supplied at construction
}

where, , , .

, , , , , , : . , , . , , MyWrapper.Base , , . , , , , ...

If you want to create a new object that extends something, and in some transparent way copies the properties of an existing object, then do something like this:

class Wrapper<G>: G
{
    public Wrapper(G g) {
        // add memberwise copy function
    }
    .. more properties
}

Wrapper<SomeType> newWrapper = Wrapper<SomeType>(someOtherObject);

.. like how you can create a new List from an existing one.

0
source

Source: https://habr.com/ru/post/1784975/


All Articles